graphql.language.FragmentSpread Java Examples

The following examples show how to use graphql.language.FragmentSpread. 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: 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));
    }
}