Search in sources :

Example 16 with ExecutionStepInfo

use of graphql.execution.ExecutionStepInfo in project graphql-orchestrator-java by intuit.

the class SubtreeBatchResultTransformerTest method producesPartitionedResult.

@Test
public void producesPartitionedResult() {
    Map<String, Object> data = new HashMap<>();
    data.put("consumer", new HashMap<String, String>() {

        {
            put("finance", "test");
            put("shouldBeIgnored", null);
        }
    });
    final ExecutionStepInfo executionStepInfo = buildCompleteExecutionStepInfo(document, "consumer", "finance");
    DataFetchingEnvironment dataFetchingEnvironment = newDataFetchingEnvironment().mergedField(executionStepInfo.getField()).executionStepInfo(executionStepInfo).build();
    DataFetcherResult<Map<String, Object>> dataFetcherResult = DataFetcherResult.<Map<String, Object>>newResult().data(data).build();
    final List<DataFetcherResult<Object>> results = new SubtreeBatchResultTransformer().toBatchResult(dataFetcherResult, Collections.singletonList(dataFetchingEnvironment));
    assertThat(results).hasSize(1).extracting(DataFetcherResult::getData).containsExactly("test");
}
Also used : DataFetcherResult(graphql.execution.DataFetcherResult) HashMap(java.util.HashMap) ExecutionStepInfo(graphql.execution.ExecutionStepInfo) GraphQLTestUtil.buildCompleteExecutionStepInfo(com.intuit.graphql.orchestrator.batch.GraphQLTestUtil.buildCompleteExecutionStepInfo) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) DataFetchingEnvironmentImpl.newDataFetchingEnvironment(graphql.schema.DataFetchingEnvironmentImpl.newDataFetchingEnvironment) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 17 with ExecutionStepInfo

use of graphql.execution.ExecutionStepInfo in project graphql-orchestrator-java by intuit.

the class SubtreeBatchResultTransformerTest method addErrorsToPartitionedResult.

@Test
public void addErrorsToPartitionedResult() {
    Map<String, Object> data = new HashMap<>();
    data.put("consumer", new HashMap<String, String>() {

        {
            put("finance", "test");
            put("experiences", "test");
            put("financialProfile", "test");
            put("shouldBeIgnored", null);
        }
    });
    List<GraphQLError> errors = new ArrayList<GraphQLError>() {

        {
            add(GraphqlErrorBuilder.newError().message("Exception while fetching data (/consumer/shouldBeIgnored)").build());
        }
    };
    final ExecutionStepInfo executionStepInfo1 = buildCompleteExecutionStepInfo(document, "consumer", "finance");
    final ExecutionStepInfo executionStepInfo2 = buildCompleteExecutionStepInfo(document, "consumer", "experiences");
    final ExecutionStepInfo executionStepInfo3 = buildCompleteExecutionStepInfo(document, "consumer", "financialProfile");
    DataFetchingEnvironment dfe1 = newDataFetchingEnvironment().mergedField(executionStepInfo1.getField()).executionStepInfo(executionStepInfo1).build();
    DataFetchingEnvironment dfe2 = newDataFetchingEnvironment().mergedField(executionStepInfo2.getField()).executionStepInfo(executionStepInfo2).build();
    DataFetchingEnvironment dfe3 = newDataFetchingEnvironment().mergedField(executionStepInfo3.getField()).executionStepInfo(executionStepInfo3).build();
    DataFetcherResult<Map<String, Object>> dataFetcherResult = DataFetcherResult.<Map<String, Object>>newResult().data(data).errors(errors).build();
    final List<DataFetcherResult<Object>> results = new SubtreeBatchResultTransformer().toBatchResult(dataFetcherResult, new ArrayList<>(Arrays.asList(dfe1, dfe2, dfe3)));
    assertThat(results).hasSize(3).extracting(DataFetcherResult::getData).containsExactly("test", "test", "test");
    assertThat(results.stream().flatMap(result -> result.getErrors().stream()).collect(Collectors.toList())).hasSize(1);
}
Also used : DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) Test(org.junit.Test) GraphqlErrorBuilder(graphql.GraphqlErrorBuilder) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Document(graphql.language.Document) ExecutionStepInfo(graphql.execution.ExecutionStepInfo) List(java.util.List) GraphQLError(graphql.GraphQLError) DataFetchingEnvironmentImpl.newDataFetchingEnvironment(graphql.schema.DataFetchingEnvironmentImpl.newDataFetchingEnvironment) Map(java.util.Map) TestHelper.document(com.intuit.graphql.orchestrator.TestHelper.document) DataFetcherResult(graphql.execution.DataFetcherResult) GraphQLTestUtil.buildCompleteExecutionStepInfo(com.intuit.graphql.orchestrator.batch.GraphQLTestUtil.buildCompleteExecutionStepInfo) Collections(java.util.Collections) Before(org.junit.Before) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) DataFetchingEnvironmentImpl.newDataFetchingEnvironment(graphql.schema.DataFetchingEnvironmentImpl.newDataFetchingEnvironment) DataFetcherResult(graphql.execution.DataFetcherResult) ExecutionStepInfo(graphql.execution.ExecutionStepInfo) GraphQLTestUtil.buildCompleteExecutionStepInfo(com.intuit.graphql.orchestrator.batch.GraphQLTestUtil.buildCompleteExecutionStepInfo) GraphQLError(graphql.GraphQLError) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 18 with ExecutionStepInfo

use of graphql.execution.ExecutionStepInfo in project graphql-orchestrator-java by intuit.

the class SubtreeBatchResultTransformerTest method nullDataPathReturnsNull.

@Test
public void nullDataPathReturnsNull() {
    Map<String, Object> data = new HashMap<>();
    data.put("consumer", new HashMap<String, String>() {

        {
            put("finance", null);
        }
    });
    final ExecutionStepInfo executionStepInfo = buildCompleteExecutionStepInfo(document, "consumer", "finance");
    DataFetchingEnvironment dataFetchingEnvironment = newDataFetchingEnvironment().mergedField(executionStepInfo.getField()).executionStepInfo(executionStepInfo).build();
    DataFetcherResult<Map<String, Object>> dataFetcherResult = DataFetcherResult.<Map<String, Object>>newResult().data(data).build();
    final List<DataFetcherResult<Object>> results = new SubtreeBatchResultTransformer().toBatchResult(dataFetcherResult, Collections.singletonList(dataFetchingEnvironment));
    assertThat(results).hasSize(1);
    assertThat(results.get(0).getData()).isNull();
}
Also used : DataFetcherResult(graphql.execution.DataFetcherResult) HashMap(java.util.HashMap) ExecutionStepInfo(graphql.execution.ExecutionStepInfo) GraphQLTestUtil.buildCompleteExecutionStepInfo(com.intuit.graphql.orchestrator.batch.GraphQLTestUtil.buildCompleteExecutionStepInfo) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) DataFetchingEnvironmentImpl.newDataFetchingEnvironment(graphql.schema.DataFetchingEnvironmentImpl.newDataFetchingEnvironment) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 19 with ExecutionStepInfo

use of graphql.execution.ExecutionStepInfo in project graphql-orchestrator-java by intuit.

the class ResolverArgumentDataFetcherHelper method callBatchLoaderWithArguments.

/**
 * Modifies the provided DataFetchingEnvironment by attaching arguments, variable definitions, and variable references
 * to objects in the environment that need them.
 *
 * Rebuilding the DataFetchingEnvironment relies on APIs that are marked with @Internal in GraphQL-Java. The stability
 * of this code depends on changes of the DataFetchingEnvironmentImpl.
 *
 * Note that only the ExecutionStepInfo is used to build the query in the QueryExecutorBatchLoader, but a majority of
 * the fields must change to achieve consistency for the most commonly used getters (field, mergedField, etc.)
 *
 * Below are the fields in the DataFetchingEnvironment that are changed:
 *
 * executionStepInfo: QueryExecutorBatchLoader extracts field information from the root ExecutionStepInfo. Arguments
 * must be injected into each level.
 *
 * arguments: Add argument data
 *
 * mergedField: Add list of arguments to the mergedField
 *
 * operationDefinition: Adds a new list of variable definitions generated by this method to the operationDefinition
 *
 * @param env a data fetching environment that will accept arguments
 * @param resolvedArgumentData argument data that should be bound to the data fetching environment
 * @return A DataFetcherResult with the value
 */
public CompletableFuture<DataFetcherResult<Object>> callBatchLoaderWithArguments(DataFetchingEnvironment env, Map<ResolverArgumentDirective, Object> resolvedArgumentData) {
    Map<String, Object> variables = new HashMap<>();
    List<VariableDefinition> variableDefinitions = new ArrayList<>();
    List<Argument> fieldArguments = new ArrayList<>();
    int uniqueIdentifier = 0;
    for (final Entry<ResolverArgumentDirective, Object> entry : resolvedArgumentData.entrySet()) {
        final ResolverArgumentDirective resolverArgumentDirective = entry.getKey();
        final Object argumentData = entry.getValue();
        // add inc to make argument globally unique
        String variableBinding = String.format(VARIABLE_REFERENCE_FORMAT, resolverArgumentDirective.getArgumentName(), uniqueIdentifier++);
        variables.put(variableBinding, argumentData);
        variableDefinitions.add(VariableDefinition.newVariableDefinition().name(variableBinding).type(createTypeBasedOnGraphQLType(resolverArgumentDirective.getInputType())).build());
        fieldArguments.add(Argument.newArgument().name(resolverArgumentDirective.getArgumentName()).value(VariableReference.newVariableReference().name(variableBinding).build()).build());
    }
    final OperationDefinition queryOperation = env.getOperationDefinition();
    final MergedField newMergedField = env.getMergedField().transform(builder -> builder.fields(singletonList(env.getField().transform(fieldBuilder -> fieldBuilder.arguments(fieldArguments)))));
    final OperationDefinition opDefWithVariables = queryOperation.transform(builder -> builder.variableDefinitions(variableDefinitions));
    Map<String, Object> dfeArguments = resolvedArgumentData.entrySet().stream().collect(Collectors.toMap(entry -> entry.getKey().getArgumentName(), Entry::getValue));
    DataFetchingEnvironment dfeWithArgs = DataFetchingEnvironmentImpl.newDataFetchingEnvironment(env).operationDefinition(opDefWithVariables).executionStepInfo(executionStepInfoModifier.addArgumentsToExecutionStepInfo(env, fieldArguments)).mergedField(newMergedField).variables(variables).arguments(dfeArguments).build();
    return dfeWithArgs.<DataFetchingEnvironment, DataFetcherResult<Object>>getDataLoader(namespace).load(dfeWithArgs);
}
Also used : OperationDefinition(graphql.language.OperationDefinition) GraphQLObjectType(graphql.schema.GraphQLObjectType) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) GraphQLUtil.createTypeBasedOnGraphQLType(com.intuit.graphql.orchestrator.utils.GraphQLUtil.createTypeBasedOnGraphQLType) VisibleForTesting(graphql.VisibleForTesting) QueryTransformer(graphql.analysis.QueryTransformer) MergedField(graphql.execution.MergedField) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Field(graphql.language.Field) ResolverArgumentDirective(com.intuit.graphql.orchestrator.resolverdirective.ResolverArgumentDirective) Collectors(java.util.stream.Collectors) DataFetchingEnvironmentImpl(graphql.schema.DataFetchingEnvironmentImpl) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) Argument(graphql.language.Argument) ExecutionStepInfo(graphql.execution.ExecutionStepInfo) List(java.util.List) VariableDefinition(graphql.language.VariableDefinition) VariableReference(graphql.language.VariableReference) Map(java.util.Map) Entry(java.util.Map.Entry) Optional(java.util.Optional) DataFetcherResult(graphql.execution.DataFetcherResult) VariableDefinition(graphql.language.VariableDefinition) Argument(graphql.language.Argument) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) ResolverArgumentDirective(com.intuit.graphql.orchestrator.resolverdirective.ResolverArgumentDirective) MergedField(graphql.execution.MergedField) OperationDefinition(graphql.language.OperationDefinition)

Example 20 with ExecutionStepInfo

use of graphql.execution.ExecutionStepInfo in project graphql-orchestrator-java by intuit.

the class SubtreeBatchResultTransformer method buildDataFetchingPath.

private Stack<String> buildDataFetchingPath(ExecutionStepInfo leafInfo) {
    Stack<String> hierarchy = new Stack<>();
    ExecutionStepInfo curr = leafInfo;
    // ignore Query/Mutation
    while (curr != null && curr.getPath().getLevel() != 0) {
        final Field field = curr.getField().getSingleField();
        hierarchy.push(field.getAlias() != null ? field.getAlias() : field.getName());
        curr = curr.getParent();
    }
    return hierarchy;
}
Also used : Field(graphql.language.Field) ExecutionStepInfo(graphql.execution.ExecutionStepInfo) Stack(java.util.Stack)

Aggregations

ExecutionStepInfo (graphql.execution.ExecutionStepInfo)47 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)28 HashMap (java.util.HashMap)25 Field (graphql.language.Field)24 MergedField (graphql.execution.MergedField)23 Map (java.util.Map)23 Test (org.junit.Test)23 DataFetchingEnvironmentImpl.newDataFetchingEnvironment (graphql.schema.DataFetchingEnvironmentImpl.newDataFetchingEnvironment)22 CompletableFuture (java.util.concurrent.CompletableFuture)22 OperationDefinition (graphql.language.OperationDefinition)19 GraphQLObjectType (graphql.schema.GraphQLObjectType)19 Collections (java.util.Collections)18 Argument (graphql.language.Argument)16 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)16 Before (org.junit.Before)16 Scalars (graphql.Scalars)15 ExecutionPath (graphql.execution.ExecutionPath)15 MergedField.newMergedField (graphql.execution.MergedField.newMergedField)15 FragmentDefinition (graphql.language.FragmentDefinition)15 GraphQLSchema (graphql.schema.GraphQLSchema)15