-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for apollo file uploads, resolves #61
- Loading branch information
Showing
10 changed files
with
356 additions
and
62 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
...c/main/java/io/leangen/graphql/spqr/spring/autoconfigure/FileUploadAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.leangen.graphql.spqr.spring.autoconfigure; | ||
|
||
import io.leangen.graphql.module.Module; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class FileUploadAutoConfiguration { | ||
|
||
@Bean | ||
@ConditionalOnProperty(name = "graphql.spqr.multipart-upload.enabled", havingValue = "true") | ||
public Internal<Module> uploadModule() { | ||
FileUploadHandler uploadAdapter = new FileUploadHandler(); | ||
return new Internal<>(context -> context.getSchemaGenerator() | ||
.withArgumentInjectors(uploadAdapter) | ||
.withTypeMappers(uploadAdapter) | ||
); | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
...nfigure/src/main/java/io/leangen/graphql/spqr/spring/autoconfigure/FileUploadHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package io.leangen.graphql.spqr.spring.autoconfigure; | ||
|
||
import java.lang.reflect.AnnotatedElement; | ||
import java.lang.reflect.AnnotatedType; | ||
import java.lang.reflect.Parameter; | ||
import java.util.*; | ||
|
||
import graphql.schema.*; | ||
import io.leangen.geantyref.GenericTypeReflector; | ||
import io.leangen.graphql.generator.mapping.ArgumentInjector; | ||
import io.leangen.graphql.generator.mapping.ArgumentInjectorParams; | ||
import io.leangen.graphql.generator.mapping.TypeMapper; | ||
import io.leangen.graphql.generator.mapping.TypeMappingEnvironment; | ||
import io.leangen.graphql.util.ClassUtils; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
class FileUploadHandler implements TypeMapper, ArgumentInjector { | ||
|
||
public static final GraphQLScalarType FILE_UPLOAD_SCALAR = GraphQLScalarType.newScalar() | ||
.name("FileUpload") | ||
.description("An apollo upload compatible scalar for multipart uploads") | ||
.coercing(new Coercing<MultipartFile, Void>() { | ||
|
||
@Override | ||
public Void serialize(Object dataFetcherResult) throws CoercingSerializeException { | ||
throw new CoercingSerializeException("Upload is not a return type"); | ||
} | ||
|
||
@Override | ||
public MultipartFile parseValue(Object input) throws CoercingParseValueException { | ||
if (input instanceof MultipartFile) { | ||
return (MultipartFile) input; | ||
} | ||
throw new CoercingParseValueException("Expected the input to be parsed by the servlet controller"); | ||
} | ||
|
||
@Override | ||
public MultipartFile parseLiteral(Object input) throws CoercingParseLiteralException { | ||
throw new CoercingParseLiteralException("Parsing the literal of the upload is not supported"); | ||
} | ||
}) | ||
.build(); | ||
|
||
@Override | ||
public GraphQLInputType toGraphQLInputType(AnnotatedType javaType, Set<Class<? extends TypeMapper>> mappersToSkip, TypeMappingEnvironment env) { | ||
return FILE_UPLOAD_SCALAR; | ||
} | ||
|
||
@Override | ||
public GraphQLOutputType toGraphQLType(AnnotatedType javaType, Set<Class<? extends TypeMapper>> mappersToSkip, TypeMappingEnvironment env) { | ||
throw new UnsupportedOperationException("FileUpload is not an output type"); | ||
} | ||
|
||
@Override | ||
public boolean supports(AnnotatedElement element, AnnotatedType type) { | ||
return type != null && ClassUtils.isAssignable(MultipartFile.class, type.getType()); | ||
} | ||
|
||
@Override | ||
public Object getArgumentValue(ArgumentInjectorParams params) { | ||
if ((params.getInput() instanceof MultipartFile)) { | ||
return params.getInput(); | ||
} | ||
if (!(params.getInput() instanceof Collection)) { | ||
return null; | ||
} | ||
if (ClassUtils.isAssignable(params.getType().getType(), params.getInput().getClass())) { | ||
return params.getInput(); | ||
} | ||
if (ClassUtils.isAssignable(List.class, params.getType().getType())) { | ||
//noinspection rawtypes,unchecked | ||
return new ArrayList((Collection) params.getInput()); | ||
} | ||
if (ClassUtils.isAssignable(Set.class, params.getType().getType())) { | ||
//noinspection rawtypes,unchecked | ||
return new LinkedHashSet((Collection) params.getInput()); | ||
} | ||
throw new UnsupportedOperationException("Cannot convert " + params.getInput().getClass() + " to " + params.getType()); | ||
} | ||
|
||
@Override | ||
public boolean supports(AnnotatedType type, Parameter parameter) { | ||
return supports(null, type) | ||
|| ClassUtils.isAssignable(Iterable.class, type.getType()) | ||
&& supports(null, GenericTypeReflector.getTypeParameter(type, Iterable.class.getTypeParameters()[0])); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.