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

org.opentripplanner.api.json.GraphQLResponseSerializer Maven / Gradle / Ivy

package org.opentripplanner.api.json;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import graphql.ExecutionResult;
import graphql.execution.AbortExecutionException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This class is responsible for serializing a GraphQL {@link ExecutionResult} into a String, which
 * can be returned as the body of the HTTP response. This differs from the mapper provided by {@link
 * JSONObjectMapperProvider}, by serializing all fields in the objects, including null fields.
 */
public class GraphQLResponseSerializer {

  static final Logger LOG = LoggerFactory.getLogger(GraphQLResponseSerializer.class);

  private static final ObjectMapper objectMapper = new ObjectMapper();

  public static String serialize(ExecutionResult executionResult) {
    try {
      return objectMapper.writeValueAsString(executionResult.toSpecification());
    } catch (JsonProcessingException e) {
      LOG.error("Unable to serialize response", e);
      throw new RuntimeException(e);
    }
  }

  public static String serializeBatch(
    List> queries,
    List> futures
  ) {
    List> responses = new LinkedList<>();
    for (int i = 0; i < queries.size(); i++) {
      ExecutionResult executionResult;
      // Try each request separately, returning both completed and failed responses is ok
      try {
        executionResult = futures.get(i).get();
      } catch (InterruptedException | ExecutionException e) {
        executionResult = new AbortExecutionException(e).toExecutionResult();
      }
      responses.add(
        Map.of("id", queries.get(i).get("id"), "payload", executionResult.toSpecification())
      );
    }

    try {
      return objectMapper.writeValueAsString(responses);
    } catch (JsonProcessingException e) {
      LOG.error("Unable to serialize response", e);
      throw new RuntimeException(e);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy