如何使用graphql-java上传文件? [英] How to upload files with graphql-java?

查看:557
本文介绍了如何使用graphql-java上传文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用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!

参考: https://github.com/graphql -java-kickstart/graphql-java-tools/issues/240

我在springboot中通过使用graphql-java-kickstart graphql-java-tools进行了尝试,但是没有用

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<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,所以我可以获取文件的一部分.

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

推荐答案

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

  1. define a scalar type in our schema

scalar Upload

我们应该为上载配置GraphQLScalarType,请在下面使用它:

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

@Configuration
public class GraphqlConfig {

   @Bean
   public GraphQLScalarType uploadScalarDefine() {
      return ApolloScalars.Upload;
   } 
}

  • 然后我们将在方案中定义一个突变,并为testMultiFilesUpload定义一个GraphQLMutationResolver

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

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

    1. javax.servlet.http.Part配置jackson解串器并将其注册到ObjectMapper

    1. configure a jackson deserializer for javax.servlet.http.Part and register it to 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参数;

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

    environment.getArgument(文件")

    environment.getArgument("files")

    将其注册到ObjectMapper:

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

    1. 要对此进行测试,请将以下表单数据(我们使用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
    

    像这样:

    请记住选择表单数据选项

    remember to select the form-data option

    通过此操作,我们可以上传多个文件

    这篇关于如何使用graphql-java上传文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆