mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-25 19:13:57 +03:00
7dd8305bdf
Changed in https://github.com/graphql-python/graphql-core/pull/185 , the docs here were out of date, as were the tests.
60 lines
1.2 KiB
ReStructuredText
60 lines
1.2 KiB
ReStructuredText
Executing a query
|
|
=================
|
|
|
|
|
|
For executing a query a schema, you can directly call the ``execute`` method on it.
|
|
|
|
|
|
.. code:: python
|
|
|
|
schema = graphene.Schema(...)
|
|
result = schema.execute('{ name }')
|
|
|
|
``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.
|
|
|
|
|
|
Context
|
|
_______
|
|
|
|
You can pass context to a query via ``context``.
|
|
|
|
|
|
.. code:: python
|
|
|
|
class Query(graphene.ObjectType):
|
|
name = graphene.String()
|
|
|
|
def resolve_name(self, info):
|
|
return info.context.get('name')
|
|
|
|
schema = graphene.Schema(Query)
|
|
result = schema.execute('{ name }', context={'name': 'Syrus'})
|
|
|
|
|
|
|
|
Variables
|
|
_______
|
|
|
|
You can pass variables to a query via ``variables``.
|
|
|
|
|
|
.. code:: python
|
|
|
|
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},
|
|
)
|