graphql-spring-boot upload binary

2020-04-08 02:31发布

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.

1条回答
【Aperson】
2楼-- · 2020-04-08 02:47

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

    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.3</version>
    </dependency>

Application.yml

spring:
    servlet:
      multipart:
         enabled: false

Spring Boot Application class

@EnableAutoConfiguration(exclude={MultipartAutoConfiguration.class})

And in your @Configuration add a @Bean

@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
    multipartResolver.setMaxUploadSize(100000);
    return multipartResolver;
}

Now you can find the uploaded multipart files in the GraphQL Context because they are automatically mapped to:

environment -> context -> files

Accessible from the DataFetchingEnvironment

And the implementation example of the mutation:

@Component
public class Mutation implements GraphQLMutationResolver {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private UserService userService;

    @Autowired
    private ProjectRepository repository;

    @Autowired
    @Qualifier( value = "modeshape" )
    private StorageService storageService;

    @GraphQLField @GraphQLRelayMutation
    public ProjectItem createProject( CreateProjectInput input, DataFetchingEnvironment environment ) {
        Project project = new Project( input.getName() );
        project.setDescription( input.getDescription() );
        GraphQLContext context = environment.getContext();
        Optional<Map<String, List<FileItem>>> files = context.getFiles();
        files.ifPresent( keys -> {
            List<FileItem> file = keys.get( "file" );
            List<StorageService.FileInfo> storedFiles = file.stream().map( f -> storageService.store( f, "files", true ) ).collect( Collectors.toList() );
            project.setFile( storedFiles.get( 0 ).getUuid() );
        } );
        repository.save( project );
        return new ProjectItem( project );
    }
class CreateProjectInput {
    private String name;
    private String description;
    private String clientMutationId;

    @GraphQLField
    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription( String description ) {
        this.description = description;
    }

    @GraphQLField
    public String getClientMutationId() {
        return clientMutationId;
    }
}
查看更多
登录 后发表回答