Graphene: Incomplete example about query variables

Created on 14 Jan 2019  路  2Comments  路  Source: graphql-python/graphene

In the docs, there's an example about how to feed variables to a query (link)

However, the example is incomplete as there is nothing demonstrating how to consume the variables in the resolver:

class Query(graphene.ObjectType):
    user = graphene.Field(User)

    def resolve_user(self, info):
        return info.context.get('user')

schema = graphene.Schema(Query)
result = schema.execute(
    '''query getUser($id: ID) {
        user(id: $id) {
            id
            firstName
            lastName
        }
    }''',
    variables={'id': 12},
)
good first issue 馃悰 bug 馃摉 documentation

Most helpful comment

Yes you're right. The code should be:

```python
class Query(graphene.ObjectType):
user = graphene.Field(User, id=graphene.ID(required=True))

def resolve_user(_, info, id):
    return get_user_by_id(id)

schema = graphene.Schema(Query)
result = schema.execute(
'''query getUser($id: ID) {
user(id: $id) {
id
firstName
lastName
}
}''',
variables={'id': 12},
)

All 2 comments

Yes you're right. The code should be:

```python
class Query(graphene.ObjectType):
user = graphene.Field(User, id=graphene.ID(required=True))

def resolve_user(_, info, id):
    return get_user_by_id(id)

schema = graphene.Schema(Query)
result = schema.execute(
'''query getUser($id: ID) {
user(id: $id) {
id
firstName
lastName
}
}''',
variables={'id': 12},
)

@jkimbo How can I use a customized class as args in graphene.Field?

class AdGroupConfigChangesArguments(graphene.InputObjectType):
    platform = graphene.String()
    platform_id = graphene.String()
    bid_strategy = graphene.String()

I tried with:

graphene.Field( graphene.List(AdGroupConfigChanges), args=AdGroupConfigChangesArguments, resolver=adgroup_config_changes_resolver)

But it gives an error:

AssertionError: Arguments in a field have to be a mapping

Was this page helpful?
0 / 5 - 0 ratings