Search in sources :

Example 1 with Post

use of io.micronaut.http.annotation.Post in project micronaut-graphql by micronaut-projects.

the class GraphQLController method post.

/**
 * Handles GraphQL {@code POST} requests.
 *
 * @param query         the GraphQL query
 * @param operationName the GraphQL operation name
 * @param variables     the GraphQL variables
 * @param body          the GraphQL request body
 * @param httpRequest   the HTTP request
 * @return the GraphQL response
 */
@Post(consumes = ALL, produces = APPLICATION_JSON, single = true)
public Publisher<MutableHttpResponse<String>> post(@Nullable @QueryValue("query") String query, @Nullable @QueryValue("operationName") String operationName, @Nullable @QueryValue("variables") String variables, @Nullable @Body String body, HttpRequest httpRequest) {
    Optional<MediaType> opt = httpRequest.getContentType();
    MediaType contentType = opt.orElse(null);
    if (body == null) {
        body = "";
    }
    if (APPLICATION_JSON_TYPE.equals(contentType)) {
        GraphQLRequestBody request = graphQLJsonSerializer.deserialize(body, GraphQLRequestBody.class);
        if (request.getQuery() == null) {
            request.setQuery("");
        }
        return executeRequest(request.getQuery(), request.getOperationName(), request.getVariables(), httpRequest);
    }
    if (query != null) {
        return executeRequest(query, operationName, convertVariablesJson(variables), httpRequest);
    }
    if (APPLICATION_GRAPHQL_TYPE.equals(contentType)) {
        return executeRequest(body, null, null, httpRequest);
    }
    throw new HttpStatusException(UNPROCESSABLE_ENTITY, "Could not process GraphQL request");
}
Also used : MediaType(io.micronaut.http.MediaType) HttpStatusException(io.micronaut.http.exceptions.HttpStatusException) Post(io.micronaut.http.annotation.Post)

Aggregations

MediaType (io.micronaut.http.MediaType)1 Post (io.micronaut.http.annotation.Post)1 HttpStatusException (io.micronaut.http.exceptions.HttpStatusException)1