Skip to content

GraphQL: Enhance documentation #572

@amotl

Description

@amotl

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions