question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

default resolver for Query

See original GitHub issue

hi , 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:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
patryscommented, Aug 20, 2019

@salvador-immortal That’s how GraphQL works: if any of the parent resolvers (in your case the user resolver on the Query type) returns NULL, query resolution won’t go any deeper. You have to either define a user resolver or provide a root_value that includes a value for the user field (so the default resolver returns it).

In your case, the logic should probably live in the Query.user resolver and not in the User.username resolver. Consider:

query = QueryType()

@query.field('user')
def resolve_user(_, info, **kwargs):
    request = info.context["request"]
    user_agent = request.headers.get("user-agent", "guest")
    username = "Hello, %s!" % user_agent
    return {
        "username": username,
        "fullName": "whatever",
    }

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 by resolve_user and use it when username is requested.

1reaction
patryscommented, Aug 20, 2019

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 (including Query).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found