jaydenseric / graphql-multipart-request-spec

A spec for GraphQL multipart form requests (file uploads).
993 stars 54 forks source link

Not able to upload image from React Ant to Node.js server via GraphQL #66

Closed Heni-ghodbane closed 1 year ago

Heni-ghodbane commented 1 year ago

This is the whole issue description on stackoverflow

I have tested the mutation through Postman, and it works correct.

Screenshot_2

But I don't know how to convert this Postman file upload request to TS react code.

Could some hone help me. I hope the issue and solution, will help others who are running on the same issue/need.

Many thanks,

Heni-ghodbane commented 1 year ago

The solution was to enable the multipart request on the client-side in the index.js file by adding link: createUploadLink() instead of link: new HttpLink

I am using graphql-upload to upload file on server-side

See below:

import { createUploadLink } from 'apollo-upload-client';

const option = {
  uri: "http://localhost:4000/graphql",
  credentials: "include"
}
 const client = new ApolloClient({
    cache: new InMemoryCache(),
    link: createUploadLink(option),

    /*link: new HttpLink({
        uri: "http://localhost:4000/graphql",
        credentials: "include"
    }),*/
});

For more details, see the installation section of apollo-upload-client