use of graphql.execution.ExecutionStrategyParameters in project graphql-java by graphql-java.
the class BatchedExecutionStrategy method executeImpl.
private void executeImpl(ExecutionContext executionContext, ExecutionStrategyParameters parameters, ExecutionNode root, ExecutionNode curNode, Queue<ExecutionNode> queueOfNodes, Iterator<String> curFieldNames, CompletableFuture<ExecutionResult> overallResult) {
if (!curFieldNames.hasNext() && queueOfNodes.isEmpty()) {
overallResult.complete(new ExecutionResultImpl(root.getParentResults().get(0).toObject(), executionContext.getErrors()));
return;
}
if (!curFieldNames.hasNext()) {
curNode = queueOfNodes.poll();
curFieldNames = curNode.getFields().keySet().iterator();
}
String fieldName = curFieldNames.next();
List<Field> currentField = curNode.getFields().get(fieldName);
//
// once an object is resolved from a interface / union to a node with an object type, the
// parent type info has effectively changed (it has got more specific), even though the path etc...
// has not changed
ExecutionTypeInfo currentParentTypeInfo = parameters.getTypeInfo();
ExecutionTypeInfo newParentTypeInfo = newTypeInfo().type(curNode.getType()).fieldDefinition(currentParentTypeInfo.getFieldDefinition()).path(currentParentTypeInfo.getPath()).parentInfo(currentParentTypeInfo.getParentTypeInfo()).build();
ExecutionPath fieldPath = curNode.getTypeInfo().getPath().segment(fieldName);
GraphQLFieldDefinition fieldDefinition = getFieldDef(executionContext.getGraphQLSchema(), curNode.getType(), currentField.get(0));
ExecutionTypeInfo typeInfo = newTypeInfo().type(fieldDefinition.getType()).fieldDefinition(fieldDefinition).path(fieldPath).parentInfo(newParentTypeInfo).build();
ExecutionStrategyParameters newParameters = parameters.transform(builder -> builder.path(fieldPath).field(currentField).typeInfo(typeInfo));
ExecutionNode finalCurNode = curNode;
Iterator<String> finalCurFieldNames = curFieldNames;
resolveField(executionContext, newParameters, fieldName, curNode).whenComplete((childNodes, exception) -> {
if (exception != null) {
handleNonNullException(executionContext, overallResult, exception);
return;
}
queueOfNodes.addAll(childNodes);
executeImpl(executionContext, newParameters, root, finalCurNode, queueOfNodes, finalCurFieldNames, overallResult);
});
}
use of graphql.execution.ExecutionStrategyParameters in project timbuctoo by HuygensING.
the class SerializerExecutionStrategy method execute.
@Override
public CompletableFuture<ExecutionResult> execute(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException {
Map<String, java.util.List<Field>> fields = parameters.fields();
GraphQLObjectType parentType = parameters.typeInfo().castType(GraphQLObjectType.class);
return super.execute(executionContext, parameters).thenApply(sourceResult -> {
Map<String, Object> data = sourceResult.getData();
if (parameters.source() instanceof TypedValue) {
String value = ((TypedValue) parameters.source()).getValue();
String typename = ((TypedValue) parameters.source()).getType();
Value result;
if (value == null) {
result = null;
} else if (data.containsKey("__typename")) {
result = Value.create(value, typename, (String) data.get("__typename"));
} else {
result = Value.create(value, typename);
}
return new ExecutionResultImpl(result, sourceResult.getErrors(), sourceResult.getExtensions());
} else if (parameters.source() instanceof SubjectReference) {
final String uri = ((SubjectReference) parameters.source()).getSubjectUri();
final Set<String> types = ((SubjectReference) parameters.source()).getTypes();
final String graphqlType = getDirectiveArgument(parentType, "rdfType", "uri").orElse(null);
String type;
if (graphqlType != null && types.contains(graphqlType)) {
type = graphqlType;
} else {
Optional<String> firstType = types.stream().sorted().findFirst();
if (firstType.isPresent()) {
type = firstType.get();
} else {
LOG.error("No type present on " + uri + ". Expected at least TIM_UNKNOWN");
type = RdfConstants.UNKNOWN;
}
}
LinkedHashMap<PredicateInfo, Serializable> copy = new LinkedHashMap<>();
for (Map.Entry<String, Object> entry : data.entrySet()) {
final String graphqlFieldName = entry.getKey();
final GraphQLFieldDefinition fieldDesc = parentType.getFieldDefinition(entry.getKey());
Optional<String> predicateUri = getDirectiveArgument(fieldDesc, "rdf", "predicate");
Optional<Direction> direction = getDirectiveArgument(fieldDesc, "rdf", "direction").map(Direction::valueOf);
final PredicateInfo predicateInfo;
predicateInfo = predicateUri.map(predUri -> PredicateInfo.predicateInfo(graphqlFieldName, predUri, direction.orElse(Direction.OUT))).orElseGet(() -> PredicateInfo.predicateInfo(graphqlFieldName, null, Direction.OUT));
if (entry.getValue() == null || entry.getValue() instanceof Serializable) {
copy.put(predicateInfo, (Serializable) entry.getValue());
} else {
copy.put(predicateInfo, Value.fromRawJavaType(entry.getValue()));
}
}
return new ExecutionResultImpl(Entity.entity(uri, type, copy), sourceResult.getErrors(), sourceResult.getExtensions());
} else if (parameters.source() instanceof PaginatedList) {
PaginatedList<? extends DatabaseResult> source = (PaginatedList) parameters.source();
return new ExecutionResultImpl(serializableList(source.getPrevCursor().orElse(null), source.getNextCursor().orElse(null), ((GraphqlIntrospectionList) data.get("items")).getItems()), sourceResult.getErrors(), sourceResult.getExtensions());
} else if (executionContext.getGraphQLSchema().getQueryType() == parentType) {
return new ExecutionResultImpl(QueryContainer.queryContainer(sourceResult.getData()), sourceResult.getErrors(), sourceResult.getExtensions());
} else {
LinkedHashMap<String, Serializable> copy = new LinkedHashMap<>();
for (Map.Entry<String, Object> entry : data.entrySet()) {
if (entry.getValue() == null || entry.getValue() instanceof Serializable) {
copy.put(entry.getKey(), (Serializable) entry.getValue());
} else {
copy.put(entry.getKey(), GraphqlIntrospectionValue.fromRawJavaType(entry.getValue()));
}
}
return new ExecutionResultImpl(graphqlIntrospectionObject(copy), sourceResult.getErrors(), sourceResult.getExtensions());
}
});
}
Aggregations