All Projects → lmcgartland → Graphene File Upload

lmcgartland / Graphene File Upload

Licence: mit
Enhances Graphene Django GraphQL Server for intuitive file uploads via GraphQL mutations.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Graphene File Upload

Cookiecutter Django Vue
Cookiecutter Django Vue is a template for Django-Vue projects.
Stars: ✭ 462 (+120%)
Mutual labels:  graphql, django
Best Of Web Python
🏆 A ranked list of awesome python libraries for web development. Updated weekly.
Stars: ✭ 1,118 (+432.38%)
Mutual labels:  graphql, django
Django Graphql Jwt
JSON Web Token (JWT) authentication for Graphene Django
Stars: ✭ 649 (+209.05%)
Mutual labels:  graphql, django
Graphene Django
Integrate GraphQL into your Django project.
Stars: ✭ 3,738 (+1680%)
Mutual labels:  graphql, django
Saleor
A modular, high performance, headless e-commerce platform built with Python, GraphQL, Django, and React.
Stars: ✭ 14,720 (+6909.52%)
Mutual labels:  graphql, django
Graphene Django Extras
Extras functionalities for Graphene-Django
Stars: ✭ 356 (+69.52%)
Mutual labels:  graphql, django
Django Graph Api
Pythonic implementation of the GraphQL specification for the Django Web Framework.
Stars: ✭ 29 (-86.19%)
Mutual labels:  graphql, django
Strawberry
A new GraphQL library for Python 🍓
Stars: ✭ 891 (+324.29%)
Mutual labels:  graphql, django
Crudl Example Django
CRUDL with Django, DRF/Graphene and SQLite
Stars: ✭ 113 (-46.19%)
Mutual labels:  graphql, django
Django Graphql Social Auth
Python Social Auth support for Graphene Django
Stars: ✭ 90 (-57.14%)
Mutual labels:  graphql, django
Gitlit
Platform to connect contributors and projects based on skill level and shared interests.
Stars: ✭ 265 (+26.19%)
Mutual labels:  graphql, django
Djangochannelsgraphqlws
Django Channels based WebSocket GraphQL server with Graphene-like subscriptions
Stars: ✭ 203 (-3.33%)
Mutual labels:  graphql, django
Cms
Club Management System of amFOSS, powered by CMS
Stars: ✭ 263 (+25.24%)
Mutual labels:  graphql, django
Django Api Domains
A pragmatic styleguide for Django API Projects
Stars: ✭ 365 (+73.81%)
Mutual labels:  graphql, django
Cookiecutter Django Vue Graphql Aws
A highly opinionated Cookiecutter template that fuses together Django, Vue.js, GraphQL, and AWS into one full-stack web application.
Stars: ✭ 213 (+1.43%)
Mutual labels:  graphql, django
Ariadne
Ariadne is a Python library for implementing GraphQL servers using schema-first approach.
Stars: ✭ 1,274 (+506.67%)
Mutual labels:  graphql, django
Graphene Django Subscriptions
This package adds support to Subscription's requests and its integration with websockets using Channels package.
Stars: ✭ 173 (-17.62%)
Mutual labels:  graphql, django
Django Graphql Auth
Django registration and authentication with GraphQL.
Stars: ✭ 200 (-4.76%)
Mutual labels:  graphql, django
Django Static Precompiler
Django Static Precompiler provides template tags and filters to compile CoffeeScript, LiveScript, SASS / SCSS, LESS, Stylus, Babel and Handlebars. It works with both inline code and external files.
Stars: ✭ 206 (-1.9%)
Mutual labels:  django
Absinthe plug
Plug support for Absinthe, the GraphQL toolkit for Elixir
Stars: ✭ 209 (-0.48%)
Mutual labels:  graphql

Build Status PyPI version Downloads

PyPI - Python Version PyPI - Django Version Flask

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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].