Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/main/java/graphql/servlet/GraphQLServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.google.common.io.ByteStreams;
import com.google.common.io.CharStreams;
import graphql.ExecutionInput;
import graphql.ExecutionResult;
import graphql.GraphQL;
Expand Down Expand Up @@ -43,6 +44,7 @@ public abstract class GraphQLServlet extends HttpServlet implements Servlet, Gra
public static final Logger log = LoggerFactory.getLogger(GraphQLServlet.class);

public static final String APPLICATION_JSON_UTF8 = "application/json;charset=UTF-8";
public static final String APPLICATION_GRAPHQL = "application/graphql";
public static final int STATUS_OK = 200;
public static final int STATUS_BAD_REQUEST = 400;

Expand Down Expand Up @@ -114,12 +116,16 @@ public GraphQLServlet(ObjectMapperConfigurer objectMapperConfigurer, List<GraphQ
final Object rootObject = createRootObject(Optional.of(request), Optional.of(response));

try {
if (request.getContentType() != null && request.getContentType().startsWith("multipart/form-data") && !request.getParts().isEmpty()) {
if (APPLICATION_GRAPHQL.equals(request.getContentType())) {
String query = CharStreams.toString(request.getReader());
doQuery(query, null, null, getSchemaProvider().getSchema(request), context, rootObject, request, response);
} else if (request.getContentType() != null && request.getContentType().startsWith("multipart/form-data") && !request.getParts().isEmpty()) {
final Map<String, List<Part>> fileItems = request.getParts().stream()
.collect(Collectors.toMap(
Part::getName,
Collections::singletonList,
(l1, l2) -> Stream.concat(l1.stream(), l2.stream()).collect(Collectors.toList())));

context.setFiles(Optional.of(fileItems));

if (fileItems.containsKey("graphql")) {
Expand Down
14 changes: 14 additions & 0 deletions src/test/groovy/graphql/servlet/GraphQLServletSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,20 @@ class GraphQLServletSpec extends Specification {
getResponseContent().data.echo == "test"
}

def "query over HTTP POST body with graphql contentType returns data"() {
setup:
request.addHeader("Content-Type", "application/graphql")
request.setContent('query { echo(arg:"test") }'.getBytes("UTF-8"))

when:
servlet.doPost(request, response)

then:
response.getStatus() == STATUS_OK
response.getContentType() == CONTENT_TYPE_JSON_UTF8
getResponseContent().data.echo == "test"
}

def "query over HTTP POST body with variables returns data"() {
setup:
request.setContent(mapper.writeValueAsBytes([
Expand Down