graphql java demo,如何使用graphql-java上传文件?

I can't find out how to upload files if i use graphql-java, can someone show me a demo? I will be appreciated!

I tried it in springboot by using graphql-java-kickstart graphql-java-tools, but it didn't work

@Component

public class FilesUpload implements GraphQLMutationResolver {

public Boolean testMultiFilesUpload(List parts, DataFetchingEnvironment env) {

// get file parts from DataFetchingEnvironment, the parts parameter is not used

List attchmentParts = env.getArgument("files");

System.out.println(attchmentParts);

return true;

}

}

this is my schema

type Mutation {

testSingleFileUpload(file: Upload): UploadResult

}

I expect this resolver can print attchmentParts,so i can get the file part.

解决方案

define a scalar type in our schema

scalar Upload

and we should configure GraphQLScalarType for Upload, use this below:

@Configuration

public class GraphqlConfig {

@Bean

public GraphQLScalarType uploadScalarDefine() {

return ApolloScalars.Upload;

}

}

then we would define a mutation in schema and a GraphQLMutationResolver for testMultiFilesUpload

type Mutation {

testMultiFilesUpload(files: [Upload!]!): Boolean

}

here is Resolver:

public Boolean testMultiFilesUpload(List parts, DataFetchingEnvironment env) {

// get file parts from DataFetchingEnvironment, the parts parameter is not use

List attachmentParts = env.getArgument("files");

int i = 1;

for (Part part : attachmentParts) {

String uploadName = "copy" + i;

try {

part.write("your path:" + uploadName);

} catch (IOException e) {

e.printStackTrace();

}

i++;

}

return true;

}

}

configure a jackson deserializer for javax.servlet.http.Part and register it to ObjectMapper

public class PartDeserializer extends JsonDeserializer {

@Override

public Part deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {

return null;

}

}

why we return null? because the List parts always null ,In the resolver's method, get the parts argument from the DataFetchingEnvironment;

environment.getArgument("files")

register it to ObjectMapper:

@Bean

public ObjectMapper objectMapper() {

ObjectMapper objectMapper = new ObjectMapper();

objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

SimpleModule module = new SimpleModule();

module.addDeserializer(Part.class, new PartDeserializer());

objectMapper.registerModule(module);

return objectMapper;

}

To test this, post the following form data (we use Postman) to GraphQL endpoint

operations

{ "query": "mutation($files: [Upload!]!) {testMultiFilesUpload(files:$files)}", "variables": {"files": [null,null] } }

map

{ "file0": ["variables.files.0"] , "file1":["variables.files.1"]}

file0

your file

file1

your file

like this:

remember to select the form-data option

89Yjc.png

through this we can upload multiple files

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值