简体   繁体   English

有没有办法通过 postman 将文件上传到 GraphQL API 中?

[英]Is there any way to upload files via postman into a GraphQL API?

Currently I'm using Altair to upload files (in my case it's just for images) to my GraphQL API.目前我正在使用 Altair 将文件(在我的情况下只是用于图像)上传到我的 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.但是,我所有的其他路线都存储在 postman 中,如果我可以只使用一个应用程序 - Postman - 来处理所有事情,那就太好了。

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.在 Altair 中,我可以简单地 select 图像并将其存储为变量,作为我的 GraphQL 上传字段的值。

Does anyone know if Postman supports that (or a similiar) feature?有谁知道 Postman 是否支持该(或类似)功能?

Thank you!谢谢!

You can use Form-Data to do this.您可以使用 Form-Data 来执行此操作。

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 )键:map(这是 map 文件到您的查询/突变)

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

Key: 0 ( upload your image/file etc )密钥:0(上传您的图像/文件等)

邮差

Answer from @Brad Larson is correct. @Brad Larson 的回答是正确的。 But contains a typo:但包含一个错字:

You should have {"0":["variables.file"]} instead of "[variables.file]"你应该有{"0":["variables.file"]}而不是"[variables.file]"

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:我使用graphene-file-upload==1.2.2作为上传处理程序遇到了这个问题,发现准备正确的有效负载非常复杂,因为请求包含上传文件的附加参数,所以我希望这会有所帮助: 在此处输入图像描述

This is the curl equivalent request for reference:这是curl等效请求供参考:

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

you can use form-data for the same您可以将表单数据用于相同的

在此处输入图像描述

you have add the below line to your graphql schema file您已将以下行添加到您的 graphql 架构文件中

scalar Upload

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

please add the below maven dependecies and modify your code to add new configuration请添加以下 maven 依赖项并修改您的代码以添加新配置

    <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;
 }
}

Postman was unable to do this, but I found another one called Altair . Postman 无法做到这一点,但我找到了另一个名为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:您甚至可以在下面的屏幕截图中看到它是如何完成的:

截屏 Image taken from https://altairgraphql.dev/图片取自https://altairgraphql.dev/

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.我在浏览器中检查了其他 GraphQL 服务器请求并尝试模拟它们。

This is how it works for me:这对我来说是这样的:

邮递员截图

My server runs Django with Graphene, the difference may be there (?) I guess我的服务器使用石墨烯运行 Django,可能存在差异(?)我猜

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM