<commit_before>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(root, 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, id=graphene.ID(required=True))

        def resolve_user(root, 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},
    )<commit_msg>Another docs test<commit_after>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(root, 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, id=graphene.ID(required=True))

        def resolve_user(root, 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},
    )
{'fraction_non_alphanumeric': 0.13225569434239529, 'fraction_numerical': 0.0014695077149155032, 'mean_word_length': 2.3575832305795315, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5255491', 'n_tokens_mistral': 816, 'n_tokens_neox': 747, 'n_words': 242}