mirror of
https://github.com/graphql-python/graphene.git
synced 2025-02-02 12:44:15 +03:00
Added ability to set up a custom graphql executor. Fixed #7. Added automatic field ordering as default
This commit is contained in:
parent
cf3a32b50a
commit
042325192a
|
@ -1,9 +1,18 @@
|
|||
from functools import wraps
|
||||
from collections import OrderedDict
|
||||
|
||||
from graphql.core import graphql
|
||||
from graphql.core.type import (
|
||||
GraphQLSchema as _GraphQLSchema
|
||||
)
|
||||
|
||||
from graphql.core.execution.executor import Executor
|
||||
from graphql.core.execution.middlewares.sync import SynchronousExecutionMiddleware
|
||||
from graphql.core.execution import ExecutionResult, execute
|
||||
from graphql.core.language.parser import parse
|
||||
from graphql.core.language.source import Source
|
||||
from graphql.core.validation import validate
|
||||
|
||||
from graphql.core.utils.introspection_query import introspection_query
|
||||
from graphene import signals
|
||||
from graphene.utils import cached_property
|
||||
|
@ -17,12 +26,14 @@ class GraphQLSchema(_GraphQLSchema):
|
|||
|
||||
class Schema(object):
|
||||
_query = None
|
||||
_executor = None
|
||||
|
||||
def __init__(self, query=None, mutation=None, name='Schema'):
|
||||
def __init__(self, query=None, mutation=None, name='Schema', executor=None):
|
||||
self._internal_types = {}
|
||||
self.mutation = mutation
|
||||
self.query = query
|
||||
self.name = name
|
||||
self.executor = executor
|
||||
signals.init_schema.send(self)
|
||||
|
||||
def __repr__(self):
|
||||
|
@ -37,6 +48,16 @@ class Schema(object):
|
|||
self._query = query
|
||||
self._query_type = query and query.internal_type(self)
|
||||
|
||||
@property
|
||||
def executor(self):
|
||||
if not self._executor:
|
||||
self.executor = Executor([SynchronousExecutionMiddleware()], map_type=OrderedDict)
|
||||
return self._executor
|
||||
|
||||
@executor.setter
|
||||
def executor(self, value):
|
||||
self._executor = value
|
||||
|
||||
@cached_property
|
||||
def schema(self):
|
||||
if not self._query_type:
|
||||
|
@ -63,12 +84,22 @@ class Schema(object):
|
|||
|
||||
def execute(self, request='', root=None, vars=None, operation_name=None):
|
||||
root = root or object()
|
||||
return graphql(
|
||||
source = Source(request, 'GraphQL request')
|
||||
ast = parse(source)
|
||||
validation_errors = validate(self.schema, ast)
|
||||
if validation_errors:
|
||||
return ExecutionResult(
|
||||
errors=validation_errors,
|
||||
invalid=True,
|
||||
)
|
||||
|
||||
return self.executor.execute(
|
||||
self.schema,
|
||||
request=request,
|
||||
ast,
|
||||
root=self.query(root),
|
||||
vars=vars,
|
||||
operation_name=operation_name
|
||||
args=vars,
|
||||
operation_name=operation_name,
|
||||
validate_ast=False
|
||||
)
|
||||
|
||||
def introspect(self):
|
||||
|
|
Loading…
Reference in New Issue
Block a user