graphene-file-upload
graphene-file-upload is 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 the Multipart Request Spec.
It currently supports Python 2.7 and 3.4+.
Installation:
$ pip install graphene-file-upload
Usage
To add an upload type to your mutation, import and use Upload. Upload is a scalar type.
from graphene_file_upload.scalars import Upload
class UploadMutation(graphene.Mutation):
class Arguments:
file = Upload(required=True)
success = graphene.Boolean()
def mutate(self, info, file, **kwargs):
# do something with your file
return UploadMutation(success=True)
Django Integration:
To use, import the view, then add to your list of urls (replace previous GraphQL view).
from graphene_file_upload.django import FileUploadGraphQLView
urlpatterns = [
url(r'^graphql', FileUploadGraphQLView.as_view(graphiql=True)),
]
Flask Integration:
Note that flask-graphql version <2.0 is not supported. At the time of writing this README, you must install flask-graphql with pip install --pre flask-graphql
Simply import the modified view and create a new url rule on your app:
from graphene_file_upload.flask import FileUploadGraphQLView
app.add_url_rule(
'/graphql',
view_func=FileUploadGraphQLView.as_view(
...
)
)
Testing
Flask
https://flask.palletsprojects.com/en/1.1.x/testing/#the-testing-skeleton/
# Create a fixture using the file_graphql_query helper and `client` fixture.
import os
import json
import tempfile
from flaskr import flaskr
import pytest
from graphene_file_upload.django.testing import file_graphql_query
@pytest.fixture
def client():
db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp()
flaskr.app.config['TESTING'] = True
with flaskr.app.test_client() as client:
with flaskr.app.app_context():
flaskr.init_db()
yield client
os.close(db_fd)
os.unlink(flaskr.app.config['DATABASE'])
@pytest.fixture
def client_query(client):
def func(*args, **kwargs):
return file_graphql_query(*args, **kwargs, client=client)
return func
# Test your query using the client_query fixture
def test_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' not in content
Django
Writing test using django’s test client
Using pytest
To 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``.
import json
import pytest
from graphene_file_upload.django.testing import file_graphql_query
@pytest.fixture
def client_query(client):
def func(*args, **kwargs):
return file_graphql_query(*args, **kwargs, client=client)
return func
# Test your query using the client_query fixture
def test_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' not in content
Using unittest
Your endpoint is set through the GRAPHQL_URL attribute on GraphQLFileUploadTestCase.
The default endpoint is GRAPHQL_URL = “/graphql/”.
import json
from graphene_file_upload.django.testing import GraphQLFileUploadTestCase
class MutationTestCase(GraphQLFileUploadTestCase):
def test_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 errors
self.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 -m venv venv
$ source venv/bin/activate
$ make install
# 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 {...additional flags...}
Check out pyenv if you’d like a simple way of installing multiple python versions to test out.
Packaging for PyPi:
Run
$ make deploy
Metadata
Release files for graphene-file-upload 1.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| graphene_file_upload-1.3.0.tar.gz | 10.7 kB | Without provenance | Details for graphene_file_upload-1.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| graphene_file_upload-1.3.0-py3-none-any.whl | Python 3 | none | any | Details for graphene_file_upload-1.3.0-py3-none-any.whl |
Total release size: 19.7 kB
Release files / graphene_file_upload-1.3.0.tar.gz
| Download URL | graphene_file_upload-1.3.0.tar.gz |
|---|---|
| Size | 10.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6898480b0556826472c80971032917c01968ade5800d84054008fe598795b063
|
|
BLAKE2b-256 checksum How to use checksums |
259ad06f6e3fa33a53c8ba2cbe1a5ad509b7dc7124e85f221b8e13cee6c7d0c2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.1
|
Release files / graphene_file_upload-1.3.0-py3-none-any.whl
| Download URL | graphene_file_upload-1.3.0-py3-none-any.whl |
|---|---|
| Size | 8.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5afe50f409f50e3d198fd92c883d98d868e6c6aaadf5df3a3f4d88ecad90ed97
|
|
BLAKE2b-256 checksum How to use checksums |
9ab22961fd74e960729a667137435dc3229fb38d73f447c9a6da58372344ab32
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.1
|
Security for graphene-file-upload 1.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.