Search in sources :

Example 16 with DataFetcherResult

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

the class BatchResultTransformerTest method defaultBatchTransformerTwoBatchResults.

@Test
public void defaultBatchTransformerTwoBatchResults() {
    batchResults.put("field1", "value1");
    batchResults.put("field2", "value2");
    final DataFetchingEnvironment field1Fetcher = DataFetchingEnvironmentImpl.newDataFetchingEnvironment().mergedField(newMergedField().addField(newField("field1").build()).build()).build();
    final DataFetchingEnvironment field2Fetcher = DataFetchingEnvironmentImpl.newDataFetchingEnvironment().mergedField(newMergedField().addField(newField("field2").build()).build()).build();
    environments.addAll(Arrays.asList(field1Fetcher, field2Fetcher));
    dataFetcherResult = DataFetcherResult.<Map<String, Object>>newResult().data(batchResults).build();
    final List<DataFetcherResult<Object>> results = batchResultTransformer.toBatchResult(dataFetcherResult, environments);
    assertThat(results).hasSize(2).extracting(DataFetcherResult::getData).containsOnly("value1", "value2");
    assertThat(results).flatExtracting(DataFetcherResult::getErrors).isEmpty();
}
Also used : DataFetcherResult(graphql.execution.DataFetcherResult) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) Test(org.junit.Test)

Example 17 with DataFetcherResult

use of graphql.execution.DataFetcherResult 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 18 with DataFetcherResult

use of graphql.execution.DataFetcherResult 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 19 with DataFetcherResult

use of graphql.execution.DataFetcherResult 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 20 with DataFetcherResult

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

the class RestDataFetcherTest method canExecuteRequest.

@SuppressWarnings("unchecked")
@Test
public void canExecuteRequest() {
    QueryExecutor queryExecutor = (executionInput, context) -> {
        assertThat(executionInput).isNotNull();
        // assertThat(executionInput.getQuery()).isNotNull();
        assertThat(executionInput.getOperationName()).isNotNull();
        assertThat(executionInput.getVariables()).isNotNull();
        Document document = context.get(Document.class);
        DataFetchingEnvironment dfe = context.get(DataFetchingEnvironment.class);
        assertThat(document).isNotNull();
        assertThat(dfe).isNotNull();
        assertThat(dfe).isEqualTo(dataFetchingEnvironment);
        Map<String, Object> responseMap = ImmutableMap.of("data", ImmutableMap.of("topLevelField", ImmutableMap.of("subField1", "stringVal1", "subField2", "stringVal2")));
        return CompletableFuture.completedFuture(responseMap);
    };
    TestServiceProvider testServiceProvider = TestServiceProvider.newBuilder().queryFunction(queryExecutor).serviceType(ServiceType.REST).build();
    ServiceMetadata serviceMetadata = mock(XtextGraph.class);
    when(serviceMetadata.getServiceProvider()).thenReturn(testServiceProvider);
    RestDataFetcher restDataFetcher = new RestDataFetcher(serviceMetadata);
    ((CompletableFuture<DataFetcherResult<Map<String, Object>>>) restDataFetcher.get(dataFetchingEnvironment)).whenComplete((dataFetcherResult, throwable) -> {
        assertThat(throwable).isNull();
        assertThat(dataFetcherResult).isNotNull();
        Map<String, Object> data = (Map<String, Object>) dataFetcherResult.getData();
        AssertionsForInterfaceTypes.assertThat(data).containsOnlyKeys("data");
        AssertionsForInterfaceTypes.assertThat((Map<String, Object>) data.get("topLevelField")).containsOnlyKeys("subField1", "subField2");
    });
}
Also used : OperationDefinition(graphql.language.OperationDefinition) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) TestServiceProvider(com.intuit.graphql.orchestrator.TestServiceProvider) Operation(graphql.language.OperationDefinition.Operation) ImmutableMap(com.google.common.collect.ImmutableMap) MergedField(graphql.execution.MergedField) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Field(graphql.language.Field) ServiceMetadata(com.intuit.graphql.orchestrator.schema.ServiceMetadata) Document(graphql.language.Document) AssertionsForInterfaceTypes(org.assertj.core.api.AssertionsForInterfaceTypes) GraphQLContext(graphql.GraphQLContext) DataFetchingEnvironmentImpl.newDataFetchingEnvironment(graphql.schema.DataFetchingEnvironmentImpl.newDataFetchingEnvironment) Map(java.util.Map) DataFetcherResult(graphql.execution.DataFetcherResult) AssertionsForClassTypes.assertThat(org.assertj.core.api.AssertionsForClassTypes.assertThat) ServiceType(com.intuit.graphql.orchestrator.ServiceProvider.ServiceType) QueryExecutor(com.intuit.graphql.orchestrator.batch.QueryExecutor) XtextGraph(com.intuit.graphql.orchestrator.xtext.XtextGraph) Mockito.mock(org.mockito.Mockito.mock) SelectionSet(graphql.language.SelectionSet) Before(org.junit.Before) CompletableFuture(java.util.concurrent.CompletableFuture) QueryExecutor(com.intuit.graphql.orchestrator.batch.QueryExecutor) TestServiceProvider(com.intuit.graphql.orchestrator.TestServiceProvider) Document(graphql.language.Document) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) DataFetchingEnvironmentImpl.newDataFetchingEnvironment(graphql.schema.DataFetchingEnvironmentImpl.newDataFetchingEnvironment) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) ServiceMetadata(com.intuit.graphql.orchestrator.schema.ServiceMetadata) Test(org.junit.Test)

Aggregations

DataFetcherResult (graphql.execution.DataFetcherResult)35 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)18 NotNull (org.jetbrains.annotations.NotNull)17 EntityNotFoundGraphQLError (jetbrains.buildServer.server.graphql.util.EntityNotFoundGraphQLError)16 Used (jetbrains.buildServer.Used)14 Map (java.util.Map)12 OperationFailedGraphQLError (jetbrains.buildServer.server.graphql.util.OperationFailedGraphQLError)12 Test (org.junit.Test)12 HashMap (java.util.HashMap)10 ExecutionStepInfo (graphql.execution.ExecutionStepInfo)8 List (java.util.List)8 Collectors (java.util.stream.Collectors)8 ArrayList (java.util.ArrayList)7 Collections (java.util.Collections)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 jetbrains.buildServer.server.graphql.model.mutation.agentPool (jetbrains.buildServer.server.graphql.model.mutation.agentPool)6 GraphQLError (graphql.GraphQLError)5 MergedField (graphql.execution.MergedField)5 Field (graphql.language.Field)5 OperationDefinition (graphql.language.OperationDefinition)5