graphql.language.NodeUtil Maven / Gradle / Ivy
package graphql.language;
import com.google.common.collect.ImmutableList;
import graphql.Internal;
import graphql.execution.UnknownOperationException;
import graphql.util.FpKit;
import graphql.util.NodeLocation;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import static graphql.util.FpKit.mergeFirst;
/**
* Helper class for working with {@link Node}s
*/
@Internal
public class NodeUtil {
public static > T findNodeByName(List namedNodes, String name) {
for (T namedNode : namedNodes) {
if (Objects.equals(namedNode.getName(), name)) {
return namedNode;
}
}
return null;
}
public static Map> allDirectivesByName(List directives) {
return FpKit.groupingBy(directives, Directive::getName);
}
public static > Map nodeByName(List nameNode) {
return FpKit.getByName(nameNode, NamedNode::getName, mergeFirst());
}
public static class GetOperationResult {
public OperationDefinition operationDefinition;
public Map fragmentsByName;
}
public static Map getFragmentsByName(Document document) {
Map fragmentsByName = new LinkedHashMap<>();
for (Definition definition : document.getDefinitions()) {
if (definition instanceof FragmentDefinition) {
FragmentDefinition fragmentDefinition = (FragmentDefinition) definition;
fragmentsByName.put(fragmentDefinition.getName(), fragmentDefinition);
}
}
return fragmentsByName;
}
public static GetOperationResult getOperation(Document document, String operationName) {
Map fragmentsByName = new LinkedHashMap<>();
Map operationsByName = new LinkedHashMap<>();
for (Definition definition : document.getDefinitions()) {
if (definition instanceof OperationDefinition) {
OperationDefinition operationDefinition = (OperationDefinition) definition;
operationsByName.put(operationDefinition.getName(), operationDefinition);
}
if (definition instanceof FragmentDefinition) {
FragmentDefinition fragmentDefinition = (FragmentDefinition) definition;
fragmentsByName.put(fragmentDefinition.getName(), fragmentDefinition);
}
}
if (operationName == null && operationsByName.size() > 1) {
throw new UnknownOperationException("Must provide operation name if query contains multiple operations.");
}
OperationDefinition operation;
if (operationName == null || operationName.isEmpty()) {
operation = operationsByName.values().iterator().next();
} else {
operation = operationsByName.get(operationName);
}
if (operation == null) {
throw new UnknownOperationException(String.format("Unknown operation named '%s'.", operationName));
}
GetOperationResult result = new GetOperationResult();
result.fragmentsByName = fragmentsByName;
result.operationDefinition = operation;
return result;
}
public static void assertNewChildrenAreEmpty(NodeChildrenContainer newChildren) {
if (!newChildren.isEmpty()) {
throw new IllegalArgumentException("Cannot pass non-empty newChildren to Node that doesn't hold children");
}
}
public static Node removeChild(Node node, NodeLocation childLocationToRemove) {
NodeChildrenContainer namedChildren = node.getNamedChildren();
NodeChildrenContainer newChildren = namedChildren.transform(builder -> builder.removeChild(childLocationToRemove.getName(), childLocationToRemove.getIndex()));
return node.withNewChildren(newChildren);
}
}