Skip to content

Commit dfc3a92

Browse files
authored
groups together fields into the one object type during AST compilation (#2501)
1 parent fd86181 commit dfc3a92

2 files changed

Lines changed: 113 additions & 47 deletions

File tree

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

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import graphql.language.Value;
1919

2020
import java.util.ArrayList;
21+
import java.util.LinkedHashMap;
2122
import java.util.List;
2223
import java.util.Map;
2324

@@ -44,16 +45,33 @@ public static Document compileToDocument(OperationDefinition.Operation operation
4445

4546
private static List<Selection<?>> selectionsForNormalizedFields(List<ExecutableNormalizedField> executableNormalizedFields) {
4647
ImmutableList.Builder<Selection<?>> result = ImmutableList.builder();
48+
49+
Map<String, List<Field>> overallGroupedFields = new LinkedHashMap<>();
4750
for (ExecutableNormalizedField nf : executableNormalizedFields) {
48-
result.addAll(selectionForNormalizedField(nf));
51+
Map<String, List<Field>> groupFieldsForChild = selectionForNormalizedField(nf);
52+
53+
groupFieldsForChild.forEach((objectTypeName, fields) -> {
54+
List<Field> fieldList = overallGroupedFields.computeIfAbsent(objectTypeName, ignored -> new ArrayList<>());
55+
fieldList.addAll(fields);
56+
});
57+
4958
}
59+
60+
overallGroupedFields.forEach((objectTypeName, fields) -> {
61+
TypeName typeName = newTypeName(objectTypeName).build();
62+
InlineFragment inlineFragment = newInlineFragment().
63+
typeCondition(typeName)
64+
.selectionSet(selectionSet(fields))
65+
.build();
66+
result.add(inlineFragment);
67+
});
68+
5069
return result.build();
5170
}
5271

53-
private static List<Selection<?>> selectionForNormalizedField(ExecutableNormalizedField executableNormalizedField) {
54-
List<Selection<?>> result = new ArrayList<>();
55-
for (String objectType : executableNormalizedField.getObjectTypeNames()) {
56-
TypeName typeName = newTypeName(objectType).build();
72+
private static Map<String, List<Field>> selectionForNormalizedField(ExecutableNormalizedField executableNormalizedField) {
73+
Map<String, List<Field>> groupedFields = new LinkedHashMap<>();
74+
for (String objectTypeName : executableNormalizedField.getObjectTypeNames()) {
5775
List<Selection<?>> subSelections = selectionsForNormalizedFields(executableNormalizedField.getChildren());
5876
SelectionSet selectionSet = null;
5977
if (subSelections.size() > 0) {
@@ -68,17 +86,14 @@ private static List<Selection<?>> selectionForNormalizedField(ExecutableNormaliz
6886
.selectionSet(selectionSet)
6987
.arguments(arguments)
7088
.build();
71-
InlineFragment inlineFragment = newInlineFragment().
72-
typeCondition(typeName)
73-
.selectionSet(selectionSet(field))
74-
.build();
75-
result.add(inlineFragment);
89+
90+
groupedFields.computeIfAbsent(objectTypeName, ignored -> new ArrayList<>()).add(field);
7691
}
77-
return result;
92+
return groupedFields;
7893
}
7994

80-
private static SelectionSet selectionSet(Field field) {
81-
return newSelectionSet().selection(field).build();
95+
private static SelectionSet selectionSet(List<Field> fields) {
96+
return newSelectionSet().selections(fields).build();
8297
}
8398

8499
private static List<Argument> createArguments(ExecutableNormalizedField executableNormalizedField) {

‎src/test/groovy/graphql/normalized/ExecutableNormalizedOperationToAstCompilerTest.groovy‎

Lines changed: 85 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import graphql.GraphQL
44
import graphql.TestUtil
55
import graphql.language.AstPrinter
66
import graphql.language.Document
7-
import graphql.language.OperationDefinition
87
import graphql.schema.GraphQLSchema
98
import spock.lang.Specification
109

@@ -14,7 +13,8 @@ import static graphql.language.OperationDefinition.Operation.SUBSCRIPTION
1413
import static graphql.normalized.ExecutableNormalizedOperationToAstCompiler.compileToDocument
1514

1615
class ExecutableNormalizedOperationToAstCompilerTest extends Specification {
17-
def "test"() {
16+
17+
def "test pet interfaces"() {
1818
String sdl = """
1919
type Query {
2020
animal: Animal
@@ -42,6 +42,7 @@ class ExecutableNormalizedOperationToAstCompilerTest extends Specification {
4242
name: String
4343
friends: [Friend]
4444
breed: String
45+
mood: String
4546
}
4647
4748
type Dog implements Animal {
@@ -61,6 +62,7 @@ class ExecutableNormalizedOperationToAstCompilerTest extends Specification {
6162
}
6263
... on Cat {
6364
name
65+
mood
6466
friends {
6567
... on Friend {
6668
isCatOwner
@@ -87,6 +89,7 @@ class ExecutableNormalizedOperationToAstCompilerTest extends Specification {
8789
}
8890
... on Dog {
8991
name
92+
breed
9093
}
9194
}
9295
}
@@ -95,34 +98,17 @@ class ExecutableNormalizedOperationToAstCompilerTest extends Specification {
9598
def fields = createNormalizedFields(schema, query)
9699
when:
97100
def document = compileToDocument(QUERY, fields)
101+
def printed = AstPrinter.printAst(document)
98102
then:
99-
AstPrinter.printAst(document) == '''query {
103+
printed == '''query {
100104
... on Query {
101105
animal {
102106
... on Bird {
103107
name
104-
}
105-
... on Cat {
106-
name
107-
}
108-
... on Dog {
109-
name
110-
}
111-
... on Bird {
112108
otherName: name
113-
}
114-
... on Cat {
115-
otherName: name
116-
}
117-
... on Dog {
118-
otherName: name
119-
}
120-
... on Cat {
121109
friends {
122110
... on Friend {
123111
isCatOwner
124-
}
125-
... on Friend {
126112
pets {
127113
... on Dog {
128114
name
@@ -131,21 +117,18 @@ class ExecutableNormalizedOperationToAstCompilerTest extends Specification {
131117
breed
132118
}
133119
}
134-
}
135-
... on Friend {
136120
isBirdOwner
137-
}
138-
... on Friend {
139121
name
140122
}
141123
}
142124
}
143-
... on Bird {
125+
... on Cat {
126+
name
127+
otherName: name
128+
mood
144129
friends {
145130
... on Friend {
146131
isCatOwner
147-
}
148-
... on Friend {
149132
pets {
150133
... on Dog {
151134
name
@@ -154,20 +137,90 @@ class ExecutableNormalizedOperationToAstCompilerTest extends Specification {
154137
breed
155138
}
156139
}
157-
}
158-
... on Friend {
159140
isBirdOwner
160-
}
161-
... on Friend {
162141
name
163142
}
164143
}
165144
}
145+
... on Dog {
146+
name
147+
otherName: name
148+
breed
149+
}
166150
}
167151
}
168152
}
169153
'''
154+
}
155+
156+
def "test a combination of plain objects and interfaces"() {
157+
def sdl = '''
158+
type Query {
159+
foo(arg: I): Foo
160+
}
161+
type Foo {
162+
bar(arg: I): Bar
163+
}
164+
type Bar {
165+
baz : Baz
166+
}
167+
168+
interface Baz {
169+
boo : String
170+
}
171+
172+
type ABaz implements Baz {
173+
boo : String
174+
a : String
175+
}
170176
177+
type BBaz implements Baz {
178+
boo : String
179+
b : String
180+
}
181+
182+
183+
input I {
184+
arg1: String
185+
}
186+
'''
187+
def query = '''query {
188+
foo(arg: {arg1 : "fooArg"}) {
189+
bar(arg: {arg1 : "barArg"}) {
190+
baz {
191+
... on ABaz {
192+
boo
193+
a
194+
}
195+
}
196+
}
197+
}
198+
}
199+
'''
200+
GraphQLSchema schema = TestUtil.schema(sdl)
201+
def fields = createNormalizedFields(schema, query)
202+
when:
203+
def document = compileToDocument(QUERY, fields)
204+
then:
205+
AstPrinter.printAst(document) == '''query {
206+
... on Query {
207+
foo(arg: {arg1 : "fooArg"}) {
208+
... on Foo {
209+
bar(arg: {arg1 : "barArg"}) {
210+
... on Bar {
211+
baz {
212+
... on ABaz {
213+
boo
214+
a
215+
}
216+
}
217+
}
218+
}
219+
}
220+
}
221+
}
222+
}
223+
'''
171224
}
172225

173226
def "test arguments"() {
@@ -190,8 +243,6 @@ class ExecutableNormalizedOperationToAstCompilerTest extends Specification {
190243
AstPrinter.printAst(document) == '''query {
191244
... on Query {
192245
foo1(arg: "hello")
193-
}
194-
... on Query {
195246
foo2(a: 123, b: true, c: 123.45)
196247
}
197248
}

0 commit comments

Comments
 (0)