/** * This will create an execution path from the list of objects * * @param objects the path objects * * @return a new execution path */ public static ExecutionPath fromList(List<?> objects) { assertNotNull(objects); ExecutionPath path = ExecutionPath.rootPath(); for (Object object : objects) { if (object instanceof Number) { path = path.segment(((Number) object).intValue()); } else { path = path.segment(String.valueOf(object)); } } return path; }
/** * Parses an execution path from the provided path string in the format /segment1/segment2[index]/segmentN * * @param pathString the path string * * @return a parsed execution path */ public static ExecutionPath parse(String pathString) { pathString = pathString == null ? "" : pathString; pathString = pathString.trim(); StringTokenizer st = new StringTokenizer(pathString, "/[]", true); ExecutionPath path = ExecutionPath.rootPath(); while (st.hasMoreTokens()) { String token = st.nextToken(); if ("/".equals(token)) { assertTrue(st.hasMoreTokens(), mkErrMsg(), pathString); path = path.segment(st.nextToken()); } else if ("[".equals(token)) { assertTrue(st.countTokens() >= 2, mkErrMsg(), pathString); path = path.segment(Integer.parseInt(st.nextToken())); String closingBrace = st.nextToken(); assertTrue(closingBrace.equals("]"), mkErrMsg(), pathString); } else { throw new AssertException(format(mkErrMsg(), pathString)); } } return path; }
private ExecutionPath mkPath(QueryVisitorFieldEnvironment traversalEnv) { QueryVisitorFieldEnvironment parentEnvironment = traversalEnv.getParentEnvironment(); if (parentEnvironment == null) { return ExecutionPath.rootPath().segment(traversalEnv.getField().getName()); } else { Deque<QueryVisitorFieldEnvironment> stack = new ArrayDeque<>(); stack.push(traversalEnv); while (parentEnvironment != null) { stack.push(parentEnvironment); parentEnvironment = parentEnvironment.getParentEnvironment(); } ExecutionPath path = ExecutionPath.rootPath(); while (!stack.isEmpty()) { QueryVisitorFieldEnvironment environment = stack.pop(); path = path.segment(environment.getField().getName()); } return path; } }
MergedField currentField = fields.getSubField(fieldName); ExecutionPath fieldPath = parameters.getPath().segment(mkNameForPath(currentField)); ExecutionStrategyParameters newParameters = parameters .transform(builder -> builder.field(currentField).path(fieldPath).parent(parameters));
private ExecutionStrategyParameters firstFieldOfSubscriptionSelection(ExecutionStrategyParameters parameters) { MergedSelectionSet fields = parameters.getFields(); MergedField firstField = fields.getSubField(fields.getKeys().get(0)); ExecutionPath fieldPath = parameters.getPath().segment(mkNameForPath(firstField.getSingleField())); return parameters.transform(builder -> builder.field(firstField).path(fieldPath)); }
public ExecutionStepInfo newExecutionStepInfoForListElement(ExecutionStepInfo executionInfo, int index) { GraphQLList fieldType = (GraphQLList) executionInfo.getUnwrappedNonNullType(); GraphQLOutputType typeInList = (GraphQLOutputType) fieldType.getWrappedType(); ExecutionPath indexedPath = executionInfo.getPath().segment(index); return executionInfo.transform(builder -> builder .parentInfo(executionInfo) .type(typeInList) .path(indexedPath)); }
final MergedField currentField = fields.getSubField(fieldName); ExecutionPath fieldPath = parameters.getPath().segment(mkNameForPath(currentField)); ExecutionStrategyParameters newParameters = parameters .transform(builder -> builder.field(currentField).path(fieldPath));
@Override @SuppressWarnings({"TypeParameterUnusedInFormals","FutureReturnValueIgnored"}) public CompletableFuture<ExecutionResult> execute(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException { Instrumentation instrumentation = executionContext.getInstrumentation(); InstrumentationExecutionStrategyParameters instrumentationParameters = new InstrumentationExecutionStrategyParameters(executionContext, parameters); InstrumentationContext<ExecutionResult> executionStrategyCtx = instrumentation.beginExecutionStrategy(instrumentationParameters); MergedSelectionSet fields = parameters.getFields(); List<String> fieldNames = new ArrayList<>(fields.keySet()); CompletableFuture<List<ExecutionResult>> resultsFuture = Async.eachSequentially(fieldNames, (fieldName, index, prevResults) -> { MergedField currentField = fields.getSubField(fieldName); ExecutionPath fieldPath = parameters.getPath().segment(mkNameForPath(currentField)); ExecutionStrategyParameters newParameters = parameters .transform(builder -> builder.field(currentField).path(fieldPath)); return resolveField(executionContext, newParameters); }); CompletableFuture<ExecutionResult> overallResult = new CompletableFuture<>(); executionStrategyCtx.onDispatched(overallResult); resultsFuture.whenComplete(handleResults(executionContext, fieldNames, overallResult)); overallResult.whenComplete(executionStrategyCtx::onCompleted); return overallResult; }
private FetchedValue fetchField(ExecutionContext executionContext, ExecutionStrategyParameters parameters, MergedSelectionSet fields, String fieldName) { MergedField currentField = fields.getSubField(fieldName); ExecutionPath fieldPath = parameters.getPath().segment(fieldName); ExecutionStrategyParameters newParameters = parameters .transform(builder -> builder.field(currentField).path(fieldPath)); return fetchField(executionContext, newParameters).join(); }
private ExecutionStrategyParameters newParameters(ExecutionStrategyParameters parameters, MergedSelectionSet fields, String fieldName) { MergedField currentField = fields.getSubField(fieldName); ExecutionPath fieldPath = parameters.getPath().segment(fieldName); return parameters .transform(builder -> builder.field(currentField).path(fieldPath)); }
@Override public CompletableFuture<ExecutionResult> execute(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException { MergedSelectionSet fields = parameters.getFields(); Map<String, FetchedValue> fetchedValues = new LinkedHashMap<>(); // first fetch every value for (String fieldName : fields.keySet()) { FetchedValue fetchedValue = fetchField(executionContext, parameters, fields, fieldName); fetchedValues.put(fieldName, fetchedValue); } // then for every fetched value, complete it Map<String, Object> results = new LinkedHashMap<>(); for (String fieldName : fetchedValues.keySet()) { MergedField currentField = fields.getSubField(fieldName); FetchedValue fetchedValue = fetchedValues.get(fieldName); ExecutionPath fieldPath = parameters.getPath().segment(fieldName); ExecutionStrategyParameters newParameters = parameters .transform(builder -> builder.field(currentField).path(fieldPath)); try { completeValue(executionContext, results, fieldName, fetchedValue, newParameters); } catch (NonNullableFieldWasNullException e) { assertNonNullFieldPrecondition(e); results = null; break; } } return CompletableFuture.completedFuture(new ExecutionResultImpl(results, executionContext.getErrors())); }
.build(); ExecutionPath fieldPath = curNode.getExecutionStepInfo().getPath().segment(mkNameForPath(currentField)); GraphQLFieldDefinition fieldDefinition = getFieldDef(executionContext.getGraphQLSchema(), curNode.getType(), currentField.getSingleField());
int index = 0; for (Object item : values) { ExecutionPath indexedPath = parameters.getPath().segment(index);
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)); }
/** * This will create an execution path from the list of objects * * @param objects the path objects * * @return a new execution path */ public static ExecutionPath fromList(List<?> objects) { assertNotNull(objects); ExecutionPath path = ExecutionPath.rootPath(); for (Object object : objects) { if (object instanceof Number) { path = path.segment(((Number) object).intValue()); } else { path = path.segment(String.valueOf(object)); } } return path; }
private ExecutionPath mkPath(QueryVisitorFieldEnvironment traversalEnv) { QueryVisitorFieldEnvironment parentEnvironment = traversalEnv.getParentEnvironment(); if (parentEnvironment == null) { return ExecutionPath.rootPath().segment(traversalEnv.getField().getName()); } else { Deque<QueryVisitorFieldEnvironment> stack = new ArrayDeque<>(); stack.push(traversalEnv); while (parentEnvironment != null) { stack.push(parentEnvironment); parentEnvironment = parentEnvironment.getParentEnvironment(); } ExecutionPath path = ExecutionPath.rootPath(); while (!stack.isEmpty()) { QueryVisitorFieldEnvironment environment = stack.pop(); path = path.segment(environment.getField().getName()); } return path; } }
private ExecutionStrategyParameters firstFieldOfSubscriptionSelection(ExecutionStrategyParameters parameters) { MergedSelectionSet fields = parameters.getFields(); MergedField firstField = fields.getSubField(fields.getKeys().get(0)); ExecutionPath fieldPath = parameters.getPath().segment(mkNameForPath(firstField.getSingleField())); return parameters.transform(builder -> builder.field(firstField).path(fieldPath)); }
public ExecutionStepInfo newExecutionStepInfoForListElement(ExecutionStepInfo executionInfo, int index) { GraphQLList fieldType = (GraphQLList) executionInfo.getUnwrappedNonNullType(); GraphQLOutputType typeInList = (GraphQLOutputType) fieldType.getWrappedType(); ExecutionPath indexedPath = executionInfo.getPath().segment(index); return executionInfo.transform(builder -> builder .parentInfo(executionInfo) .type(typeInList) .path(indexedPath)); }
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)); }