Skip to content

Commit 0ed9ee6

Browse files
committed
Added support for dynamic backends. Update version to 2.0rc0
1 parent ab5831f commit 0ed9ee6

File tree

6 files changed

+43
-14
lines changed

6 files changed

+43
-14
lines changed

flask_graphql/graphqlview.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class GraphQLView(View):
1717
root_value = None
1818
pretty = False
1919
graphiql = False
20+
backend = None
2021
graphiql_version = None
2122
graphiql_template = None
2223
graphiql_html_title = None
@@ -43,6 +44,9 @@ def get_context(self):
4344
def get_middleware(self):
4445
return self.middleware
4546

47+
def get_backend(self):
48+
return self.backend
49+
4650
def get_executor(self):
4751
return self.executor
4852

@@ -68,19 +72,27 @@ def dispatch_request(self):
6872

6973
pretty = self.pretty or show_graphiql or request.args.get('pretty')
7074

75+
extra_options = {}
76+
executor = self.get_executor()
77+
if executor:
78+
# We only include it optionally since
79+
# executor is not a valid argument in all backends
80+
extra_options['executor'] = executor
81+
7182
execution_results, all_params = run_http_query(
7283
self.schema,
7384
request_method,
7485
data,
7586
query_data=request.args,
7687
batch_enabled=self.batch,
7788
catch=catch,
89+
backend=self.get_backend(),
7890

7991
# Execute options
80-
root_value=self.get_root_value(),
81-
context_value=self.get_context(),
92+
root=self.get_root_value(),
93+
context=self.get_context(),
8294
middleware=self.get_middleware(),
83-
executor=self.get_executor(),
95+
**extra_options
8496
)
8597
result, status_code = encode_execution_results(
8698
execution_results,

setup.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
from setuptools import setup, find_packages
22

33
required_packages = [
4-
'graphql-core>=1.0',
4+
'graphql-core>=2.1rc1',
55
'flask>=0.7.0',
6-
'graphql-server-core>=1.0.dev'
6+
'graphql-server-core>=1.1rc0'
77
]
88

99
setup(
1010
name='Flask-GraphQL',
11-
version='1.4.1',
11+
version='2.0rc0',
1212
description='Adds GraphQL support to your Flask application',
1313
long_description=open('README.rst').read(),
1414
url='https://github.com/graphql-python/flask-graphql',

tests/app.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
from flask import Flask
22
from flask_graphql import GraphQLView
33
from .schema import Schema
4+
from graphql import GraphQLCachedBackend
5+
# from quiver.backend import GraphQLQuiverBackend
46

57

68
def create_app(path='/graphql', **kwargs):
9+
# backend = GraphQLCachedBackend(GraphQLQuiverBackend({"async_framework": "PROMISE"}))
10+
backend = None
711
app = Flask(__name__)
812
app.debug = True
9-
app.add_url_rule(path, view_func=GraphQLView.as_view('graphql', schema=Schema, **kwargs))
13+
app.add_url_rule(path, view_func=GraphQLView.as_view('graphql', schema=Schema, backend=backend, **kwargs))
1014
return app
1115

1216

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: info.context.args.get('q')),
1616
'context': GraphQLField(GraphQLNonNull(GraphQLString),
17-
resolver=lambda obj, args, context, info: context),
17+
resolver=lambda obj, info: 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
)

tests/test_graphqlview.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,19 @@ def test_allows_post_with_url_encoding(client):
194194
}
195195

196196

197+
# def test_benchmark(client, benchmark):
198+
# url = url_string()
199+
# data = urlencode(dict(query='{test}'))
200+
# def fun():
201+
# return client.post(url_string(), data=data, content_type='application/x-www-form-urlencoded')
202+
203+
# response = benchmark(fun)
204+
# assert response.status_code == 200
205+
# assert response_json(response) == {
206+
# 'data': {'test': "Hello World"}
207+
# }
208+
209+
197210
def test_supports_post_json_query_with_string_variables(client):
198211
response = client.post(url_string(), data=j(
199212
query='query helloWho($who: String){ test(who: $who) }',
@@ -353,7 +366,7 @@ def test_handles_field_errors_caught_by_graphql(client):
353366
assert response.status_code == 200
354367
assert response_json(response) == {
355368
'data': None,
356-
'errors': [{'locations': [{'column': 2, 'line': 1}], 'message': 'Throws!'}]
369+
'errors': [{'locations': [{'column': 2, 'line': 1}], 'path': ['thrower'], 'message': 'Throws!'}]
357370
}
358371

359372

@@ -362,7 +375,7 @@ def test_handles_syntax_errors_caught_by_graphql(client):
362375
assert response.status_code == 400
363376
assert response_json(response) == {
364377
'errors': [{'locations': [{'column': 1, 'line': 1}],
365-
'message': 'Syntax Error GraphQL request (1:1) '
378+
'message': 'Syntax Error GraphQL (1:1) '
366379
'Unexpected Name "syntaxerror"\n\n1: syntaxerror\n ^\n'}]
367380
}
368381

tox.ini

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ setenv =
88
deps =
99
pytest>=2.7.2
1010
pytest-flask>=0.10.0
11-
graphql-core>=1.0
11+
graphql-core>=2.1rc1
1212
graphql-server-core>=1.0.dev
1313
Flask>=0.10.0
1414
pytest-cov
@@ -25,7 +25,7 @@ commands =
2525
basepython=python3.5
2626
deps =
2727
isort
28-
graphql-core>=1.0
28+
graphql-core>=2.1rc1
2929
Flask>=0.10.0
3030
commands =
3131
isort --check-only flask_graphql/ -rc

0 commit comments

Comments
 (0)