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