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

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

package graphql.kickstart.servlet;

import graphql.GraphQLException;
import graphql.kickstart.execution.GraphQLObjectMapper;
import graphql.kickstart.execution.GraphQLRequest;
import graphql.kickstart.execution.context.ContextSetting;
import graphql.kickstart.execution.input.GraphQLInvocationInput;
import graphql.kickstart.servlet.input.GraphQLInvocationInputFactory;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;

@Slf4j
class GraphQLGetInvocationInputParser extends AbstractGraphQLInvocationInputParser {

  GraphQLGetInvocationInputParser(
      GraphQLInvocationInputFactory invocationInputFactory,
      GraphQLObjectMapper graphQLObjectMapper,
      ContextSetting contextSetting) {
    super(invocationInputFactory, graphQLObjectMapper, contextSetting);
  }

  public GraphQLInvocationInput getGraphQLInvocationInput(
      HttpServletRequest request, HttpServletResponse response) throws IOException {
    if (isIntrospectionQuery(request)) {
      GraphQLRequest graphqlRequest = GraphQLRequest.createIntrospectionRequest();
      return invocationInputFactory.create(graphqlRequest, request, response);
    }

    String query = request.getParameter("query");
    if (query == null) {
      throw new GraphQLException("Query parameter not found in GET request");
    }

    if (isSingleQuery(query)) {
      Map variables = getVariables(request);
      Map extensions = getExtensions(request);
      String operationName = request.getParameter("operationName");
      GraphQLRequest graphqlRequest =
          new GraphQLRequest(query, variables, extensions, operationName);
      return invocationInputFactory.createReadOnly(graphqlRequest, request, response);
    }

    List graphqlRequests = graphQLObjectMapper.readBatchedGraphQLRequest(query);
    return invocationInputFactory.createReadOnly(
        contextSetting, graphqlRequests, request, response);
  }

  private boolean isIntrospectionQuery(HttpServletRequest request) {
    String path =
        Optional.ofNullable(request.getPathInfo()).orElseGet(request::getServletPath).toLowerCase();
    return path.contentEquals("/schema.json");
  }

  private Map getVariables(HttpServletRequest request) {
    return Optional.ofNullable(request.getParameter("variables"))
        .map(graphQLObjectMapper::deserializeVariables)
        .map(HashMap::new)
        .orElseGet(HashMap::new);
  }

  private Map getExtensions(HttpServletRequest request) {
    return Optional.ofNullable(request.getParameter("extensions"))
        .map(graphQLObjectMapper::deserializeExtensions)
        .map(HashMap::new)
        .orElseGet(HashMap::new);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy