use of graphql.execution.ExecutionStepInfo in project dotwebstack-framework by dotwebstack.
the class BackendDataFetcherTest method get_returnsFluxMap_ifSourceNull_SubscriptionTrue.
@Test
void get_returnsFluxMap_ifSourceNull_SubscriptionTrue() {
var requestContext = RequestContext.builder().objectField(mock(ObjectField.class)).source(null).build();
when(requestFactory.createRequestContext(environment)).thenReturn(requestContext);
when(environment.getSource()).thenReturn(null);
graphql.language.Field fieldMock = new Field("aaa");
lenient().when(environment.getField()).thenReturn(fieldMock);
mockOperationDefinition(OperationDefinition.Operation.SUBSCRIPTION);
var collectionRequest = mock(CollectionRequest.class);
var selectionSet = mock(DataFetchingFieldSelectionSet.class);
when(environment.getSelectionSet()).thenReturn(selectionSet);
var executionStepInfo = mockExecutionStepInfoWithResultPath("bbb", "bbb");
lenient().when(requestFactory.createCollectionRequest(eq(executionStepInfo), eq(selectionSet))).thenReturn(collectionRequest);
Map<String, Object> data = new HashMap<>();
data.put("aa", new String[] { "a", "b" });
when(backendLoader.loadMany(any(CollectionRequest.class), any(RequestContext.class))).thenReturn(Flux.just(data));
mockGraphQlFieldDefinition(Map.of());
var result = ((Flux<?>) backendDataFetcher.get(environment)).blockFirst();
assertThat(result, CoreMatchers.is(notNullValue()));
assertTrue(result instanceof Map);
assertThat(((Map<?, ?>) result).get("aa"), is(data.get("aa")));
verify(requestFactory).createCollectionRequest(any(ExecutionStepInfo.class), any(DataFetchingFieldSelectionSet.class));
verify(backendLoader).loadMany(any(CollectionRequest.class), any(RequestContext.class));
}
use of graphql.execution.ExecutionStepInfo in project smallrye-graphql by smallrye.
the class MockDataFetchEnvironment method myFastQueryDfe.
public static DataFetchingEnvironment myFastQueryDfe(String typeName, String fieldName, String operationName, String executionId) {
GraphQLNamedType query = mock(GraphQLNamedType.class);
when(query.getName()).thenReturn(typeName);
Field field = mock(Field.class);
when(field.getName()).thenReturn(fieldName);
OperationDefinition operationDefinition = mock(OperationDefinition.class);
when(operationDefinition.getName()).thenReturn(operationName);
ResultPath executionPath = mock(ResultPath.class);
when(executionPath.toString()).thenReturn("/" + typeName + "/" + fieldName);
ExecutionStepInfo executionStepInfo = mock(ExecutionStepInfo.class);
when(executionStepInfo.getPath()).thenReturn(executionPath);
DataFetchingEnvironment dfe = mock(DataFetchingEnvironment.class);
when(dfe.getParentType()).thenReturn(query);
when(dfe.getField()).thenReturn(field);
when(dfe.getOperationDefinition()).thenReturn(operationDefinition);
when(dfe.getExecutionStepInfo()).thenReturn(executionStepInfo);
when(dfe.getExecutionId()).thenReturn(ExecutionId.from(executionId));
return dfe;
}
use of graphql.execution.ExecutionStepInfo in project graphql-java by graphql-java.
the class ResultNodesUtil method toDataImpl.
private static ExecutionResultData toDataImpl(ExecutionResultNode root) {
if (root instanceof LeafExecutionResultNode) {
return root.getResolvedValue().isNullValue() ? data(null, root) : data(((LeafExecutionResultNode) root).getValue(), root);
}
if (root instanceof ListExecutionResultNode) {
Optional<NonNullableFieldWasNullException> childNonNullableException = root.getChildNonNullableException();
if (childNonNullableException.isPresent()) {
return data(null, childNonNullableException.get());
}
List<GraphQLError> errors = new ArrayList<>();
List<Object> data = new ArrayList<>();
for (ExecutionResultNode child : root.getChildren()) {
ExecutionResultData erd = toDataImpl(child);
data.add(erd.data);
if (!erd.errors.isEmpty()) {
errors.addAll(erd.errors);
}
}
if (!root.getErrors().isEmpty()) {
errors.addAll(root.getErrors());
}
return data(data, errors);
}
if (root instanceof UnresolvedObjectResultNode) {
ExecutionStepInfo executionStepInfo = root.getExecutionStepInfo();
return data("Not resolved : " + executionStepInfo.getPath() + " with field " + executionStepInfo.getField(), emptyList());
}
if (root instanceof ObjectExecutionResultNode) {
Optional<NonNullableFieldWasNullException> childrenNonNullableException = root.getChildNonNullableException();
if (childrenNonNullableException.isPresent()) {
return data(null, childrenNonNullableException.get());
}
Map<String, Object> resultMap = new LinkedHashMap<>();
List<GraphQLError> errors = new ArrayList<>();
root.getChildren().forEach(child -> {
ExecutionResultData executionResultData = toDataImpl(child);
resultMap.put(child.getMergedField().getResultKey(), executionResultData.data);
errors.addAll(executionResultData.errors);
});
errors.addAll(root.getErrors());
return data(resultMap, errors);
}
return Assert.assertShouldNeverHappen("An unexpected root type %s", root.getClass());
}
use of graphql.execution.ExecutionStepInfo in project graphql-java by graphql-java.
the class ValueFetcher method fetchBatchedValues.
public CompletableFuture<List<FetchedValue>> fetchBatchedValues(ExecutionContext executionContext, List<Object> sources, MergedField field, List<ExecutionStepInfo> executionInfos) {
ExecutionStepInfo executionStepInfo = executionInfos.get(0);
// TODO - add support for field context to batching code
Object todoLocalContext = null;
if (isDataFetcherBatched(executionContext, executionStepInfo)) {
return fetchValue(executionContext, sources, todoLocalContext, field, executionStepInfo).thenApply(fetchedValue -> extractBatchedValues(fetchedValue, sources.size()));
} else {
List<CompletableFuture<FetchedValue>> fetchedValues = new ArrayList<>();
for (int i = 0; i < sources.size(); i++) {
fetchedValues.add(fetchValue(executionContext, sources.get(i), todoLocalContext, field, executionInfos.get(i)));
}
return Async.each(fetchedValues);
}
}
use of graphql.execution.ExecutionStepInfo in project graphql-java by graphql-java.
the class ValueFetcher method fetchValue.
public CompletableFuture<FetchedValue> fetchValue(ExecutionContext executionContext, Object source, Object localContext, MergedField sameFields, ExecutionStepInfo executionInfo) {
Field field = sameFields.getSingleField();
GraphQLFieldDefinition fieldDef = executionInfo.getFieldDefinition();
GraphQLCodeRegistry codeRegistry = executionContext.getGraphQLSchema().getCodeRegistry();
GraphQLFieldsContainer parentType = getFieldsContainer(executionInfo);
Supplier<Map<String, Object>> argumentValues = FpKit.intraThreadMemoize(() -> valuesResolver.getArgumentValues(codeRegistry, fieldDef.getArguments(), field.getArguments(), executionContext.getVariables()));
QueryDirectivesImpl queryDirectives = new QueryDirectivesImpl(sameFields, executionContext.getGraphQLSchema(), executionContext.getVariables());
GraphQLOutputType fieldType = fieldDef.getType();
Supplier<ExecutableNormalizedOperation> normalizedQuery = executionContext.getNormalizedQueryTree();
Supplier<ExecutableNormalizedField> normalisedField = () -> normalizedQuery.get().getNormalizedField(sameFields, executionInfo.getObjectType(), executionInfo.getPath());
DataFetchingFieldSelectionSet selectionSet = DataFetchingFieldSelectionSetImpl.newCollector(executionContext.getGraphQLSchema(), fieldType, normalisedField);
DataFetchingEnvironment environment = newDataFetchingEnvironment(executionContext).source(source).localContext(localContext).arguments(argumentValues).fieldDefinition(fieldDef).mergedField(sameFields).fieldType(fieldType).executionStepInfo(executionInfo).parentType(parentType).selectionSet(selectionSet).queryDirectives(queryDirectives).build();
ExecutionId executionId = executionContext.getExecutionId();
ResultPath path = executionInfo.getPath();
return callDataFetcher(codeRegistry, parentType, fieldDef, environment, executionId, path).thenApply(rawFetchedValue -> FetchedValue.newFetchedValue().fetchedValue(rawFetchedValue).rawFetchedValue(rawFetchedValue).build()).exceptionally(exception -> handleExceptionWhileFetching(field, path, exception)).thenApply(result -> unboxPossibleDataFetcherResult(sameFields, path, result, localContext)).thenApply(this::unboxPossibleOptional);
}
Aggregations