[英]Is there any way to upload files via postman into a GraphQL API?
目前我正在使用 Altair 將文件(在我的情況下只是用於圖像)上傳到我的 GraphQL API。 但是,我所有的其他路線都存儲在 postman 中,如果我可以只使用一個應用程序 - Postman - 來處理所有事情,那就太好了。
在 Altair 中,我可以簡單地 select 圖像並將其存儲為變量,作為我的 GraphQL 上傳字段的值。
有誰知道 Postman 是否支持該(或類似)功能?
謝謝!
@Brad Larson 的回答是正確的。 但包含一個錯字:
你應該有{"0":["variables.file"]}
而不是"[variables.file]"
我使用graphene-file-upload==1.2.2
作為上傳處理程序遇到了這個問題,發現准備正確的有效負載非常復雜,因為請求包含上傳文件的附加參數,所以我希望這會有所幫助:
這是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\"
}
}
}"'
您可以將表單數據用於相同的
您已將以下行添加到您的 graphql 架構文件中
scalar Upload
type Mutation {
uploadFloorMap(floorMapImage: Upload!) : String
}
請添加以下 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>
添加配置代碼
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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.