Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphQL: Enhance documentation #572

Open
amotl opened this issue Oct 31, 2024 · 0 comments
Open

GraphQL: Enhance documentation #572

amotl opened this issue Oct 31, 2024 · 0 comments

Comments

@amotl
Copy link
Collaborator

amotl commented Oct 31, 2024

Line range hint 44-53: Expand the GraphQL example to be more comprehensive.

While the current example is correct, consider enhancing it with:

  1. Examples of mutations
  2. Complex types
  3. Error handling
  4. Context usage example (currently mentioned separately)

Example enhancement:

import graphene
from responder.ext.graphql import GraphQLView

# Define types
class User(graphene.ObjectType):
    id = graphene.ID()
    name = graphene.String()

# Queries
class Query(graphene.ObjectType):
    hello = graphene.String(name=graphene.String(default_value="stranger"))
    user = graphene.Field(User, id=graphene.ID(required=True))

    def resolve_hello(self, info, name):
        # Example of using context
        request = info.context['request']
        return f"Hello {name} from {request.url.path}"

    def resolve_user(self, info, id):
        # Example of error handling
        try:
            return User(id=id, name="Test User")
        except Exception as e:
            raise graphene.GraphQLError(f"Failed to fetch user: {str(e)}")

# Mutations
class CreateUser(graphene.Mutation):
    class Arguments:
        name = graphene.String(required=True)

    user = graphene.Field(User)

    def mutate(self, info, name):
        user = User(id="1", name=name)
        return CreateUser(user=user)

class Mutation(graphene.ObjectType):
    create_user = CreateUser.Field()

# Schema setup
schema = graphene.Schema(query=Query, mutation=Mutation)
view = GraphQLView(api=api, schema=schema)
api.add_route("/graph", view)

-- #554 (review)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant