我无法使用graphql-java上传文件

时间:2019-11-13 22:38:14

标签: java graphql postman

我尝试使用Java graphql上传文件。我查看了以下主题的解决方案:How to upload files with graphql-java? 我使用的是graphql-java版本11.0,graphql-spring-boot-starter版本5.0.2,graphql-java-kickstart版本7.5.0。

public class PartDeserializer extends JsonDeserializer {

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

    return null;
  }

  @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;
  }
}
@Configuration
public class GraphqlConfig {

  @Bean
  public GraphQLScalarType uploadScalarDefine() {
    return ApolloScalars.Upload;
  }
}
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;
    }
scalar Upload
testMultiFilesUpload(files: [Upload!]!): Boolean

我这样在Postman中查询数据来源

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

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

file0
0.jpeg
file1
1.jpeg

这是服务器响应

INFO 11663 --- [0.1-1100-exec-7] g.servlet.AbstractGraphQLHttpServlet     : Bad POST multipart request: no part named "graphql" or "query"

我做错了什么?

2 个答案:

答案 0 :(得分:0)

我建议您使用阿波罗 https://github.com/apollographql/apollo-android

它使用RxJava集成,翻新,订阅和对AutoValue的支持。这将使您的工作更加轻松,因为没有直接的方法可以为GraphQL建立查询和解析响应。

答案 1 :(得分:0)

您可以尝试以下依赖关系:

<properties>
        <graphql-java.version>13.0</graphql-java.version>
        <graphql-java-kickstart-springboot.version>5.10.0</graphql-java-kickstart-springboot.version>
        <graphql-java-kickstart-tools.version>5.6.1</graphql-java-kickstart-tools.version>
        <graphql-java-kickstart-servlet.version>8.0.0</graphql-java-kickstart-servlet.version>
</properties>

 <dependency>
    <groupId>com.graphql-java-kickstart</groupId>
    <artifactId>graphql-spring-boot-starter</artifactId>
    <version>5.9.0</version>
 </dependency>
 <dependency>
    <groupId>com.graphql-java-kickstart</groupId>
    <artifactId>graphql-java-tools</artifactId>
    <version>5.6.1</version>
 </dependency>
 <dependency>
    <groupId>com.graphql-java-kickstart</groupId>
    <artifactId>graphiql-spring-boot-starter</artifactId>
    <version>5.6.0</version>
 </dependency>

但是graphql文件上传存在问题,我们不能删除graphql生成的临时文件,因为它始终由graphql使用,并且文件流没有关闭。 < / p>