public MergedSelectionSet collectFields(FieldCollectorParameters parameters, MergedField mergedField) { Map<String, MergedField> subFields = new LinkedHashMap<>(); List<String> visitedFragments = new ArrayList<>(); for (Field field : mergedField.getFields()) { if (field.getSelectionSet() == null) { continue; } this.collectFields(parameters, field.getSelectionSet(), visitedFragments, subFields); } return newMergedSelectionSet().subFields(subFields).build(); }
@Override public void checkField(Field field) { GraphQLOutputType type = getValidationContext().getOutputType(); if (type == null) return; if (isLeaf(type)) { if (field.getSelectionSet() != null) { String message = String.format("Sub selection not allowed on leaf type %s of field %s", type.getName(), field.getName()); addError(ValidationErrorType.SubSelectionNotAllowed, field.getSourceLocation(), message); } } else { if (field.getSelectionSet() == null) { String message = String.format("Sub selection required for type %s of field %s", type.getName(), field.getName()); addError(ValidationErrorType.SubSelectionRequired, field.getSourceLocation(), message); } } } }
return new Conflict(responseName, reason, fieldA, fieldB); SelectionSet selectionSet1 = fieldA.getSelectionSet(); SelectionSet selectionSet2 = fieldB.getSelectionSet(); if (selectionSet1 != null && selectionSet2 != null) { Set<String> visitedFragmentSpreads = new LinkedHashSet<>();
private Builder(Field existing) { this.sourceLocation = existing.getSourceLocation(); this.comments = existing.getComments(); this.name = existing.getName(); this.alias = existing.getAlias(); this.arguments = existing.getArguments(); this.directives = existing.getDirectives(); this.selectionSet = existing.getSelectionSet(); this.ignoredChars = existing.getIgnoredChars(); }
@Override public TraversalControl visitField(Field node, TraverserContext<Node> context) { Field changedNode = node.transform(builder -> { builder.arguments(sort(node.getArguments(), comparing(Argument::getName))); builder.directives(sort(node.getDirectives(), comparing(Directive::getName))); builder.selectionSet(sortSelectionSet(node.getSelectionSet())); }); return changeNode(context, changedNode); }
private NodePrinter<Field> field() { final String argSep = compactMode ? "," : ", "; final String aliasSuffix = compactMode ? ":" : ": "; return (out, node) -> { String alias = wrap("", node.getAlias(), aliasSuffix); String name = node.getName(); String arguments = wrap("(", join(node.getArguments(), argSep), ")"); String directives = directives(node.getDirectives()); String selectionSet = node(node.getSelectionSet()); out.printf("%s", spaced( alias + name + arguments, directives, selectionSet )); }; }
protected final boolean hasSelectionSet(Field field) { return field.getSelectionSet() != null; }
protected final Stream<Field> selections(Field field) { SelectionSet selectionSet = field.getSelectionSet() != null ? field.getSelectionSet() : new SelectionSet(Collections.emptyList()); return selectionSet.getSelections() .stream() .filter(it -> it instanceof Field) .map(it -> (Field) it); }
protected final Stream<Field> flatten(Field field) { SelectionSet selectionSet = field.getSelectionSet() != null ? field.getSelectionSet() : new SelectionSet(Collections.emptyList()); return Stream.concat( Stream.of(field), selectionSet.getSelections() .stream() .filter(it -> it instanceof Field) .flatMap(selection -> this.flatten((Field) selection)) ); }
protected final Optional<Field> getSelectionField(Field field, String fieldName) { return field.getSelectionSet().getSelections().stream() .filter(it -> it instanceof Field) .map(it -> (Field) it) .filter(it -> fieldName.equals(it.getName())) .findFirst(); }
public static boolean requestContainsPageInfo(Field field) { return field.getSelectionSet().getSelections().stream() .anyMatch(f -> f instanceof Field && ConnectionContainer.PAGE_INFO_KEYWORD.equals(((Field) f).getName())); } }
public static boolean requestContainsPageInfo(Field field) { return field.getSelectionSet().getSelections().stream() .anyMatch(f -> f instanceof Field && ConnectionContainer.PAGE_INFO_KEYWORD.equals(((Field) f).getName())); } }
/** * log current context for debugging * @param operation Current operation * @param environment Environment encapsulating graphQL's request environment */ private void logContext(RelationshipOp operation, Environment environment) { List<Field> children = (environment.field.getSelectionSet() != null) ? (List) environment.field.getSelectionSet().getChildren() : new ArrayList<>(); String requestedFields = environment.field.getName() + (children.size() > 0 ? "(" + children.stream().map(Field::getName).collect(Collectors.toList()) + ")" : ""); GraphQLType parent = environment.parentType; if (log.isDebugEnabled()) { log.debug("{} {} fields with parent {}<{}>", operation, requestedFields, EntityDictionary.getSimpleName(parent.getClass()), parent.getName()); } }
/** * log current context for debugging * @param operation Current operation * @param environment Environment encapsulating graphQL's request environment */ private void logContext(RelationshipOp operation, Environment environment) { List<Field> children = (environment.field.getSelectionSet() != null) ? (List) environment.field.getSelectionSet().getChildren() : new ArrayList<>(); String requestedFields = environment.field.getName() + (children.size() > 0 ? "(" + children.stream().map(Field::getName).collect(Collectors.toList()) + ")" : ""); GraphQLType parent = environment.parentType; if (log.isDebugEnabled()) { log.debug("{} {} fields with parent {}<{}>", operation, requestedFields, EntityDictionary.getSimpleName(parent.getClass()), parent.getName()); } }
public MergedSelectionSet collectFields(FieldCollectorParameters parameters, MergedField mergedField) { Map<String, MergedField> subFields = new LinkedHashMap<>(); List<String> visitedFragments = new ArrayList<>(); for (Field field : mergedField.getFields()) { if (field.getSelectionSet() == null) { continue; } this.collectFields(parameters, field.getSelectionSet(), visitedFragments, subFields); } return newMergedSelectionSet().subFields(subFields).build(); }
private GraphQLRequestField getField(Field field){ final GraphQLRequestField requestField = new GraphQLRequestField(); final List<GraphQLRequestField> fields = new ArrayList<>(); final List<GraphQLRequestArgument> arguments = new ArrayList<>(); requestField.setName(field.getName()); requestField.setFields(fields); if(field.getSelectionSet() != null && field.getSelectionSet().getSelections() != null){ for(Selection selection : field.getSelectionSet().getSelections()){ if(selection instanceof Field){ Field subField = (Field) selection; GraphQLRequestField subRequestField = getField(subField); fields.add(subRequestField); } } } for(Argument argument : field.getArguments()){ GraphQLRequestArgument requestArgument = getArgument(argument); arguments.add(requestArgument); } return requestField; }
@Override public void checkField(Field field) { GraphQLOutputType type = getValidationContext().getOutputType(); if (type == null) return; if (isLeaf(type)) { if (field.getSelectionSet() != null) { String message = String.format("Sub selection not allowed on leaf type %s of field %s", type.getName(), field.getName()); addError(ValidationErrorType.SubSelectionNotAllowed, field.getSourceLocation(), message); } } else { if (field.getSelectionSet() == null) { String message = String.format("Sub selection required for type %s of field %s", type.getName(), field.getName()); addError(ValidationErrorType.SubSelectionRequired, field.getSourceLocation(), message); } } } }
@Override public TraversalControl visitField(Field node, TraverserContext<Node> context) { Field changedNode = node.transform(builder -> { builder.arguments(sort(node.getArguments(), comparing(Argument::getName))); builder.directives(sort(node.getDirectives(), comparing(Directive::getName))); builder.selectionSet(sortSelectionSet(node.getSelectionSet())); }); return changeNode(context, changedNode); }
private Builder(Field existing) { this.sourceLocation = existing.getSourceLocation(); this.comments = existing.getComments(); this.name = existing.getName(); this.alias = existing.getAlias(); this.arguments = existing.getArguments(); this.directives = existing.getDirectives(); this.selectionSet = existing.getSelectionSet(); this.ignoredChars = existing.getIgnoredChars(); }
private NodePrinter<Field> field() { final String argSep = compactMode ? "," : ", "; final String aliasSuffix = compactMode ? ":" : ": "; return (out, node) -> { String alias = wrap("", node.getAlias(), aliasSuffix); String name = node.getName(); String arguments = wrap("(", join(node.getArguments(), argSep), ")"); String directives = directives(node.getDirectives()); String selectionSet = node(node.getSelectionSet()); out.printf("%s", spaced( alias + name + arguments, directives, selectionSet )); }; }