Currently only POST
requests are routed: https://github.com/spring-projects-experimental/spring-graphql/blob/d7f3aa6fa9e67905d81aa9a7a51006f4eb283d4a/spring-graphql-web/src/main/java/org/springframework/boot/graphql/WebMvcGraphQLAutoConfiguration.java#L51-L55
It would be nice to support the full spectrum as stated on https://graphql.org/learn/serving-over-http/
Serving over HTTP
HTTP is the most common choice for client-server protocol when using GraphQL because of its ubiquity. Here are some guidelines for setting up a GraphQL server to operate over HTTP.
Web Request Pipeline
Most modern web frameworks use a pipeline model where requests are passed through a stack of middleware (AKA filters/plugins). As the request flows through the pipeline, it can be inspected, transformed, modified, or terminated with a response. GraphQL should be placed after all authentication middleware, so that you have access to the same session and user information you would in your HTTP endpoint handlers.
URIs, Routes
HTTP is commonly associated with REST, which uses "resources" as its core concept. In contrast, GraphQL's conceptual model is an entity graph. As a result, entities in GraphQL are not identified by URLs. Instead, a GraphQL server operates on a single URL/endpoint, usually /graphql, and all GraphQL requests for a given service should be directed at this endpoint.
HTTP Methods, Headers, and Body
Your GraphQL HTTP server should handle the HTTP GET and POST methods.
GET request
When receiving an HTTP GET request, the GraphQL query should be specified in the "query" query string. For example, if we wanted to execute the following GraphQL query:
This request could be sent via an HTTP GET like so:
http://myapi/graphql?query={me{name}}
Query variables can be sent as a JSON-encoded string in an additional query parameter called variables
. If the query contains several named operations, an operationName
query parameter can be used to control which one should be executed.
POST request
A standard GraphQL POST request should use the application/json content type, and include a JSON-encoded body of the following form:
{
"query": "...",
"operationName": "...",
"variables": { "myVariable": "someValue", ... }
}
operationName
and variables
are optional fields. operationName
is only required if multiple operations are present in the query.
In addition to the above, we recommend supporting two additional cases:
If the "query" query string parameter is present (as in the GET example above), it should be parsed and handled in the same way as the HTTP GET case.
If the "application/graphql" Content-Type header is present, treat the HTTP POST body contents as the GraphQL query string.
This is similar as also added to https://github.com/graphql-java/graphql-java-spring via graphql-java/graphql-java-spring#4.
That support all the Serving over HTTP cases already: https://github.com/graphql-java/graphql-java-spring/blob/c6606f624d98b0835e719e0d48ab7223b141cd19/graphql-java-spring-webmvc/src/main/java/graphql/spring/web/servlet/components/GraphQLController.java#L45-L135 (just like Micronaut also does).