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

Nested field resolver #101

Closed
sinwires opened this issue Feb 2, 2019 · 4 comments
Closed

Nested field resolver #101

sinwires opened this issue Feb 2, 2019 · 4 comments
Labels
question Further information is requested

Comments

@sinwires
Copy link

sinwires commented Feb 2, 2019

Hello!
I have a question about resolving in nested field. I don't realize if there is a bug or that functionality is not exist yet.
I have this schema and resolver:

from ariadne import ResolverMap, gql, start_simple_server, snake_case_fallback_resolvers

type_defs = """
schema {
  query: RootQuery
}

type RootQuery {
  public: PublicEntryPoint
}

type PublicEntryPoint {
  users: [UserSchema]
}

type UserSchema {
  userId: Int
}
"""

def resolve_users(obj, info):
    print('RESOLVING USERS!')
    return [{'user_id': 1}, {'user_id': 2}]

users_map = ResolverMap("PublicEntryPoint")
users_map.field("users", resolver=resolve_users)

start_simple_server(type_defs, [users_map, snake_case_fallback_resolvers])

But when I make the query to receive data

query {
  public {
    users {
      userId
    }
  }
}

I get

{
  "data": {
    "public": null
  }
}

And the resolver function is not called! Is this correct?
Thanks

@rafalp
Copy link
Contributor

rafalp commented Feb 2, 2019

Hi, you also need to define ResolverMap for RootQuery type. Otherwise public field is resolved to null, breaking the resolution chain.

@sinwires
Copy link
Author

sinwires commented Feb 2, 2019

Thanks, worked!

@sinwires sinwires closed this as completed Feb 2, 2019
@rafalp rafalp added the question Further information is requested label Feb 2, 2019
@kolypto
Copy link

kolypto commented Jan 25, 2021

NOTE for those of us coming from Google: ResolverMap has been renamed to ObjectType :)

@kolypto
Copy link

kolypto commented Jan 25, 2021

A working example for nested queries, where an object works as a sort of a namespace:

from ariadne import ObjectType, QueryType
from ariadne import gql
from ariadne import graphql_sync
from ariadne import make_executable_schema
from graphql import GraphQLResolveInfo

GRAPHQL_SCHEMA = gql('''
type Query {
    sub: SubQuery!
}

type SubQuery {
    hello: String!
}
''')


Query = QueryType()


@Query.field('sub')
def resolve_sub(*_):
    return SubQuery


SubQuery = ObjectType('SubQuery')

@SubQuery.field('hello')
def resolve_hello(_, info: GraphQLResolveInfo):
    return 'hi'

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

No branches or pull requests

3 participants