Skip to content

Commit 12def0f

Browse files
authored
Helper for getting fields based on object type name (#3016)
1 parent 1a78669 commit 12def0f

2 files changed

Lines changed: 91 additions & 12 deletions

File tree

‎src/main/java/graphql/normalized/ExecutableNormalizedField.java‎

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@
2828
import java.util.Map;
2929
import java.util.Set;
3030
import java.util.function.Consumer;
31-
import java.util.stream.Collectors;
3231

3332
import static graphql.Assert.assertNotNull;
3433
import static graphql.Assert.assertTrue;
3534
import static graphql.schema.GraphQLTypeUtil.simplePrint;
3635
import static graphql.schema.GraphQLTypeUtil.unwrapAll;
36+
import static java.util.stream.Collectors.joining;
37+
import static java.util.stream.Collectors.toList;
38+
import static java.util.stream.Collectors.toSet;
3739

3840
/**
3941
* Intentionally Mutable
@@ -193,7 +195,7 @@ public boolean hasChildren() {
193195

194196
public GraphQLOutputType getType(GraphQLSchema schema) {
195197
List<GraphQLFieldDefinition> fieldDefinitions = getFieldDefinitions(schema);
196-
Set<String> fieldTypes = fieldDefinitions.stream().map(fd -> simplePrint(fd.getType())).collect(Collectors.toSet());
198+
Set<String> fieldTypes = fieldDefinitions.stream().map(fd -> simplePrint(fd.getType())).collect(toSet());
197199
Assert.assertTrue(fieldTypes.size() == 1, () -> "More than one type ... use getTypes");
198200
return fieldDefinitions.get(0).getType();
199201
}
@@ -355,6 +357,29 @@ public List<ExecutableNormalizedField> getChildrenWithSameResultKey(String resul
355357
return FpKit.filterList(children, child -> child.getResultKey().equals(resultKey));
356358
}
357359

360+
public List<ExecutableNormalizedField> getChildren(int includingRelativeLevel) {
361+
List<ExecutableNormalizedField> result = new ArrayList<>();
362+
assertTrue(includingRelativeLevel >= 1, () -> "relative level must be >= 1");
363+
364+
this.getChildren().forEach(child -> {
365+
traverseImpl(child, result::add, 1, includingRelativeLevel);
366+
});
367+
return result;
368+
}
369+
370+
/**
371+
* This returns the child fields that can be used if the object is of the specified object type
372+
*
373+
* @param objectTypeName the object type
374+
*
375+
* @return a list of child fields that would apply to that object type
376+
*/
377+
public List<ExecutableNormalizedField> getChildren(String objectTypeName) {
378+
return children.stream()
379+
.filter(cld -> cld.objectTypeNames.contains(objectTypeName))
380+
.collect(toList());
381+
}
382+
358383
public int getLevel() {
359384
return level;
360385
}
@@ -374,19 +399,10 @@ public String toString() {
374399
objectTypeNamesToString() + "." + fieldName +
375400
", alias=" + alias +
376401
", level=" + level +
377-
", children=" + children.stream().map(ExecutableNormalizedField::toString).collect(Collectors.joining("\n")) +
402+
", children=" + children.stream().map(ExecutableNormalizedField::toString).collect(joining("\n")) +
378403
'}';
379404
}
380405

381-
public List<ExecutableNormalizedField> getChildren(int includingRelativeLevel) {
382-
List<ExecutableNormalizedField> result = new ArrayList<>();
383-
assertTrue(includingRelativeLevel >= 1, () -> "relative level must be >= 1");
384-
385-
this.getChildren().forEach(child -> {
386-
traverseImpl(child, result::add, 1, includingRelativeLevel);
387-
});
388-
return result;
389-
}
390406

391407
public void traverseSubTree(Consumer<ExecutableNormalizedField> consumer) {
392408
this.getChildren().forEach(child -> {
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package graphql.normalized
2+
3+
import graphql.TestUtil
4+
import graphql.execution.CoercedVariables
5+
import graphql.language.Document
6+
import graphql.schema.GraphQLSchema
7+
import spock.lang.Specification
8+
9+
class ExecutableNormalizedFieldTest extends Specification {
10+
11+
def "can get children of object type"() {
12+
13+
String schema = """
14+
type Query{
15+
pets: [Pet]
16+
}
17+
interface Pet {
18+
id: ID
19+
name : String!
20+
}
21+
type Cat implements Pet{
22+
id: ID
23+
name : String!
24+
meow : String
25+
}
26+
type Dog implements Pet{
27+
id: ID
28+
name : String!
29+
woof : String
30+
}
31+
"""
32+
GraphQLSchema graphQLSchema = TestUtil.schema(schema)
33+
34+
String query = """
35+
{
36+
pets {
37+
id
38+
name
39+
... on Dog {
40+
woof
41+
}
42+
... on Cat {
43+
meow
44+
}
45+
}
46+
}
47+
48+
"""
49+
Document document = TestUtil.parseQuery(query)
50+
51+
ExecutableNormalizedOperationFactory normalizedOperationFactory = new ExecutableNormalizedOperationFactory()
52+
def normalizedOperation = normalizedOperationFactory.createExecutableNormalizedOperation(graphQLSchema, document, null, CoercedVariables.emptyVariables())
53+
54+
def pets = normalizedOperation.getTopLevelFields()[0]
55+
def allChildren = pets.getChildren()
56+
def dogFields = pets.getChildren("Dog")
57+
58+
expect:
59+
allChildren.collect { it.name } == ["id", "name", "woof", "meow"]
60+
dogFields.collect { it.name } == ["id", "name", "woof"]
61+
}
62+
63+
}

0 commit comments

Comments
 (0)