All Downloads are FREE. Search and download functionalities are using the official Maven repository.

graphql.analysis.ChildrenOfSelectionProvider Maven / Gradle / Ivy

There is a newer version: 230521-nf-execution
Show newest version
package graphql.analysis;

import graphql.Internal;
import graphql.language.Field;
import graphql.language.FragmentDefinition;
import graphql.language.FragmentSpread;
import graphql.language.InlineFragment;
import graphql.language.Node;
import graphql.language.NodeTraverser;
import graphql.language.NodeVisitorStub;
import graphql.language.Selection;
import graphql.language.SelectionSet;
import graphql.util.TraversalControl;
import graphql.util.TraverserContext;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * QueryTraversal helper class responsible for obtaining Selection
 * nodes from selection parent.
 * Uses double dispatch in order to avoid reflection based {@code instanceof} check.
 */
@Internal
public class ChildrenOfSelectionProvider extends NodeVisitorStub {

    private Map fragmentDefinitionMap;

    public ChildrenOfSelectionProvider(Map fragmentDefinitionMap) {
        this.fragmentDefinitionMap = fragmentDefinitionMap;
    }

    @Override
    public TraversalControl visitInlineFragment(InlineFragment node, TraverserContext context) {
        getSelectionSetChildren(node.getSelectionSet(), context);
        return TraversalControl.CONTINUE;
    }

    @Override
    public TraversalControl visitFragmentSpread(FragmentSpread fragmentSpread, TraverserContext context) {
        getSelectionSetChildren(fragmentDefinitionMap.get(fragmentSpread.getName()).getSelectionSet(), context);
        return TraversalControl.CONTINUE;
    }

    @Override
    public TraversalControl visitField(Field node, TraverserContext context) {
        getSelectionSetChildren(node.getSelectionSet(), context);
        return TraversalControl.CONTINUE;
    }

    @Override
    public TraversalControl visitSelectionSet(SelectionSet node, TraverserContext context) {
        context.setResult(node.getSelections());
        return TraversalControl.CONTINUE;
    }

    private void getSelectionSetChildren(SelectionSet node, TraverserContext context) {
        if (node == null) {
            context.setResult(Collections.emptyList());
        } else {
            context.setResult(node.getSelections());
        }
    }

    public List getSelections(Selection selection) {
        return NodeTraverser.oneVisitWithResult(selection, this);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy