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},
)
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
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))
schema = graphene.Schema(Query)
result = schema.execute(
'''query getUser($id: ID) {
user(id: $id) {
id
firstName
lastName
}
}''',
variables={'id': 12},
)