Repository navigation
Expand file tree
/
Copy pathNodeUtil.java
More file actions
147 lines (120 loc) · 5.47 KB
/
Copy pathNodeUtil.java
File metadata and controls
147 lines (120 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package graphql.language;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import graphql.Internal;
import graphql.execution.UnknownOperationException;
import graphql.util.FpKit;
import graphql.util.NodeLocation;
import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import static graphql.util.FpKit.mergeFirst;
import static java.util.Objects.requireNonNull;
/**
* Helper class for working with {@link Node}s
*/
@Internal
public class NodeUtil {
public static <T extends NamedNode<T>> T findNodeByName(List<T> namedNodes, String name) {
for (T namedNode : namedNodes) {
if (Objects.equals(namedNode.getName(), name)) {
return namedNode;
}
}
return null;
}
public static Map<String, ImmutableList<Directive>> allDirectivesByName(List<Directive> directives) {
return FpKit.groupingBy(directives, Directive::getName);
}
public static <T extends NamedNode<T>> Map<String, T> nodeByName(List<T> nameNode) {
return FpKit.getByName(nameNode, NamedNode::getName, mergeFirst());
}
public static class GetOperationResult {
public OperationDefinition operationDefinition;
public Map<String, FragmentDefinition> fragmentsByName;
}
public static Map<String, FragmentDefinition> getFragmentsByName(Document document) {
Map<String, FragmentDefinition> 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<String, FragmentDefinition> fragmentsByName = new LinkedHashMap<>();
Map<String, OperationDefinition> 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);
}
/**
* A simple directives holder that makes it easier for {@link DirectivesContainer} classes
* to have their methods AND be efficient via immutable structures
*/
@Internal
static class DirectivesHolder implements Serializable {
private final ImmutableList<Directive> directives;
private final ImmutableMap<String, List<Directive>> directivesByName;
static DirectivesHolder of(List<Directive> directives) {
return new DirectivesHolder(directives);
}
DirectivesHolder(List<Directive> directives) {
this.directives = ImmutableList.copyOf(directives);
directivesByName = ImmutableMap.copyOf(allDirectivesByName(directives));
}
ImmutableList<Directive> getDirectives() {
return directives;
}
ImmutableMap<String, List<Directive>> getDirectivesByName() {
return directivesByName;
}
ImmutableList<Directive> getDirectives(String directiveName) {
return ImmutableList.copyOf(requireNonNull(directivesByName.getOrDefault(directiveName, ImmutableList.of())));
}
boolean hasDirective(String directiveName) {
return directivesByName.containsKey(directiveName);
}
@Override
public String toString() {
return directives.toString();
}
}
}