default resolver for Query
See original GitHub issuehi , im trying to create resolver for my Types . … should i always first define resolver for RootQuery Field ?
my Code:
from ariadne import QueryType, make_executable_schema, ObjectType
from ariadne.asgi import GraphQL
type_defs = """
type Query {
user: User
}
type User {
username: String
fullname: String
}
"""
query = QueryType()
user = ObjectType('User')
@query.field('user')
def resolve_user(_,info,**kwargs):
return 'something'
@user.field("username")
def resolve_username(_, info, **kwargs):
request = info.context["request"]
user_agent = request.headers.get("user-agent", "guest")
return "Hello, %s!" % user_agent
schema = make_executable_schema(type_defs, [query,user])
app = GraphQL(schema, debug=True)
the resolve_username
is not working when i remove resolve_user
th binded to Query
and also resolve_username
doesnt work when i just pass
the resolve_user
needed more Examples for Modularization in Doc .
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Resolvers - Apollo GraphQL Docs
A resolver is a function that's responsible for populating the data for a single field in your schema. It can populate that data...
Read more >Resolvers – GraphQL Tools
Default Resolver · Returns a property from obj with the relevant field name, or · Calls a function on obj with the relevant...
Read more >Understanding the Apollo Default Resolver for graphql-tools
The documentation goes on to state that the default resolver will look for a property on the parent object with the field name...
Read more >GraphQL Resolvers: Best Practices | by Mark Stuart - Medium
Every GraphQL query goes through three phases. ... A default resolver will look in root to find a property with the same name...
Read more >Resolvers - Ariadne GraphQL
A resolver needs to be bound to a valid type's field in the schema in order to be used during the query execution....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@salvador-immortal That’s how GraphQL works: if any of the parent resolvers (in your case the
user
resolver on theQuery
type) returnsNULL
, query resolution won’t go any deeper. You have to either define a user resolver or provide aroot_value
that includes a value for theuser
field (so the default resolver returns it).In your case, the logic should probably live in the
Query.user
resolver and not in theUser.username
resolver. Consider:In this case, you can skip defining any resolvers for the
User
type as the default resolver will see a matching key in the dictionary returned byresolve_user
and use it whenusername
is requested.In a large application, you’d typically define resolvers, data loaders, and other helpers in their own specific modules and import their particular lists of resolvers and type definitions in one place where you combine the schema. You can also use the
extend type ...
syntax to add the module-specific fields to different types (includingQuery
).