I am trying to upload a GraphQL mutation and an image as application/form-data. The GraphQL part is working, but I would like to 'save' the uploaded binary and add the path to the GraphQL data. In the createGraphQLContext I have access to the HttpServletRequest but the (multi)parts are empty. I use the graphql-spring-boot-starter with embedded tomcat 8.5 and the supplied GraphQL Java Tools
This is my Relay Modern call to /graphql
------WebKitFormBoundaryWBzwQyVX0TvBTIBD
Content-Disposition: form-data; name="query"
mutation CreateProjectMutation(
$input: ProjectInput!
) {
createProject(input: $input) {
id
name
}
}
------WebKitFormBoundaryWBzwQyVX0TvBTIBD
Content-Disposition: form-data; name="variables"
{"input":{"name":"sdasas"}}
------WebKitFormBoundaryWBzwQyVX0TvBTIBD
Content-Disposition: form-data; name="file"; filename="51zvT5zy44L._SL500_AC_SS350_.jpg"
Content-Type: image/jpeg
------WebKitFormBoundaryWBzwQyVX0TvBTIBD--
In my @Component
public class MyGraphQLContextBuilder implements GraphQLContextBuilder
I have access to HttpServletRequest
and would like to access the file using req.getPart( "file" )
But my parts in the requests are empty intellij debugger
I've added this to my application.yml
spring:
http:
multipart:
enabled: true
file-size-threshold: 10MB
location: /tmp
max-file-size: 10MB
max-request-size: 15MB
resolve-lazily: false
And tried different @configuration to enable multipart configurations but parts are still empty.
@Configuration
public class MultipartConfig {
@Bean
public MultipartResolver multipartResolver() {
StandardServletMultipartResolver resolver = new StandardServletMultipartResolver();
return resolver;
}
}
import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class MyInitializer
extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { MultipartConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/graphql" };
}
@Override
protected void customizeRegistration(Dynamic registration) {
//Parameters:-
// location - the directory location where files will be stored
// maxFileSize - the maximum size allowed for uploaded files
// maxRequestSize - the maximum size allowed for multipart/form-data requests
// fileSizeThreshold - the size threshold after which files will be written to disk
MultipartConfigElement multipartConfig = new MultipartConfigElement("/tmp", 1048576,
10485760, 0);
registration.setMultipartConfig(multipartConfig);
}
}
I have no clue what to do. Hoping some one could help me.
Thank you.
Spring boot's embedded Tomcat is defaulted to Servlet 3.x multipart support. GraphQL java servlet supports commons FileUpload. To make things work you have to disable Spring boots default multipart config, like:
Add maven dependency for commons-fileupload in pom.xml
Application.yml
Spring Boot Application class
And in your @Configuration add a @Bean
Now you can find the uploaded multipart files in the GraphQL Context because they are automatically mapped to:
Accessible from the DataFetchingEnvironment
And the implementation example of the mutation: