Skip to content

Commit 2a2c4fc

Browse files
committed
Improved static file handling and added default_query option in GraphiQL
1 parent 31a0a1d commit 2a2c4fc

File tree

10 files changed

+31
-19
lines changed

10 files changed

+31
-19
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ A `Flask` package that provides two main views for operate with `GraphQL`:
1010
Use it like you would any other Flask View.
1111

1212
```python
13-
from flask_graphql import GraphQLView, GraphiQLView
13+
from graphql_flask import GraphQLView, GraphiQLView
1414

1515
app.add_url_rule('/graphql', view_func=GraphQLView.as_view('graphql', schema=schema))
1616
```

graphql_flask/blueprint.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ def __init__(self, app, schema, **options):
88
self.app = app
99
self.blueprint = Blueprint('graphql', __name__,
1010
template_folder='templates',
11-
static_folder='./static/')
11+
static_url_path='/static/graphql',
12+
static_folder='static/graphql/')
1213

14+
default_query = options.pop('default_query', None)
1315
app.add_url_rule('/graphql', view_func=GraphQLView.as_view('graphql', schema=schema, **options))
14-
app.add_url_rule('/graphiql', view_func=GraphiQLView.as_view('graphiql'))
16+
app.add_url_rule('/graphiql', view_func=GraphiQLView.as_view('graphiql', default_query=default_query))
1517

1618
self.app.register_blueprint(self.blueprint)

graphql_flask/graphiqlview.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44

55
class GraphiQLView(View):
6-
template_name = 'graphiql/index.html'
6+
template_name = 'graphiql.html'
7+
default_query = None
78
methods = ['GET']
89

910
def __init__(self, **kwargs):
@@ -13,4 +14,4 @@ def __init__(self, **kwargs):
1314
setattr(self, key, value)
1415

1516
def dispatch_request(self):
16-
return render_template(self.template_name)
17+
return render_template(self.template_name, default_query=self.default_query)

graphql_flask/graphqlview.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def __init__(self, response, message=None, *args, **kwargs):
2020
class GraphQLView(View):
2121
schema = None
2222
executor = None
23+
execute = None
2324
root_value = None
2425
pretty = False
2526

@@ -31,9 +32,16 @@ def __init__(self, **kwargs):
3132
if hasattr(self, key):
3233
setattr(self, key, value)
3334

34-
if not self.executor:
35+
inner_schema = getattr(self.schema, 'schema', None)
36+
execute = getattr(self.schema, 'execute', None)
37+
if execute:
38+
self.execute = execute
39+
elif not self.executor:
3540
self.executor = get_default_executor()
3641

42+
if inner_schema:
43+
self.schema = inner_schema
44+
3745
assert isinstance(self.schema, GraphQLSchema), 'A Schema is required to be provided to GraphQLView.'
3846

3947
# noinspection PyUnusedLocal
@@ -103,7 +111,9 @@ def parse_body(self, request):
103111

104112
return {}
105113

106-
def execute(self, *args, **kwargs):
114+
def _execute(self, *args, **kwargs):
115+
if self.execute:
116+
return self.execute(*args, **kwargs)
107117
return self.executor.execute(self.schema, *args, **kwargs)
108118

109119
def execute_graphql_request(self, request):
@@ -127,7 +137,7 @@ def execute_graphql_request(self, request):
127137
))
128138

129139
try:
130-
return self.execute(
140+
return self._execute(
131141
document_ast,
132142
self.get_root_value(request),
133143
variables,

graphql_flask/templates/graphiql/index.html renamed to graphql_flask/templates/graphiql.html

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<link rel="stylesheet" href="{{ url_for("static", filename="graphiql/graphiql.css") }}" />
5-
<script src="{{ url_for("static", filename="graphiql/react.min.js") }}"></script>
6-
<script src="{{ url_for("static", filename="graphiql/fetch.min.js") }}"></script>
7-
<script src="{{ url_for("static", filename="graphiql/graphiql.min.js") }}"></script>
4+
<link rel="stylesheet" href="{{ url_for("graphql.static", filename="graphiql.css") }}" />
5+
<script src="{{ url_for("graphql.static", filename="react.min.js") }}"></script>
6+
<script src="{{ url_for("graphql.static", filename="fetch.min.js") }}"></script>
7+
<script src="{{ url_for("graphql.static", filename="graphiql.min.js") }}"></script>
88
</head>
99
<body>
1010
Loading...
@@ -81,9 +81,9 @@
8181
variables: parameters.variables,
8282
onEditQuery: onEditQuery,
8383
onEditVariables: onEditVariables,
84-
{% if default_query %}
85-
defaultQuery: '{{ default_query }}',
86-
{% endif %}
84+
{%- if default_query %}
85+
defaultQuery: {{ default_query|tojson }},
86+
{%- endif %}
8787
}),
8888
document.body
8989
);

tests/test_graphiqlview.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ def test_graphiql_endpoint_exists(client):
1212
response = client.get(url_for('graphiql'))
1313
assert response.status_code == 200
1414

15-
# def test_graphiql_static_files_exposed(client):
16-
# print url_for("static", filename="graphiql/graphiql.min.js")
17-
# response = client.get(url_for("static", filename="graphiql/graphiql.min.js"))
18-
# assert response.status_code == 200
15+
def test_graphiql_static_files_exposed(client):
16+
response = client.get(url_for("graphql.static", filename="graphiql.min.js"))
17+
assert response.status_code == 200

0 commit comments

Comments
 (0)