| 1 |
from graphql.execution import execute, ExecutionContext |
| 2 |
from graphql.language import parse |
| 3 |
from graphql.type import GraphQLSchema, GraphQLObjectType, GraphQLString, GraphQLField |
| 4 |
|
| 5 |
|
| 6 |
def describe_customize_execution(): |
| 7 |
def uses_a_custom_field_resolver(): |
| 8 |
query = parse("{ foo }") |
| 9 |
|
| 10 |
schema = GraphQLSchema( |
| 11 |
GraphQLObjectType("Query", {"foo": GraphQLField(GraphQLString)}) |
| 12 |
) |
| 13 |
|
| 14 |
# For the purposes of test, just return the name of the field! |
| 15 |
def custom_resolver(_source, info, **_args): |
| 16 |
return info.field_name |
| 17 |
|
| 18 |
assert execute(schema, query, field_resolver=custom_resolver) == ( |
| 19 |
{"foo": "foo"}, |
| 20 |
None, |
| 21 |
) |
| 22 |
|
| 23 |
def uses_a_custom_execution_context_class(): |
| 24 |
query = parse("{ foo }") |
| 25 |
|
| 26 |
schema = GraphQLSchema( |
| 27 |
GraphQLObjectType( |
| 28 |
"Query", |
| 29 |
{"foo": GraphQLField(GraphQLString, resolve=lambda *_args: "bar")}, |
| 30 |
) |
| 31 |
) |
| 32 |
|
| 33 |
class TestExecutionContext(ExecutionContext): |
| 34 |
def execute_field(self, parent_type, source, field_nodes, path): |
| 35 |
result = super().execute_field(parent_type, source, field_nodes, path) |
| 36 |
return result * 2 # type: ignore |
| 37 |
|
| 38 |
assert execute(schema, query, execution_context_class=TestExecutionContext) == ( |
| 39 |
{"foo": "barbar"}, |
| 40 |
None, |
| 41 |
) |