Skip to content

nkonev/multipart-spring-graphql

Repository files navigation

Multipart Spring GraphQL

Adds missing multipart support.

Before using you need to see

Features

  1. FilePart arguments for Webflux reactive stack
@Controller
public class FileController {

    private static final Logger logger = LoggerFactory.getLogger(FileController.class);

    @MutationMapping(name = "fileUpload")
    public FileUploadResult uploadFile(@Argument FilePart file) {
        logger.info("Upload file: name={}", file.filename());

        return new FileUploadResult(UUID.randomUUID());
    }

    @MutationMapping(name = "multiFileUpload")
    public Collection<FileUploadResult> uploadMultiFiles(@Argument Collection<FilePart> files) {
        for (FilePart filePart : files) {
            logger.info("Upload file: name={}", filePart.filename());
        }
        return List.of(new FileUploadResult(UUID.randomUUID()));
    }

}
  1. MultipartFile arguments for WebMVC servlet stack
@Controller
public class FileController {

    private static final Logger logger = LoggerFactory.getLogger(FileController.class);

    @MutationMapping(name = "fileUpload")
    public FileUploadResult uploadFile(@Argument MultipartFile file) {
        logger.info("Upload file: name={}", file.getOriginalFilename());

        return new FileUploadResult(UUID.randomUUID());
    }

    @MutationMapping(name = "multiFileUpload")
    public Collection<FileUploadResult> uploadMultiFiles(@Argument Collection<MultipartFile> files) {
        for (MultipartFile file : files) {
            logger.info("Upload file: name={}", file.getOriginalFilename());
        }
        return List.of(new FileUploadResult(UUID.randomUUID()));
    }

}
  1. WebClient-based client
    @Autowired
    private MultipartGraphQlWebClient httpGraphQlClient;

    @Override
    public void run(String... args) {
        var doc = """
                mutation FileNUpload($files: [Upload!]) {
                    multiFileUpload(files: $files){
                        id
                    }
                }
                """;
        Map<String, Object> fileVariables = singletonMap("files", List.of(
            new ClassPathResource("/foo.txt"), 
            new ClassPathResource("/bar.txt")
        ));

        var request = MultipartClientGraphQlRequest.builder()
            .withDocument(doc)
            .withFileVariables(fileVariables)
            .build();
        var response = httpGraphQlClient.executeFileUpload("http://localhost:8899/graphql", request).block();
        LOGGER.info("Response is {}", response);
    }
  1. RestClient-based client
    @Autowired
    private MultipartGraphQlRestClient httpGraphQlClient;
    
    @Override
    public void run(String... args) {
        var doc = """
            mutation FileNUpload($files: [Upload!]) {
                multiFileUpload(files: $files){
                    id
                }
            }
            """;
        Map<String, Object> fileVariables = singletonMap("files", List.of(
            new ClassPathResource("/foo.txt"), 
            new ClassPathResource("/bar.txt")
        ));
        var request = MultipartClientGraphQlRequest.builder()
            .withDocument(doc)
            .withFileVariables(fileVariables)
            .build();
        var response = httpGraphQlClient.executeFileUpload("http://localhost:8889/graphql", request);
        LOGGER.info("Response is {}", response);
    } 

Download

Spring Boot Starter

<dependency>
  <groupId>name.nkonev.multipart-spring-graphql</groupId>
  <artifactId>multipart-spring-graphql</artifactId>
  <version>VERSION</version>
</dependency>

Compatibility

multipart-spring-graphql Java Spring Boot Example
0.10.x 8+ Spring Boot 2.7.x https://github.com/nkonev/multipart-graphql-demo/tree/0.10.x
1.0.x 17+ Spring Boot 3.0.x https://github.com/nkonev/multipart-graphql-demo/tree/1.0.x
1.1.x 17+ Spring Boot 3.1.x https://github.com/nkonev/multipart-graphql-demo/tree/1.1.x
1.2.x 17+ Spring Boot 3.2.x https://github.com/nkonev/multipart-graphql-demo/tree/1.2.x
1.3.x 17+ Spring Boot 3.3.x https://github.com/nkonev/multipart-graphql-demo/tree/1.3.x
1.4.x 17+ Spring Boot 3.3.x https://github.com/nkonev/multipart-graphql-demo/tree/1.4.x