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();
}
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");
}
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);
}
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();
}
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");
});
}
Aggregations