Giter Club home page Giter Club logo

Comments (2)

erikwrede avatar erikwrede commented on May 27, 2024

Graphene does support file uploads, but it's a matter of the underlying web view (for example graphene Django, starlette-graphene3, GraphQL-server) to support them. What's your stack? If you're able to provide a minimal example I might be able to help.

from graphene.

sdobbelaere avatar sdobbelaere commented on May 27, 2024

In reality if you google this topic extensively, you'll notice that there are a number of ways to tackle it.
In the example below, the path we've chosen is to stick to graphql only and used graphene-file-upload.

That graphene-file-upload package supports file transfers. By now, it seems that it hasn't received any updates in a couple of years. But if something would break, I can imagine it will be easy enough to fork and fix.

This very minimal example below is how I tackled the mutation a couple of years ago with that package. It's not intended to work out of the box, it's just a simplified snippet from a codebase in one of our projects at Tweave.tech

from graphene_file_upload.scalars import Upload
import graphene
from .validators import your_file_validator

class CreateFile(graphene.Mutation):
    file = graphene.Field(FileType, required=False)

    class Arguments:
        filename = graphene.String(required=True)
        filecontents = Upload(required=True)

    @staticmethod
    def mutate(root, info, filename, filecontents, *args, **kwargs):
        user = info.context.user
        file_instance = File()
        file_instance.file.save(filename, ContentFile(filecontents.file.read()), save=False)

        try:
            your_file_validator(file_instance.file)
            file_instance.save()
            return CreateFile(file=file_instance)
        except ValidationError:
            return CreateFile(file=None)

from graphene.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.