@Internal ExecutionContextBuilder(ExecutionContext other) { instrumentation = other.getInstrumentation(); executionId = other.getExecutionId(); instrumentationState = other.getInstrumentationState(); graphQLSchema = other.getGraphQLSchema(); queryStrategy = other.getQueryStrategy(); mutationStrategy = other.getMutationStrategy(); subscriptionStrategy = other.getSubscriptionStrategy(); context = other.getContext(); root = other.getRoot(); document = other.getDocument(); operationDefinition = other.getOperationDefinition(); variables = new HashMap<>(other.getVariables()); fragmentsByName = new HashMap<>(other.getFragmentsByName()); dataLoaderRegistry = other.getDataLoaderRegistry(); errors = new ArrayList<>(other.getErrors()); }
@SuppressWarnings("SameReturnValue") private Object handleCoercionProblem(ExecutionContext context, ExecutionStrategyParameters parameters, CoercingSerializeException e) { SerializationError error = new SerializationError(parameters.getPath(), e); log.warn(error.getMessage(), e); context.addError(error); parameters.deferredErrorSupport().onError(error); return null; }
private DataFetchingFieldSelectionSetImpl(ExecutionContext executionContext, GraphQLFieldsContainer parentFieldType, MergedField parentFields) { this(parentFields, parentFieldType, executionContext.getGraphQLSchema(), executionContext.getVariables(), executionContext.getFragmentsByName()); }
private boolean isDataLoaderCompatibleExecution(ExecutionContext executionContext) { // // currently we only support Query operations and ONLY with AsyncExecutionStrategy as the query ES // This may change in the future but this is the fix for now // if (executionContext.getOperationDefinition().getOperation() == OperationDefinition.Operation.QUERY) { ExecutionStrategy queryStrategy = executionContext.getQueryStrategy(); if (queryStrategy instanceof AsyncExecutionStrategy) { return true; } } return false; }
public ReactiveContext(ExecutionContext context, Object key) { super( context.getInstrumentation(), context.getExecutionId(), context.getGraphQLSchema(), context.getQueryStrategy(), context.getMutationStrategy(), context.getSubscriptionStrategy(), context.getFragmentsByName(), context.getOperationDefinition(), context.getVariables(), context.getRoot() ); this.key = key; this.parent = context; }
public static Builder newDataFetchingEnvironment(ExecutionContext executionContext) { return new Builder() .context(executionContext.getContext()) .root(executionContext.getRoot()) .graphQLSchema(executionContext.getGraphQLSchema()) .fragmentsByName(executionContext.getFragmentsByName()) .dataLoaderRegistry(executionContext.getDataLoaderRegistry()) .document(executionContext.getDocument()) .operationDefinition(executionContext.getOperationDefinition()) .variables(executionContext.getVariables()) .executionId(executionContext.getExecutionId()); }
operationRootType = getOperationRootType(executionContext.getGraphQLSchema(), operationDefinition); } catch (RuntimeException rte) { if (rte instanceof GraphQLError) { .schema(executionContext.getGraphQLSchema()) .objectType(operationRootType) .fragments(executionContext.getFragmentsByName()) .variables(executionContext.getVariables()) .build(); .executionStepInfo(executionStepInfo) .source(root) .localContext(executionContext.getContext()) .fields(fields) .nonNullFieldValidator(nonNullableFieldValidator) executionStrategy = queryStrategy; log.debug("Executing '{}' query operation: '{}' using '{}' execution strategy", executionContext.getExecutionId(), operation, executionStrategy.getClass().getName()); result = executionStrategy.execute(executionContext, parameters); } catch (NonNullableFieldWasNullException e) { result = completedFuture(new ExecutionResultImpl(null, executionContext.getErrors()));
Field field = parameters.getField().getSingleField(); GraphQLObjectType parentType = (GraphQLObjectType) parameters.getExecutionStepInfo().getUnwrappedNonNullType(); GraphQLFieldDefinition fieldDef = getFieldDef(executionContext.getGraphQLSchema(), parentType, field); ExecutionStepInfo executionStepInfo = createExecutionStepInfo(executionContext, parameters, fieldDef, parentType); Instrumentation instrumentation = executionContext.getInstrumentation(); InstrumentationFieldCompleteParameters instrumentationParams = new InstrumentationFieldCompleteParameters(executionContext, parameters, fieldDef, executionStepInfo, fetchedValue); InstrumentationContext<ExecutionResult> ctxCompleteField = instrumentation.beginFieldComplete( ); GraphQLCodeRegistry codeRegistry = executionContext.getGraphQLSchema().getCodeRegistry(); Map<String, Object> argumentValues = valuesResolver.getArgumentValues(codeRegistry, fieldDef.getArguments(), field.getArguments(), executionContext.getVariables()); ); log.debug("'{}' completing field '{}'...", executionContext.getExecutionId(), executionStepInfo.getPath());
public ExecutionStepInfo newExecutionStepInfoForSubField(ExecutionContext executionContext, MergedField mergedField, ExecutionStepInfo parentInfo) { GraphQLObjectType parentType = (GraphQLObjectType) parentInfo.getUnwrappedNonNullType(); GraphQLFieldDefinition fieldDefinition = Introspection.getFieldDef(executionContext.getGraphQLSchema(), parentType, mergedField.getName()); GraphQLOutputType fieldType = fieldDefinition.getType(); List<Argument> fieldArgs = mergedField.getArguments(); GraphQLCodeRegistry codeRegistry = executionContext.getGraphQLSchema().getCodeRegistry(); Map<String, Object> argumentValues = valuesResolver.getArgumentValues(codeRegistry, fieldDefinition.getArguments(), fieldArgs, executionContext.getVariables()); ExecutionPath newPath = parentInfo.getPath().segment(mergedField.getResultKey()); return parentInfo.transform(builder -> builder .parentInfo(parentInfo) .type(fieldType) .fieldDefinition(fieldDefinition) .fieldContainer(parentType) .field(mergedField) .path(newPath) .arguments(argumentValues)); }
private CompletableFuture<List<ExecutionNode>> resolveField(ExecutionContext executionContext, ExecutionStrategyParameters parameters, String fieldName, ExecutionNode node) { GraphQLObjectType parentType = node.getType(); MergedField fields = node.getFields().get(fieldName); GraphQLFieldDefinition fieldDef = getFieldDef(executionContext.getGraphQLSchema(), parentType, fields.getSingleField()); Instrumentation instrumentation = executionContext.getInstrumentation(); ExecutionStepInfo executionStepInfo = parameters.getExecutionStepInfo(); InstrumentationContext<ExecutionResult> fieldCtx = instrumentation.beginField( new InstrumentationFieldParameters(executionContext, fieldDef, executionStepInfo) ); CompletableFuture<FetchedValues> fetchedData = fetchData(executionContext, parameters, fieldName, node, fieldDef); CompletableFuture<List<ExecutionNode>> result = fetchedData.thenApply((fetchedValues) -> { GraphQLCodeRegistry codeRegistry = executionContext.getGraphQLSchema().getCodeRegistry(); Map<String, Object> argumentValues = valuesResolver.getArgumentValues( codeRegistry, fieldDef.getArguments(), fields.getSingleField().getArguments(), executionContext.getVariables()); return completeValues(executionContext, fetchedValues, executionStepInfo, fieldName, fields, argumentValues); }); fieldCtx.onDispatched(null); result = result.whenComplete((nodes, throwable) -> fieldCtx.onCompleted(null, throwable)); return result; }
static List<GraphQLError> validateFieldsAndArguments(FieldValidation fieldValidation, ExecutionContext executionContext) { Map<ExecutionPath, List<FieldAndArguments>> fieldArgumentsMap = new LinkedHashMap<>(); QueryTraversal queryTraversal = QueryTraversal.newQueryTraversal() .schema(executionContext.getGraphQLSchema()) .document(executionContext.getDocument()) .operationName(executionContext.getOperationDefinition().getName()) .variables(executionContext.getVariables()) .build(); queryTraversal.visitPreOrder(new QueryVisitorStub() { @Override public void visitField(QueryVisitorFieldEnvironment env) { Field field = env.getField(); if (field.getArguments() != null && !field.getArguments().isEmpty()) { // // only fields that have arguments make any sense to placed in play // since only they have variable input FieldAndArguments fieldArguments = new FieldAndArgumentsImpl(env); ExecutionPath path = fieldArguments.getPath(); List<FieldAndArguments> list = fieldArgumentsMap.getOrDefault(path, new ArrayList<>()); list.add(fieldArguments); fieldArgumentsMap.put(path, list); } } }); FieldValidationEnvironment environment = new FieldValidationEnvironmentImpl(executionContext, fieldArgumentsMap); // // this will allow a consumer to plugin their own validation of fields and arguments return fieldValidation.validateFields(environment); }
/** * Called to turn an java object value into an graphql object value * * @param executionContext contains the top level execution parameters * @param parameters contains the parameters holding the fields to be executed and source object * @param resolvedObjectType the resolved object type * @param result the result to be coerced * * @return a promise to an {@link ExecutionResult} */ protected CompletableFuture<ExecutionResult> completeValueForObject(ExecutionContext executionContext, ExecutionStrategyParameters parameters, GraphQLObjectType resolvedObjectType, Object result) { ExecutionStepInfo executionStepInfo = parameters.getExecutionStepInfo(); FieldCollectorParameters collectorParameters = newParameters() .schema(executionContext.getGraphQLSchema()) .objectType(resolvedObjectType) .fragments(executionContext.getFragmentsByName()) .variables(executionContext.getVariables()) .build(); MergedSelectionSet subFields = fieldCollector.collectFields(collectorParameters, parameters.getField()); ExecutionStepInfo newExecutionStepInfo = executionStepInfo.changeTypeWithPreservedNonNull(resolvedObjectType); NonNullableFieldValidator nonNullableFieldValidator = new NonNullableFieldValidator(executionContext, newExecutionStepInfo); ExecutionStrategyParameters newParameters = parameters.transform(builder -> builder.executionStepInfo(newExecutionStepInfo) .fields(subFields) .nonNullFieldValidator(nonNullableFieldValidator) .source(result) ); // Calling this from the executionContext to ensure we shift back from mutation strategy to the query strategy. return executionContext.getQueryStrategy().execute(executionContext, newParameters); }
@Override protected ExecutionResult resolveField(ExecutionContext executionContext, ExecutionParameters parameters, List<Field> fields) { GraphQLObjectType parentType = (GraphQLObjectType) parameters.typeInfo().type(); GraphQLFieldDefinition fieldDef = getFieldDef(executionContext.getGraphQLSchema(), parentType, fields.get(0)); if (fieldDef == null) return null; Field field = (Field) executionContext.getOperationDefinition().getSelectionSet().getSelections().get(0); Argument argument = field.getArguments().get(0); if (argument.getValue() instanceof VariableReference) { VariableReference ref = (VariableReference) argument.getValue(); HashMap mutationInputVariables = (HashMap) executionContext.getVariables().get(ref.getName()); clientMutationId = mutationInputVariables.get(CLIENT_MUTATION_ID); } else {
ResolvedField collectFields(ExecutionContext context) { FieldCollectorParameters parameters = FieldCollectorParameters.newParameters() .schema(context.getGraphQLSchema()) .objectType(context.getGraphQLSchema().getQueryType()) .fragments(context.getFragmentsByName()) .variables(context.getVariables()) .build(); List<Field> fields = context.getOperationDefinition().getSelectionSet().getSelections().stream() .map(selection -> (Field) selection) .collect(Collectors.toList()); Field field = fields.get(0); GraphQLFieldDefinition fieldDefinition; if (GraphQLUtils.isIntrospectionField(field)) { fieldDefinition = Introspection.SchemaMetaFieldDef; } else { fieldDefinition = Objects.requireNonNull( getRootType(context.getGraphQLSchema(), context.getOperationDefinition()) .getFieldDefinition(field.getName())); } Map<String, Object> argumentValues = valuesResolver.getArgumentValues(fieldDefinition.getArguments(), field.getArguments(), context.getVariables()); return collectFields(parameters, fields.stream().map(f -> new ResolvedField(f, fieldDefinition, argumentValues)).collect(Collectors.toList())); }
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); Map<String, Object> argumentValues = valuesResolver.getArgumentValues(codeRegistry, fieldDef.getArguments(), field.getArguments(), executionContext.getVariables()); GraphQLOutputType fieldType = fieldDef.getType(); DataFetchingFieldSelectionSet fieldCollector = DataFetchingFieldSelectionSetImpl.newCollector(executionContext, fieldType, sameFields); DataFetchingEnvironment environment = newDataFetchingEnvironment(executionContext) .source(source) .localContext(localContext) .arguments(argumentValues) .fieldDefinition(fieldDef) .mergedField(sameFields) .fieldType(fieldType) .executionStepInfo(executionInfo) .parentType(parentType) .selectionSet(fieldCollector) .build(); ExecutionId executionId = executionContext.getExecutionId(); ExecutionPath 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); }
protected BiConsumer<List<ExecutionResult>, Throwable> handleResults(ExecutionContext executionContext, List<String> fieldNames, CompletableFuture<ExecutionResult> overallResult) { return (List<ExecutionResult> results, Throwable exception) -> { if (exception != null) { handleNonNullException(executionContext, overallResult, exception); return; } Map<String, Object> resolvedValuesByField = new LinkedHashMap<>(); int ix = 0; for (ExecutionResult executionResult : results) { String fieldName = fieldNames.get(ix++); resolvedValuesByField.put(fieldName, executionResult.getData()); } overallResult.complete(new ExecutionResultImpl(resolvedValuesByField, executionContext.getErrors())); }; } }
overallResult.complete(new ExecutionResultImpl(root.getParentResults().get(0).toObject(), executionContext.getErrors())); return; GraphQLFieldDefinition fieldDefinition = getFieldDef(executionContext.getGraphQLSchema(), curNode.getType(), currentField.getSingleField());
/** * Called to discover the field definition give the current parameters and the AST {@link Field} * * @param executionContext contains the top level execution parameters * @param parameters contains the parameters holding the fields to be executed and source object * @param field the field to find the definition of * * @return a {@link GraphQLFieldDefinition} */ protected GraphQLFieldDefinition getFieldDef(ExecutionContext executionContext, ExecutionStrategyParameters parameters, Field field) { GraphQLObjectType parentType = (GraphQLObjectType) parameters.getExecutionStepInfo().getUnwrappedNonNullType(); return getFieldDef(executionContext.getGraphQLSchema(), parentType, field); }
); executionContext = instrumentation.instrumentExecutionContext(executionContext, parameters); return executeOperation(executionContext, parameters, executionInput.getRoot(), executionContext.getOperationDefinition());
private ExecutionResult executeOperation( ExecutionContext executionContext, Object root, OperationDefinition operationDefinition) { GraphQLObjectType operationRootType = getOperationRootType(executionContext.getGraphQLSchema(), executionContext.getOperationDefinition()); Map<String, List<Field>> fields = new LinkedHashMap<String, List<Field>>(); fieldCollector.collectFields(executionContext, operationRootType, operationDefinition.getSelectionSet(), new ArrayList<String>(), fields); if (operationDefinition.getOperation() == OperationDefinition.Operation.MUTATION) { return new GraphQLDefaultRxExecutionStrategy(graphQLSchemaHolder, maxQueryDepth, maxQueryComplexity) .execute(executionContext, operationRootType, root, fields); } else { return strategy.execute(executionContext, operationRootType, root, fields); } } }