|
1 | 1 | from inspect import isawaitable
|
2 | 2 | from typing import (
|
3 | 3 | Any, Awaitable, Dict, Iterable, List, NamedTuple, Optional, Set, Union,
|
4 |
| - Tuple, cast) |
| 4 | + Tuple, Type, cast) |
5 | 5 |
|
6 | 6 | from ..error import GraphQLError, INVALID, located_error
|
7 | 7 | from ..language import (
|
@@ -60,48 +60,6 @@ class ExecutionResult(NamedTuple):
|
60 | 60 | ExecutionResult.__new__.__defaults__ = (None, None) # type: ignore
|
61 | 61 |
|
62 | 62 |
|
63 |
| -def execute( |
64 |
| - schema: GraphQLSchema, document: DocumentNode, |
65 |
| - root_value: Any=None, context_value: Any=None, |
66 |
| - variable_values: Dict[str, Any]=None, |
67 |
| - operation_name: str=None, field_resolver: GraphQLFieldResolver=None |
68 |
| - ) -> MaybeAwaitable[ExecutionResult]: |
69 |
| - """Execute a GraphQL operation. |
70 |
| -
|
71 |
| - Implements the "Evaluating requests" section of the GraphQL specification. |
72 |
| -
|
73 |
| - Returns an ExecutionResult (if all encountered resolvers are synchronous), |
74 |
| - or a coroutine object eventually yielding an ExecutionResult. |
75 |
| -
|
76 |
| - If the arguments to this function do not result in a legal execution |
77 |
| - context, a GraphQLError will be thrown immediately explaining the invalid |
78 |
| - input. |
79 |
| - """ |
80 |
| - # If arguments are missing or incorrect, throw an error. |
81 |
| - assert_valid_execution_arguments(schema, document, variable_values) |
82 |
| - |
83 |
| - # If a valid execution context cannot be created due to incorrect |
84 |
| - # arguments, a "Response" with only errors is returned. |
85 |
| - exe_context = ExecutionContext.build( |
86 |
| - schema, document, root_value, context_value, |
87 |
| - variable_values, operation_name, field_resolver) |
88 |
| - |
89 |
| - # Return early errors if execution context failed. |
90 |
| - if isinstance(exe_context, list): |
91 |
| - return ExecutionResult(data=None, errors=exe_context) |
92 |
| - |
93 |
| - # Return a possible coroutine object that will eventually yield the data |
94 |
| - # described by the "Response" section of the GraphQL specification. |
95 |
| - # |
96 |
| - # If errors are encountered while executing a GraphQL field, only that |
97 |
| - # field and its descendants will be omitted, and sibling fields will still |
98 |
| - # be executed. An execution which encounters errors will still result in a |
99 |
| - # coroutine object that can be executed without errors. |
100 |
| - |
101 |
| - data = exe_context.execute_operation(exe_context.operation, root_value) |
102 |
| - return exe_context.build_response(data) |
103 |
| - |
104 |
| - |
105 | 63 | class ExecutionContext:
|
106 | 64 | """Data that must be available at all points during query execution.
|
107 | 65 |
|
@@ -794,6 +752,49 @@ def collect_subfields(
|
794 | 752 | return sub_field_nodes
|
795 | 753 |
|
796 | 754 |
|
| 755 | +def execute( |
| 756 | + schema: GraphQLSchema, document: DocumentNode, |
| 757 | + root_value: Any=None, context_value: Any=None, |
| 758 | + variable_values: Dict[str, Any]=None, |
| 759 | + operation_name: str=None, field_resolver: GraphQLFieldResolver=None, |
| 760 | + execution_context_class: Type[ExecutionContext]=ExecutionContext, |
| 761 | + ) -> MaybeAwaitable[ExecutionResult]: |
| 762 | + """Execute a GraphQL operation. |
| 763 | +
|
| 764 | + Implements the "Evaluating requests" section of the GraphQL specification. |
| 765 | +
|
| 766 | + Returns an ExecutionResult (if all encountered resolvers are synchronous), |
| 767 | + or a coroutine object eventually yielding an ExecutionResult. |
| 768 | +
|
| 769 | + If the arguments to this function do not result in a legal execution |
| 770 | + context, a GraphQLError will be thrown immediately explaining the invalid |
| 771 | + input. |
| 772 | + """ |
| 773 | + # If arguments are missing or incorrect, throw an error. |
| 774 | + assert_valid_execution_arguments(schema, document, variable_values) |
| 775 | + |
| 776 | + # If a valid execution context cannot be created due to incorrect |
| 777 | + # arguments, a "Response" with only errors is returned. |
| 778 | + exe_context = execution_context_class.build( |
| 779 | + schema, document, root_value, context_value, |
| 780 | + variable_values, operation_name, field_resolver) |
| 781 | + |
| 782 | + # Return early errors if execution context failed. |
| 783 | + if isinstance(exe_context, list): |
| 784 | + return ExecutionResult(data=None, errors=exe_context) |
| 785 | + |
| 786 | + # Return a possible coroutine object that will eventually yield the data |
| 787 | + # described by the "Response" section of the GraphQL specification. |
| 788 | + # |
| 789 | + # If errors are encountered while executing a GraphQL field, only that |
| 790 | + # field and its descendants will be omitted, and sibling fields will still |
| 791 | + # be executed. An execution which encounters errors will still result in a |
| 792 | + # coroutine object that can be executed without errors. |
| 793 | + |
| 794 | + data = exe_context.execute_operation(exe_context.operation, root_value) |
| 795 | + return exe_context.build_response(data) |
| 796 | + |
| 797 | + |
797 | 798 | def assert_valid_execution_arguments(
|
798 | 799 | schema: GraphQLSchema, document: DocumentNode,
|
799 | 800 | raw_variable_values: Dict[str, Any]=None) -> None:
|
|
0 commit comments