def execute_sync( self, query: str, variable_values: Optional[Dict[str, Any]] = None, context_value: Optional[Any] = None, root_value: Optional[Any] = None, operation_name: Optional[str] = None, ) -> ExecutionResult: execution_context = ExecutionContext( query=query, schema=self, context=context_value, root_value=root_value, variables=variable_values, operation_name=operation_name, ) result = execute_sync( self._schema, query, extensions=self.extensions, directives=self.directives, execution_context_class=self.execution_context_class, execution_context=execution_context, ) if result.errors: self.process_errors(result.errors, execution_context=execution_context) return result
async def execute( self, query: str, variable_values: Optional[Dict[str, Any]] = None, context_value: Optional[Any] = None, root_value: Optional[Any] = None, operation_name: Optional[str] = None, ) -> ExecutionResult: # Create execution context execution_context = ExecutionContext( query=query, schema=self, context=context_value, root_value=root_value, variables=variable_values, provided_operation_name=operation_name, ) result = await execute( self._schema, query, extensions=list(self.extensions) + [DirectivesExtension], execution_context_class=self.execution_context_class, execution_context=execution_context, ) if result.errors: self.process_errors(result.errors, execution_context=execution_context) return result
def _run_validation(execution_context: ExecutionContext) -> None: # Check if there are any validation rules or if validation has # already been run by an extension if len(execution_context.validation_rules ) > 0 and execution_context.errors is None: assert execution_context.graphql_document execution_context.errors = validate_document( execution_context.schema._schema, execution_context.graphql_document, execution_context.validation_rules, )
async def get_execution_context(self, request: web.Request) -> ExecutionContext: data = await self.parse_body(request) variables = data.get("variables") operation_name = data.get("operationName") try: query = data["query"] except KeyError: raise web.HTTPBadRequest(reason="No GraphQL query found in the request") return ExecutionContext( query=query, variables=variables, operation_name=operation_name, )
def get_execution_context(self, request: Request) -> ExecutionContext: try: data = self.parse_body(request) except json.JSONDecodeError: raise ServerError("Unable to parse request body as JSON", status_code=400) try: query = data["query"] except KeyError: raise ServerError("No GraphQL query found in the request", status_code=400) variables = data.get("variables") operation_name = data.get("operationName") return ExecutionContext(query=query, variables=variables, operation_name=operation_name)
async def execute( self, query: str, variable_values: Optional[Dict[str, Any]] = None, context_value: Optional[Any] = None, root_value: Optional[Any] = None, operation_name: Optional[str] = None, validate_queries: bool = True, ) -> ExecutionResult: # Create execution context execution_context = ExecutionContext( query=query, context=context_value, variables=variable_values, operation_name=operation_name, ) result = await execute( self._schema, query, variable_values=variable_values, root_value=root_value, context_value=context_value, operation_name=operation_name, additional_middlewares=self.middleware, extensions=self.extensions, execution_context_class=self.execution_context_class, validate_queries=validate_queries, execution_context=execution_context, ) if result.errors: self.process_errors(result.errors, execution_context=execution_context) return ExecutionResult( data=result.data, errors=result.errors, extensions=result.extensions, )
def execute_sync( schema: GraphQLSchema, query: str, extensions: Sequence[Type[Extension]], root_value: Any = None, context_value: Any = None, variable_values: Dict[str, Any] = None, additional_middlewares: List[Any] = None, operation_name: str = None, execution_context_class: Optional[Type[GraphQLExecutionContext]] = None, ) -> ExecutionResult: execution_context = ExecutionContext( query=query, context=context_value, variables=variable_values, operation_name=operation_name, ) extensions_runner = ExtensionsRunner( execution_context=execution_context, extensions=[extension() for extension in extensions], ) additional_middlewares = additional_middlewares or [] with extensions_runner.request(): # Note: In graphql-core the schema would be validated here but in # Strawberry we are validating it at initialisation time instead try: with extensions_runner.parsing(): document = parse(query) except GraphQLError as error: return ExecutionResult( data=None, errors=[error], extensions=extensions_runner.get_extensions_results(), ) except Exception as error: # pragma: no cover error = GraphQLError(str(error), original_error=error) return ExecutionResult( data=None, errors=[error], extensions=extensions_runner.get_extensions_results(), ) with extensions_runner.validation(): validation_errors = validate(schema, document) if validation_errors: return ExecutionResult(data=None, errors=validation_errors) result = original_execute( schema, document, root_value=root_value, middleware=extensions_runner.as_middleware_manager(*additional_middlewares), variable_values=variable_values, operation_name=operation_name, context_value=context_value, execution_context_class=execution_context_class, ) if isawaitable(result): ensure_future(cast(Awaitable[GraphQLExecutionResult], result)).cancel() raise RuntimeError("GraphQL execution failed to complete synchronously.") result = cast(GraphQLExecutionResult, result) return ExecutionResult( data=result.data, errors=result.errors, extensions=extensions_runner.get_extensions_results(), )
async def execute( schema: GraphQLSchema, query: str, extensions: Sequence[Type[Extension]], root_value: Any = None, context_value: Any = None, variable_values: Dict[str, Any] = None, additional_middlewares: List[Any] = None, operation_name: str = None, ) -> ExecutionResult: execution_context = ExecutionContext( query=query, context=context_value, variables=variable_values, operation_name=operation_name, ) extensions_runner = ExtensionsRunner( execution_context=execution_context, extensions=[extension() for extension in extensions], ) additional_middlewares = additional_middlewares or [] with extensions_runner.request(): schema_validation_errors = validate_schema(schema) if schema_validation_errors: return ExecutionResult( data=None, errors=schema_validation_errors, extensions=extensions_runner.get_extensions_results(), ) try: with extensions_runner.parsing(): document = parse(query) except GraphQLError as error: return ExecutionResult( data=None, errors=[error], extensions=extensions_runner.get_extensions_results(), ) except Exception as error: # pragma: no cover error = GraphQLError(str(error), original_error=error) return ExecutionResult( data=None, errors=[error], extensions=extensions_runner.get_extensions_results(), ) with extensions_runner.validation(): validation_errors = validate(schema, document) if validation_errors: return ExecutionResult(data=None, errors=validation_errors) result = original_execute( schema, document, root_value=root_value, middleware=extensions_runner.as_middleware_manager( *additional_middlewares), variable_values=variable_values, operation_name=operation_name, context_value=context_value, ) if isawaitable(result): result = await cast(Awaitable[GraphQLExecutionResult], result) result = cast(GraphQLExecutionResult, result) return ExecutionResult( data=result.data, errors=result.errors, extensions=extensions_runner.get_extensions_results(), )
async def execute( schema: GraphQLSchema, query: str, extensions: Sequence[Union[Type[Extension], Extension]], directives: Sequence[Any], execution_context: ExecutionContext, execution_context_class: Optional[Type[GraphQLExecutionContext]] = None, ) -> ExecutionResult: extensions_runner = ExtensionsRunner( execution_context=execution_context, extensions=list(extensions), ) additional_middlewares = [DirectivesMiddleware(directives)] async with extensions_runner.request(): # Note: In graphql-core the schema would be validated here but in # Strawberry we are validating it at initialisation time instead try: async with extensions_runner.parsing(): if not execution_context.graphql_document: execution_context.graphql_document = parse_document(query) except GraphQLError as error: execution_context.errors = [error] return ExecutionResult( data=None, errors=[error], extensions=await extensions_runner.get_extensions_results(), ) except Exception as error: # pragma: no cover error = GraphQLError(str(error), original_error=error) execution_context.errors = [error] return ExecutionResult( data=None, errors=[error], extensions=await extensions_runner.get_extensions_results(), ) async with extensions_runner.validation(): _run_validation(execution_context) if execution_context.errors: return ExecutionResult(data=None, errors=execution_context.errors) result = original_execute( schema, execution_context.graphql_document, root_value=execution_context.root_value, middleware=extensions_runner.as_middleware_manager( *additional_middlewares), variable_values=execution_context.variables, operation_name=execution_context.operation_name, context_value=execution_context.context, execution_context_class=execution_context_class, ) if isawaitable(result): result = await cast(Awaitable[GraphQLExecutionResult], result) execution_context.result = cast(GraphQLExecutionResult, result) result = cast(GraphQLExecutionResult, result) return ExecutionResult( data=result.data, errors=result.errors, extensions=await extensions_runner.get_extensions_results(), )
def execute_sync( schema: GraphQLSchema, query: str, extensions: Sequence[Union[Type[Extension], Extension]], execution_context: ExecutionContext, execution_context_class: Optional[Type[GraphQLExecutionContext]] = None, ) -> ExecutionResult: extensions_runner = ExtensionsRunner( execution_context=execution_context, extensions=list(extensions), ) with extensions_runner.request(): # Note: In graphql-core the schema would be validated here but in # Strawberry we are validating it at initialisation time instead with extensions_runner.parsing(): try: if not execution_context.graphql_document: execution_context.graphql_document = parse_document(query) except GraphQLError as error: execution_context.errors = [error] return ExecutionResult( data=None, errors=[error], extensions=extensions_runner.get_extensions_results_sync(), ) except Exception as error: # pragma: no cover error = GraphQLError(str(error), original_error=error) execution_context.errors = [error] return ExecutionResult( data=None, errors=[error], extensions=extensions_runner.get_extensions_results_sync(), ) with extensions_runner.validation(): _run_validation(execution_context) if execution_context.errors: return ExecutionResult(data=None, errors=execution_context.errors) with extensions_runner.executing(): if not execution_context.result: result = original_execute( schema, execution_context.graphql_document, root_value=execution_context.root_value, middleware=extensions_runner.as_middleware_manager(), variable_values=execution_context.variables, operation_name=execution_context.operation_name, context_value=execution_context.context, execution_context_class=execution_context_class, ) if isawaitable(result): result = cast(Awaitable[GraphQLExecutionResult], result) ensure_future(result).cancel() raise RuntimeError( "GraphQL execution failed to complete synchronously.") result = cast(GraphQLExecutionResult, result) execution_context.result = result # Also set errors on the execution_context so that it's easier # to access in extensions if result.errors: execution_context.errors = result.errors return ExecutionResult( data=execution_context.result.data, errors=execution_context.result.errors, extensions=extensions_runner.get_extensions_results_sync(), )