Package: graphene-file-upload
graphene-file-uploadgraphene-file-uploadis a drop in replacement for the the GraphQL
view in Graphene for Django, and for Flask-Graphql.It supports multi-part file uploads that adhere to theMultipart Request Spec.It currently supports Python 2.7 and 3.4+.Installation:$pipinstallgraphene-file-uploadUsageTo add an upload type to your mutation, import and useUpload.
Upload is a scalar type.fromgraphene_file_upload.scalarsimportUploadclassUploadMutation(graphene.Mutation):classArguments:file=Upload(required=True)success=graphene.Boolean()defmutate(self,info,file,**kwargs):# do something with your filereturnUploadMutation(success=True)Django Integration:To use, import the view, then add to your list of urls (replace previous
GraphQL view).fromgraphene_file_upload.djangoimportFileUploadGraphQLViewurlpatterns=[url(r'^graphql',FileUploadGraphQLView.as_view(graphiql=True)),]Flask Integration:Note thatflask-graphqlversion<2.0is not supported. At the
time of writing this README, you must installflask-graphqlwithpip install--preflask-graphqlSimply import the modified view and create a new url rule on your app:fromgraphene_file_upload.flaskimportFileUploadGraphQLViewapp.add_url_rule('/graphql',view_func=FileUploadGraphQLView.as_view(...))TestingFlaskhttps://flask.palletsprojects.com/en/1.1.x/testing/#the-testing-skeleton/# Create a fixture using the file_graphql_query helper and `client` fixture.importosimportjsonimporttempfilefromflaskrimportflaskrimportpytestfromgraphene_file_upload.django.testingimportfile_graphql_query@pytest.fixturedefclient():db_fd,flaskr.app.config['DATABASE']=tempfile.mkstemp()flaskr.app.config['TESTING']=Truewithflaskr.app.test_client()asclient:withflaskr.app.app_context():flaskr.init_db()yieldclientos.close(db_fd)os.unlink(flaskr.app.config['DATABASE'])@pytest.fixturedefclient_query(client):deffunc(*args,**kwargs):returnfile_graphql_query(*args,**kwargs,client=client)returnfunc# Test your query using the client_query fixturedeftest_some_query(client_query):test_file=SimpleUploadedFile(name='test.txt',content=file_text.encode('utf-8'))response=client_query('''
        mutation testMutation($file: Upload!) {
            myUpload(fileIn: $file) {
                ok
            }
        }
        ''',op_name='testMutation'files={'file':test_file},)content=json.loads(response.content)assert'errors'notincontentDjangoWriting test usingdjango’s test clientUsing pytestTo use pytest define a simple fixture using the query helper below# Create a fixture using the file_graphql_query helper and ``client` fixture from ``pytest-django``.importjsonimportpytestfromgraphene_file_upload.django.testingimportfile_graphql_query@pytest.fixturedefclient_query(client):deffunc(*args,**kwargs):returnfile_graphql_query(*args,**kwargs,client=client)returnfunc# Test your query using the client_query fixturedeftest_some_query(client_query):test_file=SimpleUploadedFile(name='test.txt',content=file_text.encode('utf-8'))response=client_query('''
        mutation testMutation($file: Upload!) {
            myUpload(fileIn: $file) {
                ok
            }
        }
        ''',op_name='testMutation'files={'file':test_file},)content=json.loads(response.content)assert'errors'notincontentUsing unittestYour endpoint is set through theGRAPHQL_URLattribute onGraphQLFileUploadTestCase.The default endpoint isGRAPHQL_URL = “/graphql/”.importjsonfromgraphene_file_upload.django.testingimportGraphQLFileUploadTestCaseclassMutationTestCase(GraphQLFileUploadTestCase):deftest_some_mutation(self):test_file=SimpleUploadedFile(name='test.txt',content=file_text.encode('utf-8'))response=self.file_query('''
            mutation testMutation($file: Upload!) {
                myUpload(fileIn: $file) {
                    ok
                }
            }
            ''',op_name='testMutation',files={'file':test_file},)# This validates the status code and if you get errorsself.assertResponseNoErrors(response)Contributing:If you’d like to contribute, please run the test suite prior to sending a PR.In order to run the testing environment, create a virtual environment, install
tox, and run the tox commands:$python3-mvenvvenv$sourcevenv/bin/activate$makeinstall# You may have to deactivate and reactivate to have access to the tox command,
# depending on your system.# Run the test suite with the versions of python you have installed$tox# Alternatively, if you're using something like pyenv and can easily install
# Multiple versions of python, then try running the following command$tox# If for some reason you need to recreate the tox environment (e.g. a new
# dependency has been added since you last ran it, add the -r flag to the
# tox command)$tox-r{...additionalflags...}Check outpyenvif you’d like a simple way of
installing multiple python versions to test out.Packaging for PyPi:Run$makedeploy