gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 21:49:18 35 4
gpt4 key购买 nike

如果我使用 graphql-java,我无法找到如何上传文件,有人可以给我演示一下吗?我将不胜感激!

引用:https://github.com/graphql-java-kickstart/graphql-java-tools/issues/240

我在springboot中使用graphql-java-kickstart graphql-java-tools尝试过,但没有成功

@Component
public class FilesUpload implements GraphQLMutationResolver {

public Boolean testMultiFilesUpload(List<Part> parts, DataFetchingEnvironment env) {
// get file parts from DataFetchingEnvironment, the parts parameter is not used
List<Part> attchmentParts = env.getArgument("files");
System.out.println(attchmentParts);
return true;
}
}

这是我的架构

type Mutation {
testSingleFileUpload(file: Upload): UploadResult
}

我希望这个解析器可以打印attchmentParts,这样我就可以获得文件部分。

最佳答案

  1. 在我们的架构中定义标量类型

    scalar Upload

    我们应该为上传配置 GraphQLScalarType,使用如下:

    @Configuration
    public class GraphqlConfig {

    @Bean
    public GraphQLScalarType uploadScalarDefine() {
    return ApolloScalars.Upload;
    }
    }
  2. 然后我们将为 testMultiFilesUpload 定义架构中的突变和 GraphQLMutationResolver

    type Mutation {
    testMultiFilesUpload(files: [Upload!]!): Boolean
    }

这是解析器:

public Boolean testMultiFilesUpload(List<Part> parts, DataFetchingEnvironment env) {
// get file parts from DataFetchingEnvironment, the parts parameter is not use
List<Part> 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;
}
}
  • javax.servlet.http.Part 配置 Jackson 解串器并将其注册到ObjectMapper

    public class PartDeserializer extends JsonDeserializer<Part> {

    @Override
    public Part deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    return null;
    }
    }

    为什么我们返回 null?因为List<Part> parts总是 null ,在解析器的方法中,从 DataFetchingEnvironment 获取 parts 参数;

    environment.getArgument("files")

  • 将其注册到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;
    }
  • 要对此进行测试,请将以下表单数据(我们使用 Postman)发布到 GraphQL 端点
  • 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

    像这样:

    记住选择表单数据选项 enter image description here

    通过这个我们可以上传多个文件

    关于java - 如何使用graphql-java上传文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57372259/

    35 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com