Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postman - Upload file and other argument with GraphQL

Tags:

I am trying to use Postman to upload a file to GraphQL.

I know how I can upload a file, however, in my mutation I have to pass a String! together with the Upload!.

I am not sure where I can pass this String! in Postman.

My mutation:

mutation AddBookImageOne($bookId: string, $bookImageOne: Upload!){
  addBookImageOne(bookId: $bookId, bookImageOne: $bookImageOne)
}

I tried to pass bookId in the variables key but I keep getting the error:

"message": "Variable "$bookId" of required type "String!" was not provided.",

I checked this: Graphql mutation to upload file with other fields but they use CURL

"Operations" in postman field is:

{"query":"mutation AddBookImageOne($bookId: String!, $bookImageOne: Upload!){\n  addBookImageOne(bookId: $bookId, bookImageOne: $bookImageOne)\n}"}

enter image description here

like image 866
Magofoco Avatar asked Aug 25 '20 23:08

Magofoco


People also ask

Is there any way to upload files via Postman into a GraphQL API?

You can use Form-Data to do this. Perfect, that works!

How do you pass a GraphQL variable in the Postman?

Sending GraphQL queries in the request bodyOpen a new request tab in Postman and enter your GraphQL endpoint URL in the address field. Select POST from the request method dropdown list. Under the Body tab, select the GraphQL body type. Enter your GraphQL query in the Query editor.

Can you upload files with GraphQL?

GraphQL Yoga supports GraphQL Multipart Request Specification which allows you to upload files via HTTP and consume the binary data inside GraphQL Resolvers. In GraphQL Yoga, you consume uploaded files or blobs as WHATWG standard File or Blob objects you might be familiar from the browser's API.

How do I upload a file to a GraphQL server?

For uploading files via GraphQL you will need: graphql-multipart-request-spec - will be good if you get acquainted with this spec. apollo-upload-server - for parsing multipart/form-data POST requests via busboy and providing File s data to resolve function as argument.


1 Answers

SOLVED thanks to @xadm

I had to pass the variables in the operations field like:

{"query":"mutation AddBookImageOne($bookId: String!, $bookImageOne: Upload!){\n  addBookImageOne(bookId: $bookId, bookImageOne: $bookImageOne)\n}", "variables": { "bookImageOne": null, "bookId": "be96934c-d20c-4fad-b4bb-a1165468bad9" } }`

enter image description here

like image 199
Magofoco Avatar answered Oct 01 '22 03:10

Magofoco