Skip to content

Added excution_context_class for custom ExecutionContext #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions graphql/execution/execute.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from inspect import isawaitable
from typing import (
Any, Awaitable, Dict, Iterable, List, NamedTuple, Optional, Set, Union,
Tuple, cast)
Tuple, Type, cast)

from ..error import GraphQLError, INVALID, located_error
from ..language import (
Expand Down Expand Up @@ -69,6 +69,7 @@ def execute(
variable_values: Dict[str, Any]=None,
operation_name: str=None,
field_resolver: GraphQLFieldResolver=None,
execution_context_class: Type[ExecutionContext]=ExecutionContext,
middleware: Middleware=None
) -> MaybeAwaitable[ExecutionResult]:
"""Execute a GraphQL operation.
Expand All @@ -87,7 +88,7 @@ def execute(

# If a valid execution context cannot be created due to incorrect
# arguments, a "Response" with only errors is returned.
exe_context = ExecutionContext.build(
exe_context = execution_context_class.build(
schema, document, root_value, context_value,
variable_values, operation_name, field_resolver, middleware)

Expand Down
76 changes: 45 additions & 31 deletions graphql/graphql.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
from asyncio import ensure_future
from inspect import isawaitable
from typing import Any, Awaitable, Callable, Dict, Union, cast
from typing import Any, Awaitable, Callable, Dict, Union, Type, cast

from .error import GraphQLError
from .execution import execute, ExecutionResult, Middleware
from .language import parse, Source
from .pyutils import MaybeAwaitable
from .type import GraphQLSchema, validate_schema
from .execution.execute import ExecutionResult, ExecutionContext

__all__ = ['graphql', 'graphql_sync']
__all__ = ["graphql", "graphql_sync"]


async def graphql(
schema: GraphQLSchema,
source: Union[str, Source],
root_value: Any=None,
context_value: Any=None,
variable_values: Dict[str, Any]=None,
operation_name: str=None,
field_resolver: Callable=None,
middleware: Middleware=None
) -> ExecutionResult:
schema: GraphQLSchema,
source: Union[str, Source],
root_value: Any=None,
context_value: Any=None,
variable_values: Dict[str, Any]=None,
operation_name: str=None,
field_resolver: Callable=None,
middleware: Middleware=None,
execution_context_class: Type[ExecutionContext] = ExecutionContext,
) -> ExecutionResult:
"""Execute a GraphQL operation asynchronously.

This is the primary entry point function for fulfilling GraphQL operations
Expand Down Expand Up @@ -59,6 +61,8 @@ async def graphql(
a value or method on the source value with the field's name).
:arg middleware:
The middleware to wrap the resolvers with
:arg execution_context_class:
The execution context class to use to build the context
"""
# Always return asynchronously for a consistent API.
result = graphql_impl(
Expand All @@ -69,7 +73,9 @@ async def graphql(
variable_values,
operation_name,
field_resolver,
middleware)
middleware,
execution_context_class,
)

if isawaitable(result):
return await cast(Awaitable[ExecutionResult], result)
Expand All @@ -78,15 +84,16 @@ async def graphql(


def graphql_sync(
schema: GraphQLSchema,
source: Union[str, Source],
root_value: Any=None,
context_value: Any=None,
variable_values: Dict[str, Any]=None,
operation_name: str=None,
field_resolver: Callable=None,
middleware: Middleware=None
) -> ExecutionResult:
schema: GraphQLSchema,
source: Union[str, Source],
root_value: Any=None,
context_value: Any=None,
variable_values: Dict[str, Any]=None,
operation_name: str=None,
field_resolver: Callable=None,
middleware: Middleware=None,
execution_context_class: Type[ExecutionContext] = ExecutionContext,
) -> ExecutionResult:
"""Execute a GraphQL operation synchronously.

The graphql_sync function also fulfills GraphQL operations by parsing,
Expand All @@ -102,7 +109,9 @@ def graphql_sync(
variable_values,
operation_name,
field_resolver,
middleware)
middleware,
execution_context_class,
)

# Assert that the execution was synchronous.
if isawaitable(result):
Expand All @@ -114,14 +123,16 @@ def graphql_sync(


def graphql_impl(
schema,
source,
root_value,
context_value,
variable_values,
operation_name,
field_resolver,
middleware) -> MaybeAwaitable[ExecutionResult]:
schema,
source,
root_value,
context_value,
variable_values,
operation_name,
field_resolver,
middleware,
execution_context_class,
) -> MaybeAwaitable[ExecutionResult]:
"""Execute a query, return asynchronously only if necessary."""
# Validate Schema
schema_validation_errors = validate_schema(schema)
Expand All @@ -139,6 +150,7 @@ def graphql_impl(

# Validate
from .validation import validate

validation_errors = validate(schema, document)
if validation_errors:
return ExecutionResult(data=None, errors=validation_errors)
Expand All @@ -152,4 +164,6 @@ def graphql_impl(
variable_values,
operation_name,
field_resolver,
middleware)
middleware,
execution_context_class,
)