Skip to content
This repository was archived by the owner on Oct 25, 2021. It is now read-only.

Conversation

@jakubbrodzinski
Copy link

@jakubbrodzinski jakubbrodzinski commented Aug 27, 2019

GraphQL controller that handles multi part requests. When request contains multipart data its being deserialized and then files are put into request via request.getFiles.put(...). The one requirement to make it work is putting files as variable to GraphQL request, like it is suggested everywhere when sending files via GraphQL is mentioned.
Integraton test included.
Example on integrating graphQL with file transfer:

import graphql.schema.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Part;

public class Scalars {
    public static final GraphQLScalarType UpFile =
            GraphQLScalarType.newScalar().name("UpFile")
                    .description("A file part in a multipart request").coercing(new Coercing<MultipartFile, Void>() {
                @Override
                public Void serialize(Object dataFetcherResult) throws CoercingSerializeException {
                    throw new CoercingSerializeException("Upload is an input-only type");
                }

                @Override
                public MultipartFile parseValue(Object input) throws CoercingParseValueException {
                    if (input instanceof MultipartFile) {
                        return (MultipartFile) input;
                    } else if (null == input) {
                        return null;
                    } else {
                        throw new CoercingParseValueException("Expected type " +
                                Part.class.getName() +
                                " but was " +
                                input.getClass().getName());
                    }
                }

                @Override
                public MultipartFile parseLiteral(Object input) throws CoercingParseLiteralException {
                    throw new CoercingParseLiteralException("Must use variables to specify Upload values");
                }
            }).build();
}

@AnkBurov
Copy link

Would be nice to see this feature in the project.

@andimarek andimarek closed this Oct 25, 2021
@andimarek
Copy link
Member

This project is now archived in favor of the official Spring GraphQL integration.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants