Skip to content

example code on how to upload a file with multipart requests to GraphQL in nestjs

Notifications You must be signed in to change notification settings

MuziwandileTNdlovu/blog-graphql-nestjs-fileupload

 
 

Repository files navigation

blog-graphql-nestjs-fileupload

upload file via GraphQL with multipart requests.

the graphql queries and mutations are as follows -

type Query {
  author(id: Int!): Person!
}

type Mutation {
  coverPhoto(file: Upload!): Int!
}

the mutation accepts file and returns back the length in bytes.

The data stored in the app is in-memory and is not recommended for production.

To run the mutation, you can use Postman or directly use this cURL command at the root of the project (it has a sample image)

curl --location --request POST 'http://localhost:8080/graphql' \
--form 'operations="{\"query\": \"mutation updateProfilePhoto($file: Upload!) {  coverPhoto(file: $file)} \", \"variables\": {\"file\": null}}"' \
--form 'map="{\"0\": [\"variables.file\"]}"' \
--form '0=@"./assets/grand-palais-mrsauravsahu.jpg"'

this will return the length of the file in bytes.

If you import this request in Postman, make sure the file path is correct. 👍

-S

About

example code on how to upload a file with multipart requests to GraphQL in nestjs

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 88.5%
  • JavaScript 11.5%