Tejas Sathe
Tejas Sathe

Reputation: 51

Is there any way to send multipart form request to graphql in reactjs with apollo client

I am new to react with graphql.

I want to upload .pdf file to server via network interface.

All I need to do is write a graphql query using apollo client which sends multipart form data to server.

I tried to search it on google but I did't found any proper solution.

mutation createUser($user: myfile) {
    createData(myfile: $user) {
        id
        name
        email
    }  
}

Upvotes: 5

Views: 10999

Answers (1)

Jesper We
Jesper We

Reputation: 6087

GraphQL doesn't normally deal with multipart data or file uploads, but if you are writing your server yourself you can create a custom request type which has more fields than just the query, and thus make it work. This guy did it here.

This is a rather roundabout way to go at it which will probably give you lots of headaches. I would recommend being less stubborn about the single endpoint, and use a different server to manage the upload. After all, there are lots of well tested libraries dealing with all "the devils details" surrounding uploads.

Upvotes: 6

Related Questions