Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import graphql.language.Value;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

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

private static List<Selection<?>> selectionsForNormalizedFields(List<ExecutableNormalizedField> executableNormalizedFields) {
ImmutableList.Builder<Selection<?>> result = ImmutableList.builder();

Map<String, List<Field>> overallGroupedFields = new LinkedHashMap<>();
for (ExecutableNormalizedField nf : executableNormalizedFields) {
result.addAll(selectionForNormalizedField(nf));
Map<String, List<Field>> groupFieldsForChild = selectionForNormalizedField(nf);

groupFieldsForChild.forEach((objectTypeName, fields) -> {
List<Field> fieldList = overallGroupedFields.computeIfAbsent(objectTypeName, ignored -> new ArrayList<>());
fieldList.addAll(fields);
});

}

overallGroupedFields.forEach((objectTypeName, fields) -> {
TypeName typeName = newTypeName(objectTypeName).build();
InlineFragment inlineFragment = newInlineFragment().
typeCondition(typeName)
.selectionSet(selectionSet(fields))
.build();
result.add(inlineFragment);
});

return result.build();
}

private static List<Selection<?>> selectionForNormalizedField(ExecutableNormalizedField executableNormalizedField) {
List<Selection<?>> result = new ArrayList<>();
for (String objectType : executableNormalizedField.getObjectTypeNames()) {
TypeName typeName = newTypeName(objectType).build();
private static Map<String, List<Field>> selectionForNormalizedField(ExecutableNormalizedField executableNormalizedField) {
Map<String, List<Field>> groupedFields = new LinkedHashMap<>();
for (String objectTypeName : executableNormalizedField.getObjectTypeNames()) {
List<Selection<?>> subSelections = selectionsForNormalizedFields(executableNormalizedField.getChildren());
SelectionSet selectionSet = null;
if (subSelections.size() > 0) {
Expand All @@ -68,17 +86,14 @@ private static List<Selection<?>> selectionForNormalizedField(ExecutableNormaliz
.selectionSet(selectionSet)
.arguments(arguments)
.build();
InlineFragment inlineFragment = newInlineFragment().
typeCondition(typeName)
.selectionSet(selectionSet(field))
.build();
result.add(inlineFragment);

groupedFields.computeIfAbsent(objectTypeName, ignored -> new ArrayList<>()).add(field);
}
return result;
return groupedFields;
}

private static SelectionSet selectionSet(Field field) {
return newSelectionSet().selection(field).build();
private static SelectionSet selectionSet(List<Field> fields) {
return newSelectionSet().selections(fields).build();
}

private static List<Argument> createArguments(ExecutableNormalizedField executableNormalizedField) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import graphql.GraphQL
import graphql.TestUtil
import graphql.language.AstPrinter
import graphql.language.Document
import graphql.language.OperationDefinition
import graphql.schema.GraphQLSchema
import spock.lang.Specification

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

class ExecutableNormalizedOperationToAstCompilerTest extends Specification {
def "test"() {

def "test pet interfaces"() {
String sdl = """
type Query {
animal: Animal
Expand Down Expand Up @@ -42,6 +42,7 @@ class ExecutableNormalizedOperationToAstCompilerTest extends Specification {
name: String
friends: [Friend]
breed: String
mood: String
}
type Dog implements Animal {
Expand All @@ -61,6 +62,7 @@ class ExecutableNormalizedOperationToAstCompilerTest extends Specification {
}
... on Cat {
name
mood
friends {
... on Friend {
isCatOwner
Expand All @@ -87,6 +89,7 @@ class ExecutableNormalizedOperationToAstCompilerTest extends Specification {
}
... on Dog {
name
breed
}
}
}
Expand All @@ -95,34 +98,17 @@ class ExecutableNormalizedOperationToAstCompilerTest extends Specification {
def fields = createNormalizedFields(schema, query)
when:
def document = compileToDocument(QUERY, fields)
def printed = AstPrinter.printAst(document)
then:
AstPrinter.printAst(document) == '''query {
printed == '''query {
... on Query {
animal {
... on Bird {
name
}
... on Cat {
name
}
... on Dog {
name
}
... on Bird {
otherName: name
}
... on Cat {
otherName: name
}
... on Dog {
otherName: name
}
... on Cat {
friends {
... on Friend {
isCatOwner
}
... on Friend {
pets {
... on Dog {
name
Expand All @@ -131,21 +117,18 @@ class ExecutableNormalizedOperationToAstCompilerTest extends Specification {
breed
}
}
}
... on Friend {
isBirdOwner
}
... on Friend {
name
}
}
}
... on Bird {
... on Cat {
name
otherName: name
mood
friends {
... on Friend {
isCatOwner
}
... on Friend {
pets {
... on Dog {
name
Expand All @@ -154,20 +137,90 @@ class ExecutableNormalizedOperationToAstCompilerTest extends Specification {
breed
}
}
}
... on Friend {
isBirdOwner
}
... on Friend {
name
}
}
}
... on Dog {
name
otherName: name
breed
}
}
}
}
'''
}

def "test a combination of plain objects and interfaces"() {
def sdl = '''
type Query {
foo(arg: I): Foo
}
type Foo {
bar(arg: I): Bar
}
type Bar {
baz : Baz
}
interface Baz {
boo : String
}
type ABaz implements Baz {
boo : String
a : String
}
type BBaz implements Baz {
boo : String
b : String
}
input I {
arg1: String
}
'''
def query = '''query {
foo(arg: {arg1 : "fooArg"}) {
bar(arg: {arg1 : "barArg"}) {
baz {
... on ABaz {
boo
a
}
}
}
}
}
'''
GraphQLSchema schema = TestUtil.schema(sdl)
def fields = createNormalizedFields(schema, query)
when:
def document = compileToDocument(QUERY, fields)
then:
AstPrinter.printAst(document) == '''query {
... on Query {
foo(arg: {arg1 : "fooArg"}) {
... on Foo {
bar(arg: {arg1 : "barArg"}) {
... on Bar {
baz {
... on ABaz {
boo
a
}
}
}
}
}
}
}
}
'''
}

def "test arguments"() {
Expand All @@ -190,8 +243,6 @@ class ExecutableNormalizedOperationToAstCompilerTest extends Specification {
AstPrinter.printAst(document) == '''query {
... on Query {
foo1(arg: "hello")
}
... on Query {
foo2(a: 123, b: true, c: 123.45)
}
}
Expand Down