I can't find out how to upload files if i use graphql-java, can someone show me a demo? I will be appreciated!
reference : https://github.com/graphql-java-kickstart/graphql-java-tools/issues/240
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;
}
}
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:
then we would define a mutation in schema and a GraphQLMutationResolver for testMultiFilesUpload
here is Resolver:
configure a jackson deserializer for
javax.servlet.http.Part
and register it to ObjectMapperwhy we return null? because the
List<Part> parts
always null ,In the resolver's method, get the parts argument from the DataFetchingEnvironment;register it to ObjectMapper:
like this:
remember to select the form-data option
through this we can upload multiple files