Skip to content
410 changes: 262 additions & 148 deletions src/main/java/graphql/execution/ValuesResolver.java

Large diffs are not rendered by default.

21 changes: 16 additions & 5 deletions src/main/java/graphql/normalized/NormalizedField.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,22 @@
import static graphql.schema.GraphQLTypeUtil.unwrapAll;

/**
* Intentionally Mutable: parent and object types can be mutated.
* Intentionally Mutable
*/
@Internal
public class NormalizedField {
private final String alias;
private final ImmutableMap<String, NormalizedInputValue> normalizedArguments;
private final ImmutableMap<String, Object> resolvedArguments;
private final ImmutableList<Argument> astArguments;

// Mutable List on purpose: it is modified after creation
private final LinkedHashSet<String> objectTypeNames;
private final ArrayList<NormalizedField> children;
private NormalizedField parent;

private final String fieldName;
private final List<NormalizedField> children;
private final int level;
private NormalizedField parent;


private NormalizedField(Builder builder) {
Expand Down Expand Up @@ -107,6 +109,15 @@ public void addObjectTypeNames(Collection<String> objectTypeNames) {
this.objectTypeNames.addAll(objectTypeNames);
}

public void addChild(NormalizedField normalizedField) {
this.children.add(normalizedField);
}

public void clearChildren() {
this.children.clear();
;
}

/**
* All merged fields have the same name.
* <p>
Expand Down Expand Up @@ -267,7 +278,7 @@ private void traverseImpl(NormalizedField root,
public static class Builder {
private LinkedHashSet<String> objectTypeNames = new LinkedHashSet<>();
private String fieldName;
private List<NormalizedField> children = new ArrayList<>();
private ArrayList<NormalizedField> children = new ArrayList<>();
private int level;
private NormalizedField parent;
private String alias;
Expand All @@ -286,7 +297,7 @@ private Builder(NormalizedField existing) {
this.resolvedArguments = existing.resolvedArguments;
this.objectTypeNames = new LinkedHashSet<>(existing.getObjectTypeNames());
this.fieldName = existing.getFieldName();
this.children = existing.getChildren();
this.children = new ArrayList<>(existing.children);
this.level = existing.getLevel();
this.parent = existing.getParent();
}
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/graphql/normalized/NormalizedInputValue.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package graphql.normalized;

import graphql.language.Value;

import java.util.Objects;

import static graphql.language.AstPrinter.printAst;

/**
* A value with type information.
*/
Expand All @@ -25,9 +29,9 @@ public String getTypeName() {

/**
* Depending on the type it returns:
* Scalar or Enum: the value of the Scalar.
* Scalar or Enum: the ast literal of the Scalar.
* InputObject: the value is a map of field-name to NormalizedInputValue
* List of Scalar/Enum/InputObject (or even List of List ..)
* List of Scalar literal or Enum literal or NormalizedInput (or even List of List ..)
*
* @return
*/
Expand Down Expand Up @@ -62,6 +66,9 @@ public boolean equals(Object o) {
return false;
}
NormalizedInputValue that = (NormalizedInputValue) o;
if (value instanceof Value && that.value instanceof Value) {
return Objects.equals(typeName, that.typeName) && Objects.equals(printAst((Value) value), printAst((Value) that.value));
}
return Objects.equals(typeName, that.typeName) && Objects.equals(value, that.value);
}

Expand Down
118 changes: 118 additions & 0 deletions src/main/java/graphql/normalized/NormalizedQueryToAstCompiler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package graphql.normalized;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import graphql.Internal;
import graphql.language.Argument;
import graphql.language.ArrayValue;
import graphql.language.Document;
import graphql.language.Field;
import graphql.language.InlineFragment;
import graphql.language.NullValue;
import graphql.language.ObjectField;
import graphql.language.ObjectValue;
import graphql.language.Selection;
import graphql.language.SelectionSet;
import graphql.language.TypeName;
import graphql.language.Value;

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

import static graphql.collect.ImmutableKit.map;
import static graphql.language.Argument.newArgument;
import static graphql.language.Field.newField;
import static graphql.language.InlineFragment.newInlineFragment;
import static graphql.language.OperationDefinition.Operation.QUERY;
import static graphql.language.OperationDefinition.newOperationDefinition;
import static graphql.language.SelectionSet.newSelectionSet;
import static graphql.language.TypeName.newTypeName;

@Internal
public class NormalizedQueryToAstCompiler {

public static Document compileToDocument(List<NormalizedField> topLevelFields) {
List<Selection<?>> selections = selectionsForNormalizedFields(topLevelFields);
SelectionSet selectionSet = newSelectionSet(selections).build();
Document document = Document.newDocument().definition(newOperationDefinition()
.operation(QUERY)
.selectionSet(selectionSet)
.build())
.build();
return document;
}

private static List<Selection<?>> selectionsForNormalizedFields(List<NormalizedField> normalizedFields) {
ImmutableList.Builder<Selection<?>> result = ImmutableList.builder();
for (NormalizedField nf : normalizedFields) {
result.addAll(selectionForNormalizedField(nf));
}
return result.build();
}

private static List<Selection<?>> selectionForNormalizedField(NormalizedField normalizedField) {
List<Selection<?>> result = new ArrayList<>();
for (String objectType : normalizedField.getObjectTypeNames()) {
TypeName typeName = newTypeName(objectType).build();
List<Selection<?>> subSelections = selectionsForNormalizedFields(normalizedField.getChildren());
SelectionSet selectionSet = null;
if (subSelections.size() > 0) {
selectionSet = newSelectionSet()
.selections(subSelections)
.build();
}
List<Argument> arguments = createArguments(normalizedField);
Field field = newField()
.name(normalizedField.getFieldName())
.alias(normalizedField.getAlias())
.selectionSet(selectionSet)
.arguments(arguments)
.build();
InlineFragment inlineFragment = newInlineFragment().
typeCondition(typeName)
.selectionSet(selectionSet(field))
.build();
result.add(inlineFragment);
}
return result;
}

private static SelectionSet selectionSet(Field field) {
return newSelectionSet().selection(field).build();
}

private static List<Argument> createArguments(NormalizedField normalizedField) {
ImmutableList.Builder<Argument> result = ImmutableList.builder();
ImmutableMap<String, NormalizedInputValue> normalizedArguments = normalizedField.getNormalizedArguments();
for (String argName : normalizedArguments.keySet()) {
Argument argument = newArgument()
.name(argName)
.value(argValue(normalizedArguments.get(argName).getValue()))
.build();
result.add(argument);
}
return result.build();
}

private static Value<?> argValue(Object value) {
if (value instanceof List) {
ArrayValue.Builder arrayValue = ArrayValue.newArrayValue();
arrayValue.values(map((List<Object>) value, NormalizedQueryToAstCompiler::argValue));
return arrayValue.build();
}
if (value instanceof Map) {
ObjectValue.Builder objectValue = ObjectValue.newObjectValue();
Map<String, Object> map = (Map<String, Object>) value;
for (String fieldName : map.keySet()) {
Value<?> fieldValue = argValue(((NormalizedInputValue) map.get(fieldName)).getValue());
objectValue.objectField(ObjectField.newObjectField().name(fieldName).value(fieldValue).build());
}
return objectValue.build();
}
if (value == null) {
return NullValue.newNullValue().build();
}
return (Value<?>) value;
}
}
Loading