From 199001527fb58975bac05e2b2fe68462ee7e3150 Mon Sep 17 00:00:00 2001 From: Amit Saha Date: Wed, 27 Jan 2016 15:45:13 +1100 Subject: [PATCH] Add support for multipart/form-data This will allow mutations with file uploads --- graphql_flask/graphqlview.py | 4 +++- tests/test_graphqlview.py | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/graphql_flask/graphqlview.py b/graphql_flask/graphqlview.py index d371466..077bfe2 100644 --- a/graphql_flask/graphqlview.py +++ b/graphql_flask/graphqlview.py @@ -95,7 +95,6 @@ def json_encode(self, request, d): # noinspection PyBroadException def parse_body(self, request): content_type = self.get_content_type(request) - if content_type == 'application/graphql': return {'query': request.data.decode()} @@ -110,6 +109,9 @@ def parse_body(self, request): elif content_type == 'application/x-www-form-urlencoded': return request.form + elif content_type == 'multipart/form-data': + return request.form + return {} def _execute(self, *args, **kwargs): diff --git a/tests/test_graphqlview.py b/tests/test_graphqlview.py index 1c4b673..f8f7035 100644 --- a/tests/test_graphqlview.py +++ b/tests/test_graphqlview.py @@ -1,6 +1,11 @@ import pytest import json +try: + from StringIO import StringIO +except ImportError: + from io import StringIO + try: from urllib import urlencode except ImportError: @@ -420,3 +425,17 @@ def test_passes_request_into_request_context(client): 'request': 'testing' } } + +def test_post_multipart_data(client): + query = 'mutation TestMutation { writeTest { test } }' + response = client.post( + url_string(), + data= { + 'query': query, + 'file': (StringIO(), 'text1.txt'), + }, + content_type='multipart/form-data' + ) + + assert response.status_code == 200 + assert response_json(response) == {'data': {u'writeTest': {u'test': u'Hello World'}}}