Search in sources :

Example 1 with MergedField

use of graphql.execution.MergedField in project graphql-java by graphql-java.

the class FetchedValueAnalyzer method analyzeFetchedValueImpl.

private FetchedValueAnalysis analyzeFetchedValueImpl(ExecutionContext executionContext, FetchedValue fetchedValue, Object toAnalyze, ExecutionStepInfo executionInfo) throws NonNullableFieldWasNullException {
    GraphQLType fieldType = executionInfo.getUnwrappedNonNullType();
    MergedField field = executionInfo.getField();
    if (isList(fieldType)) {
        return analyzeList(executionContext, fetchedValue, toAnalyze, executionInfo);
    } else if (fieldType instanceof GraphQLScalarType) {
        return analyzeScalarValue(fetchedValue, toAnalyze, (GraphQLScalarType) fieldType, executionInfo);
    } else if (fieldType instanceof GraphQLEnumType) {
        return analyzeEnumValue(fetchedValue, toAnalyze, (GraphQLEnumType) fieldType, executionInfo);
    }
    // 
    if (toAnalyze == null) {
        return newFetchedValueAnalysis(OBJECT).fetchedValue(fetchedValue).executionStepInfo(executionInfo).nullValue().build();
    }
    try {
        GraphQLObjectType resolvedObjectType = resolveType.resolveType(executionContext, field, toAnalyze, executionInfo, fieldType, fetchedValue.getLocalContext());
        return newFetchedValueAnalysis(OBJECT).fetchedValue(fetchedValue).executionStepInfo(executionInfo).completedValue(toAnalyze).resolvedType(resolvedObjectType).build();
    } catch (UnresolvedTypeException ex) {
        return handleUnresolvedTypeProblem(fetchedValue, executionInfo, ex);
    }
}
Also used : GraphQLEnumType(graphql.schema.GraphQLEnumType) MergedField(graphql.execution.MergedField) GraphQLType(graphql.schema.GraphQLType) GraphQLObjectType(graphql.schema.GraphQLObjectType) UnresolvedTypeException(graphql.execution.UnresolvedTypeException) GraphQLScalarType(graphql.schema.GraphQLScalarType)

Example 2 with MergedField

use of graphql.execution.MergedField in project graphql-java by graphql-java.

the class ExecutableNormalizedOperationFactory method createNormalizedQueryImpl.

/**
 * Creates a new Normalized query tree for the provided query
 */
private ExecutableNormalizedOperation createNormalizedQueryImpl(GraphQLSchema graphQLSchema, OperationDefinition operationDefinition, Map<String, FragmentDefinition> fragments, Map<String, Object> coercedVariableValues, @Nullable Map<String, NormalizedInputValue> normalizedVariableValues) {
    FieldCollectorNormalizedQueryParams parameters = FieldCollectorNormalizedQueryParams.newParameters().fragments(fragments).schema(graphQLSchema).coercedVariables(coercedVariableValues).normalizedVariables(normalizedVariableValues).build();
    GraphQLObjectType rootType = Common.getOperationRootType(graphQLSchema, operationDefinition);
    CollectNFResult collectFromOperationResult = collectFromOperation(parameters, operationDefinition, rootType);
    ImmutableListMultimap.Builder<Field, ExecutableNormalizedField> fieldToNormalizedField = ImmutableListMultimap.builder();
    ImmutableMap.Builder<ExecutableNormalizedField, MergedField> normalizedFieldToMergedField = ImmutableMap.builder();
    ImmutableListMultimap.Builder<FieldCoordinates, ExecutableNormalizedField> coordinatesToNormalizedFields = ImmutableListMultimap.builder();
    for (ExecutableNormalizedField topLevel : collectFromOperationResult.children) {
        ImmutableList<FieldAndAstParent> mergedField = collectFromOperationResult.normalizedFieldToAstFields.get(topLevel);
        normalizedFieldToMergedField.put(topLevel, newMergedField(map(mergedField, fieldAndAstParent -> fieldAndAstParent.field)).build());
        updateFieldToNFMap(topLevel, mergedField, fieldToNormalizedField);
        updateCoordinatedToNFMap(coordinatesToNormalizedFields, topLevel);
        buildFieldWithChildren(topLevel, mergedField, parameters, fieldToNormalizedField, normalizedFieldToMergedField, coordinatesToNormalizedFields, 1);
    }
    for (FieldCollectorNormalizedQueryParams.PossibleMerger possibleMerger : parameters.possibleMergerList) {
        List<ExecutableNormalizedField> childrenWithSameResultKey = possibleMerger.parent.getChildrenWithSameResultKey(possibleMerger.resultKey);
        ENFMerger.merge(possibleMerger.parent, childrenWithSameResultKey, graphQLSchema);
    }
    return new ExecutableNormalizedOperation(operationDefinition.getOperation(), operationDefinition.getName(), new ArrayList<>(collectFromOperationResult.children), fieldToNormalizedField.build(), normalizedFieldToMergedField.build(), coordinatesToNormalizedFields.build());
}
Also used : OperationDefinition(graphql.language.OperationDefinition) NodeUtil(graphql.language.NodeUtil) ValuesResolver(graphql.execution.ValuesResolver) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) FragmentSpread(graphql.language.FragmentSpread) GraphQLInterfaceType(graphql.schema.GraphQLInterfaceType) GraphQLUnionType(graphql.schema.GraphQLUnionType) Collections.singletonList(java.util.Collections.singletonList) Selection(graphql.language.Selection) Assert.assertShouldNeverHappen(graphql.Assert.assertShouldNeverHappen) Collections.singleton(java.util.Collections.singleton) Map(java.util.Map) GraphQLCompositeType(graphql.schema.GraphQLCompositeType) SelectionSet(graphql.language.SelectionSet) GraphQLObjectType(graphql.schema.GraphQLObjectType) ImmutableSet(com.google.common.collect.ImmutableSet) MergedField.newMergedField(graphql.execution.MergedField.newMergedField) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) FpKit.groupingBy(graphql.util.FpKit.groupingBy) Collection(java.util.Collection) Set(java.util.Set) Sets(com.google.common.collect.Sets) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) ConditionalNodes(graphql.execution.ConditionalNodes) FragmentDefinition(graphql.language.FragmentDefinition) FieldCoordinates(graphql.schema.FieldCoordinates) Internal(graphql.Internal) GraphQLTypeUtil.unwrapAll(graphql.schema.GraphQLTypeUtil.unwrapAll) GraphQLType(graphql.schema.GraphQLType) ArrayList(java.util.ArrayList) Introspection(graphql.introspection.Introspection) LinkedHashMap(java.util.LinkedHashMap) ImmutableList(com.google.common.collect.ImmutableList) Common(graphql.execution.nextgen.Common) GraphQLSchema(graphql.schema.GraphQLSchema) GraphQLTypeUtil(graphql.schema.GraphQLTypeUtil) MergedField(graphql.execution.MergedField) GraphQLUnmodifiedType(graphql.schema.GraphQLUnmodifiedType) Field(graphql.language.Field) FpKit.filterSet(graphql.util.FpKit.filterSet) Document(graphql.language.Document) VariableDefinition(graphql.language.VariableDefinition) ImmutableKit.map(graphql.collect.ImmutableKit.map) Assert.assertNotNull(graphql.Assert.assertNotNull) InlineFragment(graphql.language.InlineFragment) Collections(java.util.Collections) FieldCoordinates(graphql.schema.FieldCoordinates) ImmutableMap(com.google.common.collect.ImmutableMap) MergedField.newMergedField(graphql.execution.MergedField.newMergedField) MergedField(graphql.execution.MergedField) Field(graphql.language.Field) MergedField.newMergedField(graphql.execution.MergedField.newMergedField) MergedField(graphql.execution.MergedField) GraphQLObjectType(graphql.schema.GraphQLObjectType) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap)

Example 3 with MergedField

use of graphql.execution.MergedField 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)

Example 4 with MergedField

use of graphql.execution.MergedField in project graphql-java by graphql-java.

the class ExecutionStrategyUtil method createFieldSubSelection.

public FieldSubSelection createFieldSubSelection(ExecutionContext executionContext, ExecutionStepInfo executionInfo, ResolvedValue resolvedValue) {
    MergedField field = executionInfo.getField();
    Object source = resolvedValue.getCompletedValue();
    Object localContext = resolvedValue.getLocalContext();
    GraphQLOutputType sourceType = executionInfo.getUnwrappedNonNullType();
    GraphQLObjectType resolvedObjectType = resolveType.resolveType(executionContext, field, source, executionInfo, sourceType, localContext);
    FieldCollectorParameters collectorParameters = newParameters().schema(executionContext.getGraphQLSchema()).objectType(resolvedObjectType).fragments(executionContext.getFragmentsByName()).variables(executionContext.getVariables()).build();
    MergedSelectionSet subFields = fieldCollector.collectFields(collectorParameters, executionInfo.getField());
    // it is not really a new step but rather a refinement
    ExecutionStepInfo newExecutionStepInfoWithResolvedType = executionInfo.changeTypeWithPreservedNonNull(resolvedObjectType);
    return FieldSubSelection.newFieldSubSelection().source(source).localContext(localContext).mergedSelectionSet(subFields).executionInfo(newExecutionStepInfoWithResolvedType).build();
}
Also used : MergedSelectionSet(graphql.execution.MergedSelectionSet) GraphQLOutputType(graphql.schema.GraphQLOutputType) FieldCollectorParameters(graphql.execution.FieldCollectorParameters) MergedField(graphql.execution.MergedField) ExecutionStepInfo(graphql.execution.ExecutionStepInfo) GraphQLObjectType(graphql.schema.GraphQLObjectType)

Example 5 with MergedField

use of graphql.execution.MergedField in project dotwebstack-framework by dotwebstack.

the class BackendRequestFactoryTest method createRequestContext_returnsRequestContext_whithObjectFieldType_Address.

@Test
void createRequestContext_returnsRequestContext_whithObjectFieldType_Address() {
    LocalDate date = LocalDate.of(2021, 1, 1);
    Map<String, Object> data = new HashMap<>();
    data.put("key", new DateSupplier(false, date));
    Map<String, Object> source = Map.of("arg", Map.of("arg1", data));
    DataFetchingEnvironmentImpl.Builder envBuilder = new DataFetchingEnvironmentImpl.Builder();
    envBuilder.source(source);
    ExecutionStepInfo executionStepInfo = mock(ExecutionStepInfo.class);
    GraphQLObjectType objectType = mock(GraphQLObjectType.class);
    when(objectType.getName()).thenReturn("Brewery");
    when(executionStepInfo.getObjectType()).thenReturn(objectType);
    MergedField mergedField = mock(MergedField.class);
    when(mergedField.getName()).thenReturn("addresses");
    when(executionStepInfo.getField()).thenReturn(mergedField);
    envBuilder.executionStepInfo(executionStepInfo);
    envBuilder.fieldDefinition(newFieldDefinition().name("field").type(Scalars.GraphQLID).definition(FieldDefinition.newFieldDefinition().additionalData(GraphQlConstants.IS_PAGING_NODE, Boolean.TRUE.toString()).build()).build());
    envBuilder.fieldDefinition(newFieldDefinition().name("field").type(Scalars.GraphQLID).definition(FieldDefinition.newFieldDefinition().additionalData(GraphQlConstants.IS_PAGING_NODE, Boolean.TRUE.toString()).build()).build());
    var schema = testHelper.loadSchema("dotwebstack/dotwebstack-objecttypes.yaml");
    var backendRequestFactory = new BackendRequestFactory(schema, new BackendExecutionStepInfo());
    var result = backendRequestFactory.createRequestContext(envBuilder.build());
    assertThat(result, is(notNullValue()));
    assertThat(result.getObjectField().getName(), is("addresses"));
    assertThat(result.getObjectField().getType(), is("Address"));
}
Also used : HashMap(java.util.HashMap) DataFetchingEnvironmentImpl(graphql.schema.DataFetchingEnvironmentImpl) LocalDate(java.time.LocalDate) DateSupplier(org.dotwebstack.framework.core.scalars.DateSupplier) ExecutionStepInfo(graphql.execution.ExecutionStepInfo) MergedField(graphql.execution.MergedField) GraphQLObjectType(graphql.schema.GraphQLObjectType) Test(org.junit.jupiter.api.Test)

Aggregations

MergedField (graphql.execution.MergedField)5 GraphQLObjectType (graphql.schema.GraphQLObjectType)4 ExecutionStepInfo (graphql.execution.ExecutionStepInfo)3 ImmutableList (com.google.common.collect.ImmutableList)2 Internal (graphql.Internal)2 ValuesResolver (graphql.execution.ValuesResolver)2 Field (graphql.language.Field)2 GraphQLFieldDefinition (graphql.schema.GraphQLFieldDefinition)2 GraphQLOutputType (graphql.schema.GraphQLOutputType)2 GraphQLTypeUtil (graphql.schema.GraphQLTypeUtil)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 Collections.singletonList (java.util.Collections.singletonList)2 List (java.util.List)2 Map (java.util.Map)2 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Sets (com.google.common.collect.Sets)1 Assert (graphql.Assert)1