Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b26ee01

Browse filesBrowse files
committed
Prettify request response when GraphiQL. Fixed #6
1 parent d08e093 commit b26ee01
Copy full SHA for b26ee01

File tree

3 files changed

+27
-3
lines changed
Filter options

3 files changed

+27
-3
lines changed

‎flask_graphql/graphqlview.py

Copy file name to clipboardExpand all lines: flask_graphql/graphqlview.py
+4-3Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,15 @@ def get_response(self, request, data, show_graphiql=False):
133133
'status': status_code,
134134
}
135135

136-
result = self.json_encode(request, response)
136+
result = self.json_encode(request, response, show_graphiql)
137137
else:
138138
result = None
139139

140140
return result, status_code
141141

142-
def json_encode(self, request, d):
143-
if not self.pretty and not request.args.get('pretty'):
142+
def json_encode(self, request, d, show_graphiql=False):
143+
pretty = self.pretty or show_graphiql or request.args.get('pretty')
144+
if not pretty:
144145
return json.dumps(d, separators=(',', ':'))
145146

146147
return json.dumps(d, sort_keys=True,

‎tests/test_graphiqlview.py

Copy file name to clipboardExpand all lines: tests/test_graphiqlview.py
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,17 @@ def app():
1212
def test_graphiql_is_enabled(client):
1313
response = client.get(url_for('graphql'), headers={'Accept': 'text/html'})
1414
assert response.status_code == 200
15+
16+
17+
def test_graphiql_renders_pretty(client):
18+
response = client.get(url_for('graphql', query='{test}'), headers={'Accept': 'text/html'})
19+
assert response.status_code == 200
20+
pretty_response = (
21+
'{\n'
22+
' "data": {\n'
23+
' "test": "Hello World"\n'
24+
' }\n'
25+
'}'
26+
).replace("\"","\\\"").replace("\n","\\n")
27+
28+
assert pretty_response in response.data.decode('utf-8')

‎tests/test_graphqlview.py

Copy file name to clipboardExpand all lines: tests/test_graphqlview.py
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,15 @@ def test_supports_pretty_printing(client):
327327
)
328328

329329

330+
@pytest.mark.parametrize('app', [create_app(pretty=False)])
331+
def test_not_pretty_by_default(client):
332+
response = client.get(url_string(query='{test}'))
333+
334+
assert response.data.decode() == (
335+
'{"data":{"test":"Hello World"}}'
336+
)
337+
338+
330339
def test_supports_pretty_printing_by_request(client):
331340
response = client.get(url_string(query='{test}', pretty='1'))
332341

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.