I’m currently working on a Spring Boot project that uses GraphQL, and I need to implement a file upload functionality. I’ve set up my GraphQL schema and resolvers, but I’m unsure how to properly handle file uploads through GraphQL. Most of the examples I’ve found involve REST APIs rather than GraphQL.
So far my app looks like this:
<dependencies>
<!--Spring Boot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-graphql</artifactId>
</dependency>
<!--GraphQL-->
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java-extended-scalars</artifactId>
<version>${com.graphql-java}</version>
</dependency>
<dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>graphql-java-servlet</artifactId>
<version>15.2.0</version>
</dependency>
<!--Tests-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webflux</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.graphql</groupId>
<artifactId>spring-graphql-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
@Configuration
public class GraphQLConfig {
@Bean
public RuntimeWiringConfigurer runtimeWiringConfigurer() {
return wiringBuilder -> wiringBuilder.scalar(ApolloScalars.Upload);
}
}
//File schema.graphqls
scalar Upload
type Mutation {
uploadFile(file: Upload!): String
}
@Controller
public class MediaController {
@MutationMapping
public String uploadFile(@Argument MultipartFile file, DataFetchingEnvironment environment) {
return "Uploaded";
}
}
New contributor
lemon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.