Skip to content

Commit d60b180

Browse files
committed
Improved query execution using pluggable backend
1 parent 9486fef commit d60b180

File tree

5 files changed

+45
-44
lines changed

5 files changed

+45
-44
lines changed

Diff for: .travis.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ matrix:
66
env: TOX_ENV=pypy
77
- python: '2.7'
88
env: TOX_ENV=py27
9-
- python: '3.3'
10-
env: TOX_ENV=py33
119
- python: '3.4'
1210
env: TOX_ENV=py34
1311
- python: '3.5'
14-
env: TOX_ENV=py35,import-order,flake8
12+
env: TOX_ENV=py35
13+
- python: '3.6'
14+
env: TOX_ENV=py36,import-order,flake8
1515
cache:
1616
directories:
1717
- $HOME/.cache/pip

Diff for: graphql_server/__init__.py

+29-31
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
from collections import namedtuple, MutableMapping
33

44
import six
5-
from graphql import Source, execute, parse, validate
6-
from graphql.error import format_error as format_graphql_error
7-
from graphql.error import GraphQLError
5+
from graphql import get_default_backend
6+
from graphql.error import format_error as default_format_error
87
from graphql.execution import ExecutionResult
9-
from graphql.utils.get_operation_ast import get_operation_ast
108

119
from .error import HttpQueryError
1210

@@ -19,13 +17,6 @@ class SkipException(Exception):
1917
GraphQLResponse = namedtuple('GraphQLResponse', 'result,status_code')
2018

2119

22-
def default_format_error(error):
23-
if isinstance(error, GraphQLError):
24-
return format_graphql_error(error)
25-
26-
return {'message': six.text_type(error)}
27-
28-
2920
def run_http_query(schema, request_method, data, query_data=None, batch_enabled=False, catch=False, **execute_options):
3021
if request_method not in ('get', 'post'):
3122
raise HttpQueryError(
@@ -110,15 +101,15 @@ def load_json_variables(variables):
110101
if variables and isinstance(variables, six.string_types):
111102
try:
112103
return json.loads(variables)
113-
except:
104+
except Exception:
114105
raise HttpQueryError(400, 'Variables are invalid JSON.')
115106
return variables
116107

117108

118109
def get_graphql_params(data, query_data):
119110
query = data.get('query') or query_data.get('query')
120111
variables = data.get('variables') or query_data.get('variables')
121-
# id = data.get('id')
112+
# document_id = data.get('documentId')
122113
operation_name = data.get('operationName') or query_data.get('operationName')
123114

124115
return GraphQLParams(query, load_json_variables(variables), operation_name)
@@ -159,51 +150,58 @@ def format_execution_result(execution_result, format_error):
159150
return GraphQLResponse(response, status_code)
160151

161152

162-
def execute_graphql_request(schema, params, allow_only_query=False, **kwargs):
153+
def execute_graphql_request(schema, params, allow_only_query=False, backend=None, **kwargs):
163154
if not params.query:
164155
raise HttpQueryError(400, 'Must provide query string.')
165156

166157
try:
167-
source = Source(params.query, name='GraphQL request')
168-
ast = parse(source)
169-
validation_errors = validate(schema, ast)
170-
if validation_errors:
171-
return ExecutionResult(
172-
errors=validation_errors,
173-
invalid=True,
174-
)
158+
if not backend:
159+
backend = get_default_backend()
160+
document = backend.document_from_string(schema, params.query)
175161
except Exception as e:
176162
return ExecutionResult(errors=[e], invalid=True)
177163

178164
if allow_only_query:
179-
operation_ast = get_operation_ast(ast, params.operation_name)
180-
if operation_ast and operation_ast.operation != 'query':
165+
operation_type = document.get_operation_type(params.operation_name)
166+
if operation_type and operation_type != 'query':
181167
raise HttpQueryError(
182168
405,
183-
'Can only perform a {} operation from a POST request.'.format(operation_ast.operation),
169+
'Can only perform a {} operation from a POST request.'.format(operation_type),
184170
headers={
185171
'Allow': 'POST',
186172
}
187173
)
188174

189175
try:
190-
return execute(
191-
schema,
192-
ast,
176+
return document.execute(
193177
operation_name=params.operation_name,
194-
variable_values=params.variables,
178+
variables=params.variables,
195179
**kwargs
196180
)
197-
198181
except Exception as e:
199182
return ExecutionResult(errors=[e], invalid=True)
200183

201184

202185
def load_json_body(data):
203186
try:
204187
return json.loads(data)
205-
except:
188+
except Exception:
206189
raise HttpQueryError(
207190
400,
208191
'POST body sent invalid JSON.'
209192
)
193+
194+
195+
__all__ = [
196+
'default_format_error',
197+
'SkipException',
198+
'run_http_query',
199+
'encode_execution_results',
200+
'json_encode',
201+
'load_json_variables',
202+
'get_graphql_params',
203+
'get_response',
204+
'format_execution_result',
205+
'execute_graphql_request',
206+
'load_json_body'
207+
]

Diff for: setup.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
from setuptools import setup, find_packages
22

3-
required_packages = ['graphql-core', 'promise']
3+
required_packages = [
4+
'graphql-core>=2.1rc1',
5+
'promise'
6+
]
47

58
setup(
69
name='graphql-server-core',
7-
version='1.0.dev20170322001',
10+
version='1.1rc0',
811
description='GraphQL Server tools for powering your server',
912
long_description=open('README.rst').read(),
1013
url='https://github.com/graphql-python/graphql-server-core',

Diff for: tests/schema.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ def resolve_raises(*_):
1212
fields={
1313
'thrower': GraphQLField(GraphQLNonNull(GraphQLString), resolver=resolve_raises),
1414
'request': GraphQLField(GraphQLNonNull(GraphQLString),
15-
resolver=lambda obj, args, context, info: context.args.get('q')),
15+
resolver=lambda obj, info: context.args.get('q')),
1616
'context': GraphQLField(GraphQLNonNull(GraphQLString),
17-
resolver=lambda obj, args, context, info: context),
17+
resolver=lambda obj, info: context),
1818
'test': GraphQLField(
1919
type=GraphQLString,
2020
args={
2121
'who': GraphQLArgument(GraphQLString)
2222
},
23-
resolver=lambda obj, args, context, info: 'Hello %s' % (args.get('who') or 'World')
23+
resolver=lambda obj, info, who='World': 'Hello %s' % who
2424
)
2525
}
2626
)

Diff for: tox.ini

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@ setenv =
77
PYTHONPATH = {toxinidir}
88
deps =
99
pytest>=2.7.2
10-
graphql-core>=1.0
10+
graphql-core>=2.1rc1
1111
pytest-cov
1212
commands =
13-
py{py,27,33,34,35}: py.test tests {posargs}
13+
py{py,27,34,35,36}: py.test tests {posargs}
1414

1515
[testenv:flake8]
16-
basepython=python3.5
16+
basepython=python3.6
1717
deps = flake8
1818
commands =
1919
flake8 graphql_server
2020

2121
[testenv:import-order]
22-
basepython=python3.5
22+
basepython=python3.6
2323
deps =
2424
isort
25-
graphql-core>=1.0
25+
graphql-core>=2.1rc1
2626
commands =
2727
isort --check-only graphql_server/ -rc

0 commit comments

Comments
 (0)