-
-
Notifications
You must be signed in to change notification settings - Fork 216
Open
Description
Line range hint 44-53: Expand the GraphQL example to be more comprehensive.
While the current example is correct, consider enhancing it with:
- Examples of mutations
- Complex types
- Error handling
- 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)Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels