2019-06-10 02:49:56 +03:00
.. _SchemaExecute:
2017-04-20 10:53:17 +03:00
Executing a query
=================
2019-12-20 10:02:45 +03:00
For executing a query against a schema, you can directly call the `` execute `` method on it.
2017-04-20 10:53:17 +03:00
.. code :: python
2017-04-27 02:27:44 +03:00
2019-06-10 02:49:56 +03:00
from graphene import Schema
schema = Schema(...)
2017-04-20 10:53:17 +03:00
result = schema.execute('{ name }')
2017-04-27 02:27:44 +03:00
`` result `` represents the result of execution. `` result.data `` is the result of executing the query, `` result.errors `` is `` None `` if no errors occurred, and is a non-empty list if an error occurred.
2017-04-20 10:53:17 +03:00
2020-03-14 19:48:12 +03:00
For executing a subscription, you can directly call the `` subscribe `` method on it.
This method is async and must be awaited.
.. code :: python
import asyncio
from datetime import datetime
from graphene import ObjectType, String, Schema, Field
# All schema require a query.
class Query(ObjectType):
hello = String()
def resolve_hello(root, info):
return 'Hello, world!'
class Subscription(ObjectType):
time_of_day = Field(String)
async def subscribe_time_of_day(root, info):
while True:
yield { 'time_of_day': datetime.now().isoformat()}
await asyncio.sleep(1)
SCHEMA = Schema(query=Query, subscription=Subscription)
async def main(schema):
subscription = 'subscription { timeOfDay }'
result = await schema.subscribe(subscription)
async for item in result:
print(item.data['timeOfDay'])
asyncio.run(main(SCHEMA))
The `` result `` is an async iterator which yields items in the same manner as a query.
2019-06-10 02:49:56 +03:00
.. _SchemaExecuteContext:
2017-04-20 10:53:17 +03:00
Context
_______
2018-08-29 12:35:44 +03:00
You can pass context to a query via `` context `` .
2017-04-20 10:53:17 +03:00
.. code :: python
2019-06-10 02:49:56 +03:00
from graphene import ObjectType, String, Schema
class Query(ObjectType):
name = String()
2017-04-20 10:53:17 +03:00
2019-03-28 02:44:41 +03:00
def resolve_name(root, info):
2017-07-27 13:00:21 +03:00
return info.context.get('name')
2017-04-27 02:27:44 +03:00
2019-06-10 02:49:56 +03:00
schema = Schema(Query)
2018-08-29 12:35:44 +03:00
result = schema.execute('{ name }', context={'name': 'Syrus'})
2019-06-10 02:49:56 +03:00
assert result.data['name'] == 'Syrus'
2017-07-12 19:52:46 +03:00
Variables
2019-03-28 02:44:41 +03:00
_________
2017-07-12 19:52:46 +03:00
2018-08-29 12:35:44 +03:00
You can pass variables to a query via `` variables `` .
2017-07-12 19:52:46 +03:00
.. code :: python
2019-06-10 02:49:56 +03:00
from graphene import ObjectType, Field, ID, Schema
class Query(ObjectType):
user = Field(User, id=ID(required=True))
2017-07-12 19:52:46 +03:00
2019-03-28 02:44:41 +03:00
def resolve_user(root, info, id):
return get_user_by_id(id)
2017-07-12 19:52:46 +03:00
2019-06-10 02:49:56 +03:00
schema = Schema(Query)
2017-07-12 19:52:46 +03:00
result = schema.execute(
2019-04-08 22:33:29 +03:00
'''
2019-04-08 22:36:35 +03:00
query getUser($id: ID) {
user(id: $id) {
id
firstName
lastName
2017-07-12 19:52:46 +03:00
}
2019-04-08 22:36:35 +03:00
}
2019-04-08 22:33:29 +03:00
''',
2018-08-29 12:35:44 +03:00
variables={'id': 12},
2017-07-12 19:52:46 +03:00
)
2019-06-10 02:49:56 +03:00
Root Value
__________
Value used for :ref: `ResolverParamParent` in root queries and mutations can be overridden using `` root `` parameter.
.. code :: python
from graphene import ObjectType, Field, Schema
class Query(ObjectType):
me = Field(User)
def resolve_user(root, info):
return {'id': root.id, 'firstName': root.name}
schema = Schema(Query)
user_root = User(id=12, name='bob'}
result = schema.execute(
'''
query getUser {
user {
id
firstName
lastName
}
}
''',
root=user_root
)
assert result.data['user']['id'] == user_root.id
Operation Name
______________
If there are multiple operations defined in a query string, `` operation_name `` should be used to indicate which should be executed.
.. code :: python
from graphene import ObjectType, Field, Schema
class Query(ObjectType):
me = Field(User)
def resolve_user(root, info):
return get_user_by_id(12)
schema = Schema(Query)
query_string = '''
query getUserWithFirstName {
user {
id
firstName
lastName
}
}
query getUserWithFullName {
user {
id
fullName
}
}
'''
result = schema.execute(
query_string,
operation_name='getUserWithFullName'
)
assert result.data['user']['fullName']