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
40 changes: 28 additions & 12 deletions src/main/java/graphql/normalized/ExecutableNormalizedField.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import static graphql.Assert.assertNotNull;
import static graphql.Assert.assertTrue;
import static graphql.schema.GraphQLTypeUtil.simplePrint;
import static graphql.schema.GraphQLTypeUtil.unwrapAll;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;

/**
* Intentionally Mutable
Expand Down Expand Up @@ -193,7 +195,7 @@ public boolean hasChildren() {

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

public List<ExecutableNormalizedField> getChildren(int includingRelativeLevel) {
List<ExecutableNormalizedField> result = new ArrayList<>();
assertTrue(includingRelativeLevel >= 1, () -> "relative level must be >= 1");

this.getChildren().forEach(child -> {
traverseImpl(child, result::add, 1, includingRelativeLevel);
});
return result;
}

/**
* This returns the child fields that can be used if the object is of the specified object type
*
* @param objectTypeName the object type
*
* @return a list of child fields that would apply to that object type
*/
public List<ExecutableNormalizedField> getChildren(String objectTypeName) {
return children.stream()
.filter(cld -> cld.objectTypeNames.contains(objectTypeName))
.collect(toList());
}

public int getLevel() {
return level;
}
Expand All @@ -374,19 +399,10 @@ public String toString() {
objectTypeNamesToString() + "." + fieldName +
", alias=" + alias +
", level=" + level +
", children=" + children.stream().map(ExecutableNormalizedField::toString).collect(Collectors.joining("\n")) +
", children=" + children.stream().map(ExecutableNormalizedField::toString).collect(joining("\n")) +
'}';
}

public List<ExecutableNormalizedField> getChildren(int includingRelativeLevel) {
List<ExecutableNormalizedField> result = new ArrayList<>();
assertTrue(includingRelativeLevel >= 1, () -> "relative level must be >= 1");

this.getChildren().forEach(child -> {
traverseImpl(child, result::add, 1, includingRelativeLevel);
});
return result;
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved it


public void traverseSubTree(Consumer<ExecutableNormalizedField> consumer) {
this.getChildren().forEach(child -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package graphql.normalized

import graphql.TestUtil
import graphql.execution.CoercedVariables
import graphql.language.Document
import graphql.schema.GraphQLSchema
import spock.lang.Specification

class ExecutableNormalizedFieldTest extends Specification {

def "can get children of object type"() {

String schema = """
type Query{
pets: [Pet]
}
interface Pet {
id: ID
name : String!
}
type Cat implements Pet{
id: ID
name : String!
meow : String
}
type Dog implements Pet{
id: ID
name : String!
woof : String
}
"""
GraphQLSchema graphQLSchema = TestUtil.schema(schema)

String query = """
{
pets {
id
name
... on Dog {
woof
}
... on Cat {
meow
}
}
}
"""
Document document = TestUtil.parseQuery(query)

ExecutableNormalizedOperationFactory normalizedOperationFactory = new ExecutableNormalizedOperationFactory()
def normalizedOperation = normalizedOperationFactory.createExecutableNormalizedOperation(graphQLSchema, document, null, CoercedVariables.emptyVariables())

def pets = normalizedOperation.getTopLevelFields()[0]
def allChildren = pets.getChildren()
def dogFields = pets.getChildren("Dog")

expect:
allChildren.collect { it.name } == ["id", "name", "woof", "meow"]
dogFields.collect { it.name } == ["id", "name", "woof"]
}

}