Skip to content
This repository was archived by the owner on Sep 10, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ wheels/
*.egg-info/
.installed.cfg
*.egg
.idea/

# PyInstaller
# Usually these files are written by a python script from a template
Expand Down
31 changes: 13 additions & 18 deletions flasql/graphiql.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import json
from flask import render_template_string, json

from flask import render_template_string

GRAPHIQL_VERSION = '0.7.1'
GRAPHIQL_VERSION = '0.11.5'

TEMPLATE = '''<!--
The request to this GraphQL server provided the header "Accept: text/html"
Expand All @@ -23,11 +21,14 @@
}
</style>
<meta name="referrer" content="no-referrer">
<link href="//cdn.jsdelivr.net/graphiql/{{graphiql_version}}/graphiql.css" rel="stylesheet" />

<script src="//cdn.jsdelivr.net/es6-promise/4.0.5/es6-promise.auto.min.js"></script>
<script src="//cdn.jsdelivr.net/fetch/0.9.0/fetch.min.js"></script>
<script src="//cdn.jsdelivr.net/react/15.0.0/react.min.js"></script>
<script src="//cdn.jsdelivr.net/react/15.0.0/react-dom.min.js"></script>
<script src="//cdn.jsdelivr.net/graphiql/{{graphiql_version}}/graphiql.min.js"></script>
<script src="//cdn.jsdelivr.net/react/15.4.2/react.min.js"></script>
<script src="//cdn.jsdelivr.net/react/15.4.2/react-dom.min.js"></script>

<link href="//cdn.jsdelivr.net/npm/graphiql@{{graphiql_version}}/graphiql.css" rel="stylesheet" />
<script src="//cdn.jsdelivr.net/npm/graphiql@{{graphiql_version}}/graphiql.min.js"></script>
</head>
<body>
<script>
Expand Down Expand Up @@ -89,21 +90,14 @@
// that it can be easily shared.
function onEditQuery(newQuery) {
parameters.query = newQuery;
updateURL();
}

function onEditVariables(newVariables) {
parameters.variables = newVariables;
updateURL();
}

function onEditOperationName(newOperationName) {
parameters.operationName = newOperationName;
updateURL();
}

function updateURL() {
history.replaceState(null, null, locationQuery(parameters));
}

// Render <GraphiQL /> into the body.
Expand All @@ -125,10 +119,11 @@
</html>''' # noqa


def render(params, result):
def render(params, result, graphiql_version=GRAPHIQL_VERSION):
result = json.dumps(result, indent=2) if result else ""
return render_template_string(
TEMPLATE,
graphiql_version=GRAPHIQL_VERSION,
result=json.dumps(result, indent=2, ),
graphiql_version=graphiql_version or GRAPHIQL_VERSION,
result=result,
params=params
)
25 changes: 19 additions & 6 deletions flasql/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def __init__(self,
error_handler=None,
result_class=None,
enable_graphiql=True,
graphiql_version=graphiql.GRAPHIQL_VERSION,
context_factory=None):

super(GraphQLView, self).__init__()
Expand All @@ -53,6 +54,8 @@ def __init__(self,
self.result_class = result_class or GraphQLResult
self.enable_graphiql = enable_graphiql
self.context_factory = context_factory or False
self.graphiql_version = graphiql_version
self._params = None

@property
def can_display_graphiql(self):
Expand Down Expand Up @@ -80,26 +83,35 @@ def should_display_graphiql(self):

@property
def params(self):
if self._params:
return self._params

request_keys = {'query', 'operationName', 'variables'}

if request.method == 'GET':
# we will collect the data from the query parameters,
# such as ?query={}
return {k: request.args.get(k) for k in request_keys}

self._params = {k: request.args.get(k) for k in request_keys}
return self._params
if request.method == 'POST':
if request.mimetype == 'application/graphql':
return {'query': request.data.decode('utf8')}

elif request.mimetype == 'application/json':
return request.json
payload = request.json
if "variables" in payload and isinstance(payload["variables"], str):
payload["variables"] = json.loads(payload["variables"])
self._params = payload
return self._params

elif request.mimetype in ('application/x-www-form-urlencoded',
'multipart/form-data'):
return request.form
self._params = request.form
return self._params

# Else, return an empty map
return {k: None for k in request_keys}
self._params = {k: None for k in request_keys}
return self._params

def format_execution_result(self, result):
"""
Expand Down Expand Up @@ -185,7 +197,8 @@ def get(self):
result = self.handle_request()

if self.should_display_graphiql:
return graphiql.render(params=self.params, result=result)
return graphiql.render(params=self.params, result=result,
graphiql_version=self.graphiql_version)

return self.result_class(result)

Expand Down