All Downloads are FREE. Search and download functionalities are using the official Maven repository.

graphql.kickstart.servlet.SingleQueryResponseWriter Maven / Gradle / Ivy

There is a newer version: 16.0.0
Show newest version
package graphql.kickstart.servlet;

import graphql.ExecutionResult;
import graphql.kickstart.execution.GraphQLObjectMapper;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
class SingleQueryResponseWriter implements QueryResponseWriter {

  private final ExecutionResult result;
  private final GraphQLObjectMapper graphQLObjectMapper;

  @Override
  public void write(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setContentType(HttpRequestHandler.APPLICATION_JSON_UTF8);
    response.setStatus(HttpRequestHandler.STATUS_OK);
    response.setCharacterEncoding(StandardCharsets.UTF_8.name());

    byte[] contentBytes = graphQLObjectMapper.serializeResultAsBytes(result);
    response.setContentLength(contentBytes.length);
    response.getOutputStream().write(contentBytes);
    response.getOutputStream().flush();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy