Search in sources :

Example 6 with ExecutionStepInfo

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));
}
Also used : CollectionRequest(org.dotwebstack.framework.core.query.model.CollectionRequest) HashMap(java.util.HashMap) DataFetchingFieldSelectionSet(graphql.schema.DataFetchingFieldSelectionSet) GroupedFlux(reactor.core.publisher.GroupedFlux) KeyGroupedFlux(org.dotwebstack.framework.core.datafetchers.KeyGroupedFlux) Flux(reactor.core.publisher.Flux) Field(graphql.language.Field) ObjectField(org.dotwebstack.framework.core.model.ObjectField) MergedField(graphql.execution.MergedField) Field(graphql.language.Field) ExecutionStepInfo(graphql.execution.ExecutionStepInfo) ObjectField(org.dotwebstack.framework.core.model.ObjectField) GraphQLObjectType.newObject(graphql.schema.GraphQLObjectType.newObject) RequestContext(org.dotwebstack.framework.core.query.model.RequestContext) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test)

Example 7 with ExecutionStepInfo

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;
}
Also used : Field(graphql.language.Field) ExecutionStepInfo(graphql.execution.ExecutionStepInfo) ResultPath(graphql.execution.ResultPath) GraphQLNamedType(graphql.schema.GraphQLNamedType) OperationDefinition(graphql.language.OperationDefinition) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment)

Example 8 with ExecutionStepInfo

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());
}
Also used : NonNullableFieldWasNullException(graphql.execution.NonNullableFieldWasNullException) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ExecutionStepInfo(graphql.execution.ExecutionStepInfo) GraphQLError(graphql.GraphQLError)

Example 9 with ExecutionStepInfo

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);
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ExecutionStepInfo(graphql.execution.ExecutionStepInfo) ArrayList(java.util.ArrayList)

Example 10 with ExecutionStepInfo

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);
}
Also used : DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) GraphQLCodeRegistry(graphql.schema.GraphQLCodeRegistry) Internal(graphql.Internal) FetchedValue(graphql.execution.FetchedValue) ValuesResolver(graphql.execution.ValuesResolver) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutionContext(graphql.execution.ExecutionContext) ExecutableNormalizedField(graphql.normalized.ExecutableNormalizedField) Supplier(java.util.function.Supplier) DefaultValueUnboxer(graphql.execution.DefaultValueUnboxer) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) ImmutableKit(graphql.collect.ImmutableKit) ExecutionId(graphql.execution.ExecutionId) ExecutionStepInfo(graphql.execution.ExecutionStepInfo) ImmutableList(com.google.common.collect.ImmutableList) GraphQLError(graphql.GraphQLError) Map(java.util.Map) DataFetcher(graphql.schema.DataFetcher) GraphQLTypeUtil(graphql.schema.GraphQLTypeUtil) DataFetcherResult(graphql.execution.DataFetcherResult) Assert(graphql.Assert) LogKit(graphql.util.LogKit) Async(graphql.execution.Async) Logger(org.slf4j.Logger) ExceptionWhileDataFetching(graphql.ExceptionWhileDataFetching) MergedField(graphql.execution.MergedField) GraphQLOutputType(graphql.schema.GraphQLOutputType) ResultPath(graphql.execution.ResultPath) Field(graphql.language.Field) DataFetchingFieldSelectionSetImpl(graphql.schema.DataFetchingFieldSelectionSetImpl) ExecutableNormalizedOperation(graphql.normalized.ExecutableNormalizedOperation) List(java.util.List) QueryDirectivesImpl(graphql.execution.directives.QueryDirectivesImpl) CompletionStage(java.util.concurrent.CompletionStage) DataFetchingFieldSelectionSet(graphql.schema.DataFetchingFieldSelectionSet) FpKit(graphql.util.FpKit) DataFetchingEnvironmentImpl.newDataFetchingEnvironment(graphql.schema.DataFetchingEnvironmentImpl.newDataFetchingEnvironment) GraphQLFieldsContainer(graphql.schema.GraphQLFieldsContainer) Collections(java.util.Collections) DataFetchingFieldSelectionSet(graphql.schema.DataFetchingFieldSelectionSet) ExecutableNormalizedOperation(graphql.normalized.ExecutableNormalizedOperation) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) DataFetchingEnvironmentImpl.newDataFetchingEnvironment(graphql.schema.DataFetchingEnvironmentImpl.newDataFetchingEnvironment) ExecutableNormalizedField(graphql.normalized.ExecutableNormalizedField) MergedField(graphql.execution.MergedField) Field(graphql.language.Field) GraphQLOutputType(graphql.schema.GraphQLOutputType) ExecutableNormalizedField(graphql.normalized.ExecutableNormalizedField) ResultPath(graphql.execution.ResultPath) GraphQLCodeRegistry(graphql.schema.GraphQLCodeRegistry) QueryDirectivesImpl(graphql.execution.directives.QueryDirectivesImpl) GraphQLFieldsContainer(graphql.schema.GraphQLFieldsContainer) Map(java.util.Map) ExecutionId(graphql.execution.ExecutionId)

Aggregations

ExecutionStepInfo (graphql.execution.ExecutionStepInfo)19 Test (org.junit.jupiter.api.Test)7 MergedField (graphql.execution.MergedField)6 ArrayList (java.util.ArrayList)6 Field (graphql.language.Field)5 DataFetchingFieldSelectionSet (graphql.schema.DataFetchingFieldSelectionSet)5 HashMap (java.util.HashMap)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)4 GraphQLObjectType.newObject (graphql.schema.GraphQLObjectType.newObject)4 ObjectField (org.dotwebstack.framework.core.model.ObjectField)4 GraphQLObjectType (graphql.schema.GraphQLObjectType)3 Map (java.util.Map)3 RequestContext (org.dotwebstack.framework.core.query.model.RequestContext)3 GraphQLError (graphql.GraphQLError)2 FetchedValue (graphql.execution.FetchedValue)2 FieldCollectorParameters (graphql.execution.FieldCollectorParameters)2 MergedSelectionSet (graphql.execution.MergedSelectionSet)2 NonNullableFieldWasNullException (graphql.execution.NonNullableFieldWasNullException)2 ResultPath (graphql.execution.ResultPath)2