0

Im tried upload file with java graphql. I looked at a solution to this topic: How to upload files with graphql-java? I'm using graphql-java version 11.0, graphql-spring-boot-starter version 5.0.2, graphql-java-kickstart version 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

My query from-data in Postman like that

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

this is server response

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

what I'm doing wrong?

Logan_on
  • 117
  • 4
  • 11

2 Answers2

0

I suggest you use Apollo https://github.com/apollographql/apollo-android

It uses RxJava Integration, Retrofit, Subscriptions and support for AutoValue. This will make your work easier as there are no straightforward ways in building Queries & Parsing responses for GraphQL.

olajide
  • 972
  • 1
  • 13
  • 26
  • Thanks for the advice. Just tell me please can I use apollo without gradle? I am currently using Maven. I saw that Apollo needs to send the file, I have to define this mapping in build.gradle file. How do i do that in maven project ? – Logan_on Nov 17 '19 at 09:54
  • Yes, you can use apollographql client without gradle. Check out this answer https://stackoverflow.com/a/56266319/4564200 – olajide Nov 17 '19 at 15:58
0

you can try this dependencies :

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

But there is a problem with graphql file upload,we can't delete the temp file that generate by graphql, because it always be used by graphql and the file stream didn't closed.

EA Ubisoft
  • 326
  • 1
  • 3
  • 9
  • Thanks for You answer. I have another problem.When I try to send a file by postman. I receive such a message: ~~~ "message": "Exception while fetching data (/testMultiFilesUpload) : No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ArrayList[0]->org.apache.catalina.core.ApplicationPart[\"inputStream\"]->java.io.FileInputStream[\"fd\"])" ~~~ What's wrong is that? It is disabled in the PartDeserializer.class – Logan_on Nov 19 '19 at 23:12
  • objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); this code should be avoid FAIL_ON_EMPTY_BEANS exception ,but it look like didn't work . – EA Ubisoft Nov 20 '19 at 02:02
  • Sorry my mistake I forgot the annotation @Component. How to change the save path? – Logan_on Nov 20 '19 at 19:51
  • "the save path" you mean your storage path?you need to handle the file stream, this is your choice,i can't help you – EA Ubisoft Nov 22 '19 at 02:08