Added graphql-core min version to requirements

This commit is contained in:
Syrus Akbary 2018-06-05 01:24:53 -07:00
parent a480a39713
commit 33c6fdf5ab
3 changed files with 33 additions and 25 deletions

View File

@ -448,7 +448,11 @@ def test_handles_field_errors_caught_by_graphql(client):
assert response.status_code == 200 assert response.status_code == 200
assert response_json(response) == { assert response_json(response) == {
'data': None, 'data': None,
'errors': [{'locations': [{'column': 2, 'line': 1}], 'message': 'Throws!'}] 'errors': [{
'locations': [{'column': 2, 'line': 1}],
'path': ['thrower'],
'message': 'Throws!',
}]
} }
@ -457,7 +461,7 @@ def test_handles_syntax_errors_caught_by_graphql(client):
assert response.status_code == 400 assert response.status_code == 400
assert response_json(response) == { assert response_json(response) == {
'errors': [{'locations': [{'column': 1, 'line': 1}], 'errors': [{'locations': [{'column': 1, 'line': 1}],
'message': 'Syntax Error GraphQL request (1:1) ' 'message': 'Syntax Error GraphQL (1:1) '
'Unexpected Name "syntaxerror"\n\n1: syntaxerror\n ^\n'}] 'Unexpected Name "syntaxerror"\n\n1: syntaxerror\n ^\n'}]
} }

View File

@ -10,7 +10,7 @@ from django.utils.decorators import method_decorator
from django.views.generic import View from django.views.generic import View
from django.views.decorators.csrf import ensure_csrf_cookie from django.views.decorators.csrf import ensure_csrf_cookie
from graphql import Source, execute, parse, validate from graphql import get_default_backend
from graphql.error import format_error as format_graphql_error from graphql.error import format_error as format_graphql_error
from graphql.error import GraphQLError from graphql.error import GraphQLError
from graphql.execution import ExecutionResult from graphql.execution import ExecutionResult
@ -59,16 +59,20 @@ class GraphQLView(View):
schema = None schema = None
graphiql = False graphiql = False
executor = None executor = None
backend = None
middleware = None middleware = None
root_value = None root_value = None
pretty = False pretty = False
batch = False batch = False
def __init__(self, schema=None, executor=None, middleware=None, root_value=None, graphiql=False, pretty=False, def __init__(self, schema=None, executor=None, middleware=None, root_value=None, graphiql=False, pretty=False,
batch=False): batch=False, backend=None):
if not schema: if not schema:
schema = graphene_settings.SCHEMA schema = graphene_settings.SCHEMA
if backend is None:
backend = get_default_backend()
if middleware is None: if middleware is None:
middleware = graphene_settings.MIDDLEWARE middleware = graphene_settings.MIDDLEWARE
@ -80,6 +84,7 @@ class GraphQLView(View):
self.pretty = self.pretty or pretty self.pretty = self.pretty or pretty
self.graphiql = self.graphiql or graphiql self.graphiql = self.graphiql or graphiql
self.batch = self.batch or batch self.batch = self.batch or batch
self.backend = backend
assert isinstance( assert isinstance(
self.schema, GraphQLSchema), 'A Schema is required to be provided to GraphQLView.' self.schema, GraphQLSchema), 'A Schema is required to be provided to GraphQLView.'
@ -96,6 +101,9 @@ class GraphQLView(View):
def get_context(self, request): def get_context(self, request):
return request return request
def get_backend(self, request):
return self.backend
@method_decorator(ensure_csrf_cookie) @method_decorator(ensure_csrf_cookie)
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
try: try:
@ -225,9 +233,6 @@ class GraphQLView(View):
return {} return {}
def execute(self, *args, **kwargs):
return execute(self.schema, *args, **kwargs)
def execute_graphql_request(self, request, data, query, variables, operation_name, show_graphiql=False): def execute_graphql_request(self, request, data, query, variables, operation_name, show_graphiql=False):
if not query: if not query:
if show_graphiql: if show_graphiql:
@ -235,39 +240,37 @@ class GraphQLView(View):
raise HttpError(HttpResponseBadRequest( raise HttpError(HttpResponseBadRequest(
'Must provide query string.')) 'Must provide query string.'))
source = Source(query, name='GraphQL request')
try: try:
document_ast = parse(source) backend = self.get_backend(request)
validation_errors = validate(self.schema, document_ast) document = backend.document_from_string(self.schema, query)
if validation_errors:
return ExecutionResult(
errors=validation_errors,
invalid=True,
)
except Exception as e: except Exception as e:
return ExecutionResult(errors=[e], invalid=True) return ExecutionResult(errors=[e], invalid=True)
if request.method.lower() == 'get': if request.method.lower() == 'get':
operation_ast = get_operation_ast(document_ast, operation_name) operation_type = document.get_operation_type(operation_name)
if operation_ast and operation_ast.operation != 'query': if operation_type and operation_type != 'query':
if show_graphiql: if show_graphiql:
return None return None
raise HttpError(HttpResponseNotAllowed( raise HttpError(HttpResponseNotAllowed(
['POST'], 'Can only perform a {} operation from a POST request.'.format( ['POST'], 'Can only perform a {} operation from a POST request.'.format(
operation_ast.operation) operation_type)
)) ))
try: try:
return self.execute( extra_options = {}
document_ast, if self.executor:
root_value=self.get_root_value(request), # We only include it optionally since
variable_values=variables, # executor is not a valid argument in all backends
extra_options['executor'] = self.executor
return document.execute(
root=self.get_root_value(request),
variables=variables,
operation_name=operation_name, operation_name=operation_name,
context_value=self.get_context(request), context=self.get_context(request),
middleware=self.get_middleware(request), middleware=self.get_middleware(request),
executor=self.executor, **extra_options
) )
except Exception as e: except Exception as e:
return ExecutionResult(errors=[e], invalid=True) return ExecutionResult(errors=[e], invalid=True)

View File

@ -59,6 +59,7 @@ setup(
install_requires=[ install_requires=[
'six>=1.10.0', 'six>=1.10.0',
'graphene>=2.0.1,<3', 'graphene>=2.0.1,<3',
'graphql-core>=2.1rc0',
django_version, django_version,
'iso8601', 'iso8601',
'singledispatch>=3.4.0.3', 'singledispatch>=3.4.0.3',