35

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!

6 Answers 6

61

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

Sign up to request clarification or add additional context in comments.

11 Comments

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!
operations is not being detected by graphql for me. I get the error ``` Bad POST multipart request: no part named "graphql" or "query" ```
varilables seems like a typo
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?
@EmadBaqeri I had the same problem and this worked for me stackoverflow.com/a/69607115/4984903.
|
7

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)

Comments

3

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

4 Comments

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).
btw, same thing works if you use Phoenix + Absinthe (Elixir)
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.
This answer is useless if you are not showing the content of query
2

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.

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

Comments

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\"
        }
    }
}"'

1 Comment

The correct answer gave me the error: "message": "The variables are expected to mutable at this point." for hotchocolate. Using this answer it solves the problem.
-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;
 }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.