How to upload file with graphql and graphene

Viewed 487

I'm trying to upload a file using graphql and graphene.

On frontend I have fetch request as follows:

fetch(
            '/graphql', {
                method: 'POST',
                headers: {'Content-Type': 'application/json'},
                body: JSON.stringify({query: MUTATION, variables: {file: file}})
            })
            .then(res => res.json())
            .then(res => {
                debugger;
            })
            .catch(error => {
                debugger;
            });

And mutation is as follows:

const MUTATION = `mutation UploadMutation($file: Upload!) { 
        uploadMutation(file: $file) { 
            ok                
            url
        }
    }`;

And on the backend side I have mutation as follows:

class UploadMutation(graphene.Mutation):
    class Arguments:
        file = Upload(required=True)

    ok = graphene.Boolean()
    url = graphene.String()

    def mutate(root, info, file, **kwargs):
        url = handle_upload_file(file)
        ok = True

        return UploadMutation(ok=ok, url=url)

The uploaded file is as follows:

lastModified: 1569487573753
lastModifiedDate: Thu Sep 26 2019 10:46:13 GMT+0200 (Central European Summer Time) {}
name: "1.png"
path: "1.png"
size: 138373
type: "image/png"
webkitRelativePath: ""

But in the mutate function I get:

{'path': '1.png'}

Any idea what am I doing wrong?

0 Answers
Related