| 1 |
from asyncio import ensure_future |
| 2 |
from inspect import isawaitable |
| 3 |
from typing import Any, Callable, Dict, Optional, Type, Union |
| 4 |
|
| 5 |
from .error import GraphQLError |
| 6 |
from .execution import ExecutionContext, ExecutionResult, Middleware, execute |
| 7 |
from .language import Source, parse |
| 8 |
from .pyutils import AwaitableOrValue |
| 9 |
from .type import ( |
| 10 |
GraphQLFieldResolver, |
| 11 |
GraphQLSchema, |
| 12 |
GraphQLTypeResolver, |
| 13 |
validate_schema, |
| 14 |
) |
| 15 |
|
| 16 |
__all__ = ["graphql", "graphql_sync"] |
| 17 |
|
| 18 |
|
| 19 |
async def graphql( |
| 20 |
schema: GraphQLSchema, |
| 21 |
source: Union[str, Source], |
| 22 |
root_value: Any = None, |
| 23 |
context_value: Any = None, |
| 24 |
variable_values: Optional[Dict[str, Any]] = None, |
| 25 |
operation_name: Optional[str] = None, |
| 26 |
field_resolver: Optional[GraphQLFieldResolver] = None, |
| 27 |
type_resolver: Optional[GraphQLTypeResolver] = None, |
| 28 |
middleware: Optional[Middleware] = None, |
| 29 |
execution_context_class: Optional[Type[ExecutionContext]] = None, |
| 30 |
is_awaitable: Optional[Callable[[Any], bool]] = None, |
| 31 |
) -> ExecutionResult: |
| 32 |
"""Execute a GraphQL operation asynchronously. |
| 33 |
|
| 34 |
This is the primary entry point function for fulfilling GraphQL operations by |
| 35 |
parsing, validating, and executing a GraphQL document along side a GraphQL schema. |
| 36 |
|
| 37 |
More sophisticated GraphQL servers, such as those which persist queries, may wish |
| 38 |
to separate the validation and execution phases to a static time tooling step, |
| 39 |
and a server runtime step. |
| 40 |
|
| 41 |
Accepts the following arguments: |
| 42 |
|
| 43 |
:arg schema: |
| 44 |
The GraphQL type system to use when validating and executing a query. |
| 45 |
:arg source: |
| 46 |
A GraphQL language formatted string representing the requested operation. |
| 47 |
:arg root_value: |
| 48 |
The value provided as the first argument to resolver functions on the top level |
| 49 |
type (e.g. the query object type). |
| 50 |
:arg context_value: |
| 51 |
The context value is provided as an attribute of the second argument |
| 52 |
(the resolve info) to resolver functions. It is used to pass shared information |
| 53 |
useful at any point during query execution, for example the currently logged in |
| 54 |
user and connections to databases or other services. |
| 55 |
:arg variable_values: |
| 56 |
A mapping of variable name to runtime value to use for all variables defined |
| 57 |
in the request string. |
| 58 |
:arg operation_name: |
| 59 |
The name of the operation to use if request string contains multiple possible |
| 60 |
operations. Can be omitted if request string contains only one operation. |
| 61 |
:arg field_resolver: |
| 62 |
A resolver function to use when one is not provided by the schema. |
| 63 |
If not provided, the default field resolver is used (which looks for a value |
| 64 |
or method on the source value with the field's name). |
| 65 |
:arg type_resolver: |
| 66 |
A type resolver function to use when none is provided by the schema. |
| 67 |
If not provided, the default type resolver is used (which looks for a |
| 68 |
``__typename`` field or alternatively calls the |
| 69 |
:meth:`~graphql.type.GraphQLObjectType.is_type_of` method). |
| 70 |
:arg middleware: |
| 71 |
The middleware to wrap the resolvers with |
| 72 |
:arg execution_context_class: |
| 73 |
The execution context class to use to build the context |
| 74 |
:arg is_awaitable: |
| 75 |
The predicate to be used for checking whether values are awaitable |
| 76 |
""" |
| 77 |
# Always return asynchronously for a consistent API. |
| 78 |
result = graphql_impl( |
| 79 |
schema, |
| 80 |
source, |
| 81 |
root_value, |
| 82 |
context_value, |
| 83 |
variable_values, |
| 84 |
operation_name, |
| 85 |
field_resolver, |
| 86 |
type_resolver, |
| 87 |
middleware, |
| 88 |
execution_context_class, |
| 89 |
is_awaitable, |
| 90 |
) |
| 91 |
|
| 92 |
if isawaitable(result): |
| 93 |
return await result |
| 94 |
|
| 95 |
return result |
| 96 |
|
| 97 |
|
| 98 |
def assume_not_awaitable(_value: Any) -> bool: |
| 99 |
"""Replacement for isawaitable if everything is assumed to be synchronous.""" |
| 100 |
return False |
| 101 |
|
| 102 |
|
| 103 |
def graphql_sync( |
| 104 |
schema: GraphQLSchema, |
| 105 |
source: Union[str, Source], |
| 106 |
root_value: Any = None, |
| 107 |
context_value: Any = None, |
| 108 |
variable_values: Optional[Dict[str, Any]] = None, |
| 109 |
operation_name: Optional[str] = None, |
| 110 |
field_resolver: Optional[GraphQLFieldResolver] = None, |
| 111 |
type_resolver: Optional[GraphQLTypeResolver] = None, |
| 112 |
middleware: Optional[Middleware] = None, |
| 113 |
execution_context_class: Optional[Type[ExecutionContext]] = None, |
| 114 |
check_sync: bool = False, |
| 115 |
) -> ExecutionResult: |
| 116 |
"""Execute a GraphQL operation synchronously. |
| 117 |
|
| 118 |
The graphql_sync function also fulfills GraphQL operations by parsing, validating, |
| 119 |
and executing a GraphQL document along side a GraphQL schema. However, it guarantees |
| 120 |
to complete synchronously (or throw an error) assuming that all field resolvers |
| 121 |
are also synchronous. |
| 122 |
|
| 123 |
Set check_sync to True to still run checks that no awaitable values are returned. |
| 124 |
""" |
| 125 |
is_awaitable = ( |
| 126 |
check_sync |
| 127 |
if callable(check_sync) |
| 128 |
else (None if check_sync else assume_not_awaitable) |
| 129 |
) |
| 130 |
result = graphql_impl( |
| 131 |
schema, |
| 132 |
source, |
| 133 |
root_value, |
| 134 |
context_value, |
| 135 |
variable_values, |
| 136 |
operation_name, |
| 137 |
field_resolver, |
| 138 |
type_resolver, |
| 139 |
middleware, |
| 140 |
execution_context_class, |
| 141 |
is_awaitable, |
| 142 |
) |
| 143 |
|
| 144 |
# Assert that the execution was synchronous. |
| 145 |
if isawaitable(result): |
| 146 |
ensure_future(result).cancel() |
| 147 |
raise RuntimeError("GraphQL execution failed to complete synchronously.") |
| 148 |
|
| 149 |
return result |
| 150 |
|
| 151 |
|
| 152 |
def graphql_impl( |
| 153 |
schema: GraphQLSchema, |
| 154 |
source: Union[str, Source], |
| 155 |
root_value: Any, |
| 156 |
context_value: Any, |
| 157 |
variable_values: Optional[Dict[str, Any]], |
| 158 |
operation_name: Optional[str], |
| 159 |
field_resolver: Optional[GraphQLFieldResolver], |
| 160 |
type_resolver: Optional[GraphQLTypeResolver], |
| 161 |
middleware: Optional[Middleware], |
| 162 |
execution_context_class: Optional[Type[ExecutionContext]], |
| 163 |
is_awaitable: Optional[Callable[[Any], bool]], |
| 164 |
) -> AwaitableOrValue[ExecutionResult]: |
| 165 |
"""Execute a query, return asynchronously only if necessary.""" |
| 166 |
# Validate Schema |
| 167 |
schema_validation_errors = validate_schema(schema) |
| 168 |
if schema_validation_errors: |
| 169 |
return ExecutionResult(data=None, errors=schema_validation_errors) |
| 170 |
|
| 171 |
# Parse |
| 172 |
try: |
| 173 |
document = parse(source) |
| 174 |
except GraphQLError as error: |
| 175 |
return ExecutionResult(data=None, errors=[error]) |
| 176 |
|
| 177 |
# Validate |
| 178 |
from .validation import validate |
| 179 |
|
| 180 |
validation_errors = validate(schema, document) |
| 181 |
if validation_errors: |
| 182 |
return ExecutionResult(data=None, errors=validation_errors) |
| 183 |
|
| 184 |
# Execute |
| 185 |
return execute( |
| 186 |
schema, |
| 187 |
document, |
| 188 |
root_value, |
| 189 |
context_value, |
| 190 |
variable_values, |
| 191 |
operation_name, |
| 192 |
field_resolver, |
| 193 |
type_resolver, |
| 194 |
None, |
| 195 |
50, |
| 196 |
middleware, |
| 197 |
execution_context_class, |
| 198 |
is_awaitable, |
| 199 |
) |