Skip to content

Commit

Permalink
Merge a3f84aa into 2825345
Browse files Browse the repository at this point in the history
  • Loading branch information
switchtrue committed Feb 22, 2017
2 parents 2825345 + a3f84aa commit fe328e5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
10 changes: 6 additions & 4 deletions flask_graphql/graphqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class GraphQLView(View):
graphiql_template = None
middleware = None
batch = False
json_encoder = None
json_decoder = None

methods = ['GET', 'POST', 'PUT', 'DELETE']

Expand Down Expand Up @@ -142,10 +144,10 @@ def get_response(self, request, data, show_graphiql=False):
def json_encode(self, request, d, show_graphiql=False):
pretty = self.pretty or show_graphiql or request.args.get('pretty')
if not pretty:
return json.dumps(d, separators=(',', ':'))
return json.dumps(d, separators=(',', ':'), cls=self.json_encoder)

return json.dumps(d, sort_keys=True,
indent=2, separators=(',', ': '))
return json.dumps(d, sort_keys=True, indent=2,
separators=(',', ': '), cls=self.json_encoder)

# noinspection PyBroadException
def parse_body(self, request):
Expand All @@ -155,7 +157,7 @@ def parse_body(self, request):

elif content_type == 'application/json':
try:
request_json = json.loads(request.data.decode('utf8'))
request_json = json.loads(request.data.decode('utf8'), cls=self.json_decoder)
if self.batch:
assert isinstance(request_json, list)
else:
Expand Down
12 changes: 12 additions & 0 deletions tests/encoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from json import JSONEncoder, JSONDecoder
from json.decoder import WHITESPACE


class TestJSONEncoder(JSONEncoder):
def encode(self, o):
return 'TESTSTRING'


class TestJSONDecoder(JSONDecoder):
def decode(self, s, _w=WHITESPACE.match):
return {'query': '{test}'}
25 changes: 23 additions & 2 deletions tests/test_graphqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from urllib.parse import urlencode

from .app import create_app
from .encoder import TestJSONEncoder, TestJSONDecoder
from flask import url_for


Expand Down Expand Up @@ -500,8 +501,8 @@ def test_batch_supports_post_json_query_with_json_variables(client):
'payload': { 'data': {'test': "Hello Dolly"} },
'status': 200,
}]


@pytest.mark.parametrize('app', [create_app(batch=True)])
def test_batch_allows_post_with_operation_name(client):
response = client.post(
Expand Down Expand Up @@ -532,3 +533,23 @@ def test_batch_allows_post_with_operation_name(client):
},
'status': 200,
}]


@pytest.mark.parametrize('app', [create_app(json_encoder=TestJSONEncoder)])
def test_custom_encoder(client):
response = client.get(url_string(query='{test}'))

# TestJSONEncoder just encodes everything to 'TESTSTRING'
assert response.data.decode() == 'TESTSTRING'


@pytest.mark.parametrize('app', [create_app(json_decoder=TestJSONDecoder)])
def test_custom_decoder(client):
# The submitted data here of 'TEST' is clearly not valid JSON. The TestJSONDecoder will
# decode this into valid JSON with a valid gql query.
response = client.post(url_string(), data='TEST', content_type='application/json')

assert response.status_code == 200
assert response_json(response) == {
'data': {'test': "Hello World"}
}

0 comments on commit fe328e5

Please sign in to comment.