3

I use Graphene on the server side with similar code to the one from documentation:

class UploadFile(graphene.ClientIDMutation):
     class Input:
         pass
         # nothing needed for uploading file

     # your return fields
     success = graphene.String()

    @classmethod
    def mutate_and_get_payload(cls, root, info, **input):
        # When using it in Django, context will be the request
        files = info.context.FILES
        # Or, if used in Flask, context will be the flask global request
        # files = context.files

        # do something with files

        return UploadFile(success=True)

It's all clear, but how should the request look like ?

I've seen people suggesting multipart/form-data, but AFAIK that requires additional layer to parse the multipart request, so that's probably not what I need.. or is it ? :

curl -X "POST" "http://127.0.0.1:5001/graphql" \
     -H 'Content-Type: multipart/form-data; boundary=----GraphQLFileUpload' \
     -F "operations={\"query\":\"mutation ($files: [Upload!]!) {uploadFile(selfie: $file) {status}}\",\"variables\":{}}" \
     -F "map={\"x\":[\"variables.files.x\"]}" \
     -F "x=@/tmp/dummy.jpg "
Jakub Czaplicki
  • 1,787
  • 2
  • 28
  • 50

1 Answers1

4

I'll reply myself. The curl code I had was based on an external library that confused the hell out of me.

Here's my solution that doesn't require any additional library:

Python server code (graphene):

class UploadImage(graphene.Mutation):
    class Arguments(object):
        file = graphene.String(required=True)

    status = graphene.Boolean()

    def mutate(self, info, file):
        img = info.context.files[file].read()
        # more stuff
        return UploadImage(status=True)

Curl request (multipart form)

curl -X POST http://localhost:5001/graphql \
     -H 'content-type: multipart/form-data; boundary=----GraphQlFileUpload' \
     -F 'query=mutation {uploadImage(file: "photo") {status}}' \
     -F 'photo=@selfie.jpg'
Jakub Czaplicki
  • 1,787
  • 2
  • 28
  • 50