private NodePrinter<SelectionSet> selectionSet() { return (out, node) -> { out.printf("%s", comments(node)); out.printf("%s", block(node.getSelections())); }; }
private void collectFields(FieldCollectorParameters parameters, SelectionSet selectionSet, List<String> visitedFragments, Map<String, MergedField> fields) { for (Selection selection : selectionSet.getSelections()) { if (selection instanceof Field) { collectField(parameters, fields, (Field) selection); } else if (selection instanceof InlineFragment) { collectInlineFragment(parameters, visitedFragments, fields, (InlineFragment) selection); } else if (selection instanceof FragmentSpread) { collectFragmentSpread(parameters, visitedFragments, fields, (FragmentSpread) selection); } } }
private Builder(SelectionSet existing) { this.sourceLocation = existing.getSourceLocation(); this.comments = existing.getComments(); this.selections = existing.getSelections(); this.ignoredChars = existing.getIgnoredChars(); }
private void collectFields(Map<String, List<FieldAndType>> fieldMap, SelectionSet selectionSet, GraphQLType parentType, Set<String> visitedFragmentSpreads) { for (Selection selection : selectionSet.getSelections()) { if (selection instanceof Field) { collectFieldsForField(fieldMap, parentType, (Field) selection); } else if (selection instanceof InlineFragment) { collectFieldsForInlineFragment(fieldMap, visitedFragmentSpreads, parentType, (InlineFragment) selection); } else if (selection instanceof FragmentSpread) { collectFieldsForFragmentSpread(fieldMap, visitedFragmentSpreads, (FragmentSpread) selection); } } }
private SelectionSet sortSelectionSet(SelectionSet selectionSet) { if (selectionSet == null) { return null; } List<Selection> selections = sort(selectionSet.getSelections(), comparingSelections()); return selectionSet.transform(builder -> builder.selections(selections)); }
private List<Selection> getUnconditionalSelections(SelectionSet selectionSet) { return selectionSet.getSelections().stream() .filter(selection -> !isConditional(selection)) .collect(Collectors.toList()); }
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())); } }
@Override public void visitInlineFragment(QueryVisitorInlineFragmentEnvironment env) { InlineFragment fragment = env.getInlineFragment(); boolean containsField = fieldsToFind.stream().anyMatch(field -> fragment.getSelectionSet().getSelections().contains(field)); boolean isRelevant = containsField || relevantFragments.stream().anyMatch(frag -> fragment.getSelectionSet().getSelections().contains(frag)); if (isRelevant) { relevantFragments.add(fragment); inlineFragmentDirs.addAll(fragment.getDirectives()); } }
private NodePrinter<SelectionSet> selectionSet() { return (out, node) -> { out.printf("%s", comments(node)); out.printf("%s", block(node.getSelections())); }; }
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)) ); }
private Map<String, List<Selection>> getConditionalSelections(SelectionSet selectionSet) { return selectionSet.getSelections().stream() .filter(this::isConditional) .collect(Collectors.groupingBy(s -> s instanceof FragmentDefinition ? ((FragmentDefinition) s).getTypeCondition().getName() : ((InlineFragment) s).getTypeCondition().getName())); }
@Override public void visitFragmentSpread(QueryVisitorFragmentSpreadEnvironment env) { boolean containsField = fieldsToFind.stream() .anyMatch(field -> env.getFragmentDefinition().getSelectionSet().getSelections().contains(field)); boolean isRelevant = containsField || relevantFragments.stream().anyMatch(frag -> env.getFragmentDefinition().getSelectionSet().getSelections().contains(frag)); if (isRelevant) { relevantFragments.add(env.getFragmentSpread()); fragmentDirs.addAll(env.getFragmentSpread().getDirectives()); fragmentDefDirs.addAll(env.getFragmentDefinition().getDirectives()); } }
private void collectFields(Map<String, List<FieldAndType>> fieldMap, SelectionSet selectionSet, GraphQLType parentType, Set<String> visitedFragmentSpreads) { for (Selection selection : selectionSet.getSelections()) { if (selection instanceof Field) { collectFieldsForField(fieldMap, parentType, (Field) selection); } else if (selection instanceof InlineFragment) { collectFieldsForInlineFragment(fieldMap, visitedFragmentSpreads, parentType, (InlineFragment) selection); } else if (selection instanceof FragmentSpread) { collectFieldsForFragmentSpread(fieldMap, visitedFragmentSpreads, (FragmentSpread) selection); } } }
private void collectFields(FieldCollectorParameters parameters, SelectionSet selectionSet, List<String> visitedFragments, Map<String, MergedField> fields) { for (Selection selection : selectionSet.getSelections()) { if (selection instanceof Field) { collectField(parameters, fields, (Field) selection); } else if (selection instanceof InlineFragment) { collectInlineFragment(parameters, visitedFragments, fields, (InlineFragment) selection); } else if (selection instanceof FragmentSpread) { collectFragmentSpread(parameters, visitedFragments, fields, (FragmentSpread) selection); } } }
private Builder(SelectionSet existing) { this.sourceLocation = existing.getSourceLocation(); this.comments = existing.getComments(); this.selections = existing.getSelections(); this.ignoredChars = existing.getIgnoredChars(); }
@Override public InstrumentationContext<ExecutionResult> beginExecuteOperation(InstrumentationExecuteOperationParameters parameters) { ResolvedField root = new ComplexityAnalyzer(complexityFunction, maximumComplexity).collectFields(parameters.getExecutionContext()); if (log.isDebugEnabled()) { log.debug("Operation {} has total complexity of {}", AstPrinter.printAst(parameters.getExecutionContext().getOperationDefinition().getSelectionSet().getSelections().get(0)), root.getComplexityScore()); } log.info("Total operation complexity: {}", root.getComplexityScore()); return super.beginExecuteOperation(parameters); } }
private SelectionSet sortSelectionSet(SelectionSet selectionSet) { if (selectionSet == null) { return null; } List<Selection> selections = sort(selectionSet.getSelections(), comparingSelections()); return selectionSet.transform(builder -> builder.selections(selections)); }
private void collectInlineFragment(FieldCollectorParameters parameters, Map<String, List<ResolvedField>> fields, List<String> visitedFragments, InlineFragment inlineFragment, GraphQLFieldsContainer parent) { if (!conditionalNodes.shouldInclude(parameters.getVariables(), inlineFragment.getDirectives())) { return; } if (inlineFragment.getTypeCondition() != null) { parent = (GraphQLFieldsContainer) getTypeFromAST(parameters.getGraphQLSchema(), inlineFragment.getTypeCondition()); } collectFields(parameters, fields, inlineFragment.getSelectionSet().getSelections(), visitedFragments, parent); }