31

Currently I'm using Altair to upload files (in my case it's just for images) to my GraphQL API. However, all my other routes are stored in postman and it'd be nice if I could use just one application - Postman - for everything.

In Altair I can simply select an image and store that as a variable that I put as the value for my GraphQL Upload field.

Does anyone know if Postman supports that (or a similiar) feature?

Thank you!

joelostblom
  • 43,590
  • 17
  • 150
  • 159
meteoray
  • 335
  • 1
  • 3
  • 8

6 Answers6

51

You can use Form-Data to do this.

Key: Operations (this is for the query / mutation )

{"query":"mutation updateAvatar($avatar:Upload!) {\n  updateAvatar(avatar: $avatar)\n}"}

Key: map ( this is to map the files to your query / mutation )

{"0": ["variables.avatar"]}

Key: 0 ( upload your image/file etc )

postman

A.L
  • 10,259
  • 10
  • 67
  • 98
Brad Larson
  • 589
  • 5
  • 6
  • 2
    Perfect, that works! I was wondering where you could get the 'select file prompt', but i eventually found it. It's kind of hidden as you need to hover over the key field to change it from 'Text' to 'File'. Thank you! – meteoray May 19 '20 at 14:04
  • 5
    operations is not being detected by graphql for me. I get the error ``` Bad POST multipart request: no part named "graphql" or "query" ``` – GSUgambit Nov 08 '20 at 20:50
  • I get this error "KeyError at / graphql / 'variables' " Can you tell me why? https://prnt.sc/yu73z9 - error – LORD Feb 09 '21 at 12:08
  • In my case `{"0": ["varilables.avatar"]}` is only populating `variables.avatar` with the value `"0"` (and not with the file we used as its content). Do you have suggestions? – Kurt Bourbaki May 14 '21 at 11:24
  • 1
    `varilables` seems like a typo – BigMan73 Jun 21 '21 at 17:11
  • 5
    I have tried the solution provided here but I got the error `POST body missing, invalid Content-Type, or JSON object has no keys.` any idea on this? – Emad Baqeri Sep 14 '21 at 09:47
  • this worked for me with [graphql-spring-boot](https://github.com/graphql-java-kickstart/graphql-spring-boot) – stereo Nov 16 '21 at 13:14
  • 2
    @EmadBaqeri I had the same problem and this worked for me https://stackoverflow.com/a/69607115/4984903. – Mukund Gandlur Dec 08 '21 at 10:25
  • For graphene and django users: you would find the upload contents of the mutation in `info.context.FILES` – Kurt Bourbaki Jun 07 '22 at 11:32
  • How would you do to add others variables? E.g. filename, prefix, etc. – BiasInput Apr 22 '23 at 13:03
6

Answer from @Brad Larson is correct. But contains a typo:

You should have {"0":["variables.file"]} instead of "[variables.file]"

(Sorry I don't have enough reputation to comment)

Dmytro H.
  • 96
  • 1
  • 5
5

The above solution didn't work for me but helped as a base.

I've inspected other GraphQL server requests in my browser and tried emulating them.

This is how it works for me:

Postman screenshot

My server runs Django with Graphene, the difference may be there (?) I guess

Cristian Rojas
  • 2,746
  • 7
  • 33
  • 42
  • 4
    Could you please share the content of the query as well please? In my case `profile_picture` is not populating the query variables and `query` is not being passed to graphene/django. I have to use `operations` as in the other answer (but `map` is not populating the query variables either). – Kurt Bourbaki May 14 '21 at 11:21
  • btw, same thing works if you use Phoenix + Absinthe (Elixir) – Alex Filatov Aug 23 '23 at 19:30
  • What is the _query_ data that you put as the value? The screenshot doesn't make it clear how the file input (variable) is defined in the query string. Thanks. – Maxim Philippov Aug 30 '23 at 06:52
1

I ran into this issue using graphene-file-upload==1.2.2 as the upload handler and found it extremely complex to prepare the correct payload as the request included additional parameters to the upload file, so I hope this helps:enter image description here

This is the curl equivalent request for reference:

curl --location --request POST '<REDACTED>/graphql' \
--header 'Authorization: Bearer <REDACTED>' \
--form '0=@"/home/user/Downloads/Five.pdf"' \
--form 'map="{\"0\": [\"variables.data.attachments\"]}"' \
--form 'operations="{
    \"query\": \"mutation AddPOAttachments($data: AddProductAttachmentsInput!) { add_product_attachments(data: $data) { attachments { id url purpose file_info { name type size_in_bytes} __typename } __typename }}\",
    \"variables\":
    {
        \"data\":
        {
            \"sku\": \"123456sku\",
            \"attachments\": null,
            \"purpose\": \"Test\"
        }
    }
}"'
Javi Romero
  • 327
  • 5
  • 14
0

Postman was unable to do this, but I found another one called Altair. See this blog that describes how to use it for file uploads. You can even see a glimpse of how it is done in the screenshot below:

screenshot Image taken from https://altairgraphql.dev/

smac89
  • 39,374
  • 15
  • 132
  • 179
-1

you can use form-data for the same

enter image description here

you have add the below line to your graphql schema file

scalar Upload

type Mutation {
uploadFloorMap(floorMapImage: Upload!) : String
}

please add the below maven dependecies and modify your code to add new configuration

    <dependency>
        <groupId>com.graphql-java-kickstart</groupId>
        <artifactId>graphql-java-servlet</artifactId>
        <version>14.0.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.graphql-java/graphql-java-extended-scalars -->
    <dependency>
        <groupId>com.graphql-java</groupId>
        <artifactId>graphql-java-extended-scalars</artifactId>
    </dependency>

add configuration code

import graphql.kickstart.servlet.apollo.ApolloScalars;
import graphql.schema.GraphQLScalarType;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class GraphQLConfiguration {

@Bean
public GraphQLScalarType uploadScalarDefine() {
return ApolloScalars.Upload;
 }
}
Nantha kumar
  • 153
  • 1
  • 3
  • 8