graphql.language.InlineFragment Java Examples

The following examples show how to use graphql.language.InlineFragment. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ComplexityAnalyzer.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
private void collectFields(FieldCollectorParameters parameters, Map<String, List<ResolvedField>> fields, List<Selection> selectionSet,
                           List<String> visitedFragments, GraphQLFieldsContainer parent) {

    for (Selection selection : selectionSet) {
        if (selection instanceof Field) {
            collectField(parameters, fields, (Field) selection, parent);
        } else if (selection instanceof InlineFragment) {
            collectInlineFragment(parameters, fields, visitedFragments, (InlineFragment) selection, parent);
        } else if (selection instanceof FragmentSpread) {
            collectFragmentSpread(parameters, fields, visitedFragments, (FragmentSpread) selection, parent);
        }
    }
}
 
Example #2
Source File: ComplexityAnalyzer.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
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()));
}
 
Example #3
Source File: Directives.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
@Override
public void visitInlineFragment(QueryVisitorInlineFragmentEnvironment env) {
    InlineFragment fragment = env.getInlineFragment();
    boolean containsField = fieldsToFind.stream().anyMatch(field -> fragment.getSelectionSet().getSelections().contains(field));
    @SuppressWarnings("SuspiciousMethodCalls")
    boolean isRelevant = containsField || relevantFragments.stream().anyMatch(frag -> fragment.getSelectionSet().getSelections().contains(frag));
    if (isRelevant) {
        relevantFragments.add(fragment);
        inlineFragmentDirs.addAll(fragment.getDirectives());
    }
}
 
Example #4
Source File: ComplexityAnalyzer.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
private boolean isConditional(Selection selection) {
    return (selection instanceof FragmentDefinition && ((FragmentDefinition) selection).getTypeCondition() != null)
            || (selection instanceof InlineFragment && ((InlineFragment) selection).getTypeCondition() != null);
}
 
Example #5
Source File: ContentTypeBasedDataFetcher.java    From engine with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Adds the required filters to the ES query for the given field
 */
protected void processSelection(String path, Selection currentSelection, BoolQueryBuilder query,
                                List<String> queryFieldIncludes, DataFetchingEnvironment env)  {
    if (currentSelection instanceof Field) {
        // If the current selection is a field
        Field currentField = (Field) currentSelection;

        // Get the original field name
        String propertyName = getOriginalName(currentField.getName());
        // Build the ES-friendly path
        String fullPath = StringUtils.isEmpty(path)? propertyName : path + "." + propertyName;

        // If the field has sub selection
        if (Objects.nonNull(currentField.getSelectionSet())) {
            // If the field is a flattened component
            if (fullPath.matches(COMPONENT_INCLUDE_REGEX)) {
                // Include the 'content-type' field to make sure the type can be resolved during runtime
                String contentTypeFieldPath = fullPath + "." + QUERY_FIELD_NAME_CONTENT_TYPE;
                if (!queryFieldIncludes.contains(contentTypeFieldPath)) {
                    queryFieldIncludes.add(contentTypeFieldPath);
                }
            }

            // Process recursively and finish
            currentField.getSelectionSet().getSelections()
                .forEach(selection -> processSelection(fullPath, selection, query, queryFieldIncludes, env));
            return;
        }

        // Add the field to the list
        logger.debug("Adding selected field '{}' to query", fullPath);
        queryFieldIncludes.add(fullPath);

        // Check the filters to build the ES query
        Optional<Argument> arg =
            currentField.getArguments().stream().filter(a -> a.getName().equals(FILTER_NAME)).findFirst();
        if (arg.isPresent()) {
            logger.debug("Adding filters for field {}", fullPath);
            Value<?> argValue = arg.get().getValue();
            if (argValue instanceof ObjectValue) {
                List<ObjectField> filters = ((ObjectValue) argValue).getObjectFields();
                filters.forEach((filter) -> addFieldFilterFromObjectField(fullPath, filter, query, env));
            } else if (argValue instanceof VariableReference &&
                    env.getVariables().containsKey(((VariableReference) argValue).getName())) {
                Map<String, Object> map =
                        (Map<String, Object>) env.getVariables().get(((VariableReference) argValue).getName());
                map.entrySet().forEach(filter -> addFieldFilterFromMapEntry(fullPath, filter, query, env));
            }
        }
    } else if (currentSelection instanceof InlineFragment) {
        // If the current selection is an inline fragment, process recursively
        InlineFragment fragment = (InlineFragment) currentSelection;
        fragment.getSelectionSet().getSelections()
            .forEach(selection -> processSelection(path, selection, query, queryFieldIncludes, env));
    } else if (currentSelection instanceof FragmentSpread) {
        // If the current selection is a fragment spread, find the fragment and process recursively
        FragmentSpread fragmentSpread = (FragmentSpread) currentSelection;
        FragmentDefinition fragmentDefinition = env.getFragmentsByName().get(fragmentSpread.getName());
        fragmentDefinition.getSelectionSet().getSelections()
            .forEach(selection -> processSelection(path, selection, query, queryFieldIncludes, env));
    }
}