Skip to content

Commit

Permalink
Variable document compilation now handles enums properly AND also tha…
Browse files Browse the repository at this point in the history
…t some values can be null (#2784)
  • Loading branch information
bbakerman authored Apr 13, 2022
1 parent e95283f commit 2148cdf
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/main/java/graphql/language/EnumValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public EnumValue(String name) {
this(name, null, emptyList(), IgnoredChars.EMPTY, emptyMap());
}

public static EnumValue of(String name) {
return newEnumValue().name(name).build();
}

@Override
public String getName() {
return name;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/graphql/language/NullValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ protected NullValue(SourceLocation sourceLocation, List<Comment> comments, Ignor
super(sourceLocation, comments, ignoredChars, additionalData);
}

public static NullValue of() {
return NullValue.newNullValue().build();
}

@Override
public List<Node> getChildren() {
return Collections.emptyList();
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/graphql/normalized/ValueToVariableValueCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import graphql.Internal;
import graphql.language.ArrayValue;
import graphql.language.BooleanValue;
import graphql.language.EnumValue;
import graphql.language.FloatValue;
import graphql.language.IntValue;
import graphql.language.NullValue;
Expand Down Expand Up @@ -41,7 +42,7 @@ static VariableValueWithDefinition normalizedInputValueToVariable(NormalizedInpu

@SuppressWarnings("unchecked")
@Nullable
private static Object normalisedValueToVariableValue(Object maybeValue) {
static Object normalisedValueToVariableValue(Object maybeValue) {
Object variableValue;
if (maybeValue instanceof NormalizedInputValue) {
NormalizedInputValue normalizedInputValue = (NormalizedInputValue) maybeValue;
Expand All @@ -52,8 +53,10 @@ private static Object normalisedValueToVariableValue(Object maybeValue) {
variableValue = normalisedValueToVariableValues((List<Object>) inputValue);
} else if (inputValue instanceof Map) {
variableValue = normalisedValueToVariableValues((Map<String, Object>) inputValue);
} else if (inputValue == null) {
variableValue = null;
} else {
throw new AssertException("Should never happen. Did not expect NormalizedInputValue.getValue() of type: " + inputValue.getClass());
throw new AssertException("Should never happen. Did not expect NormalizedInputValue.getValue() of type: " + maybeClass(inputValue));
}
} else if (maybeValue instanceof Value) {
Value<?> value = (Value<?>) maybeValue;
Expand All @@ -63,7 +66,7 @@ private static Object normalisedValueToVariableValue(Object maybeValue) {
} else if (maybeValue instanceof Map) {
variableValue = normalisedValueToVariableValues((Map<String, Object>) maybeValue);
} else {
throw new AssertException("Should never happen. Did not expect type: " + maybeValue.getClass());
throw new AssertException("Should never happen. Did not expect type: " + maybeClass(maybeValue));
}
return variableValue;
}
Expand Down Expand Up @@ -115,10 +118,16 @@ private static Object toVariableValue(Value<?> value) {
return ((IntValue) value).getValue();
} else if (value instanceof BooleanValue) {
return ((BooleanValue) value).isValue();
} else if (value instanceof EnumValue) {
return ((EnumValue) value).getName();
} else if (value instanceof NullValue) {
return null;
}
throw new AssertException("Should never happen. Cannot handle node of type: " + value.getClass());
throw new AssertException("Should never happen. Cannot handle node of type: " + maybeClass(value));
}

private static Object maybeClass(Object maybe) {
return maybe == null ? "null" : maybe.getClass();
}

private static String getVarName(int variableOrdinal) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package graphql.normalized

import graphql.language.ArrayValue
import graphql.language.BooleanValue
import graphql.language.EnumValue
import graphql.language.FloatValue
import graphql.language.IntValue
import graphql.language.NullValue
import graphql.language.ObjectField
import graphql.language.ObjectValue
import graphql.language.StringValue
import graphql.schema.idl.TypeUtil
import spock.lang.Specification

class ValueToVariableValueCompilerTest extends Specification {

def "cam handle different ast Value objects"() {

expect:
def actual = ValueToVariableValueCompiler.normalisedValueToVariableValue(value)
actual == expected

where:
value | expected
NullValue.of() | null
IntValue.of(666) | 666
StringValue.of("str") | "str"
BooleanValue.of(true) | true
FloatValue.of(999d) | 999d
EnumValue.of("enumValue") | "enumValue"
ObjectValue.newObjectValue()
.objectField(ObjectField.newObjectField().name("a").value(IntValue.of(64)).build())
.objectField(ObjectField.newObjectField().name("b").value(StringValue.of("65")).build())
.build() | [a: 64, b: "65"]
ArrayValue.newArrayValue()
.value(IntValue.of(9))
.value(StringValue.of("10"))
.value(EnumValue.of("enum"))
.build() | [9, "10", "enum"]

}

def "can handle NormalizedInputValue values that are literals"() {
expect:
def niv = new NormalizedInputValue("TypeName", value)
def actual = ValueToVariableValueCompiler.normalisedValueToVariableValue(niv)
actual == expected

where:
value | expected
NullValue.of() | null
IntValue.of(666) | 666
StringValue.of("str") | "str"
BooleanValue.of(true) | true
FloatValue.of(999d) | 999d
EnumValue.of("enumValue") | "enumValue"
ObjectValue.newObjectValue()
.objectField(ObjectField.newObjectField().name("a").value(IntValue.of(64)).build())
.objectField(ObjectField.newObjectField().name("b").value(StringValue.of("65")).build())
.build() | [a: 64, b: "65"]
ArrayValue.newArrayValue()
.value(IntValue.of(9))
.value(StringValue.of("10"))
.value(EnumValue.of("enum"))
.build() | [9, "10", "enum"]


}

def "can handle NormalizedInputValue values that are not literals"() {
expect:
def niv = new NormalizedInputValue("TypeName", value)
def actual = ValueToVariableValueCompiler.normalisedValueToVariableValue(niv)
actual == expected

where:
value | expected
null | null
[IntValue.of(666), IntValue.of(664)] | [666, 664]
[a: IntValue.of(666), b: IntValue.of(664)] | [a: 666, b: 664]

// at present we dont handle straight up java objects like 123 because
// the ValueResolver never produces them during
// ValueResolver.getNormalizedVariableValues say - this is debatable behavior
// but for now this is what we do
}


def "can print variables as expected"() {
expect:
def niv = new NormalizedInputValue(typeName, value)
def actual = ValueToVariableValueCompiler.normalizedInputValueToVariable(niv, varCount)
actual.value == expectedValue
actual.variableReference.name == expectedVarName
actual.definition.name == expectedVarName
TypeUtil.simplePrint(actual.definition.type) == typeName

where:
value | varCount | typeName | expectedValue | expectedVarName
NullValue.newNullValue().build() | 1 | "ID" | null | "v1"
IntValue.of(666) | 2 | "Int!" | 666 | "v2"
StringValue.of("str") | 3 | "String" | "str" | "v3"
BooleanValue.of(true) | 4 | "Boolean!" | true | "v4"
FloatValue.of(999d) | 5 | "Float" | 999d | "v5"
EnumValue.of("enumValue") | 6 | "Foo!" | "enumValue" | "v6"
ObjectValue.newObjectValue()
.objectField(ObjectField.newObjectField().name("a").value(IntValue.of(64)).build())
.objectField(ObjectField.newObjectField().name("b").value(StringValue.of("65")).build())
.build() | 7 | "ObjectType" | [a: 64, b: "65"] | "v7"
ArrayValue.newArrayValue()
.value(IntValue.of(9))
.value(StringValue.of("10"))
.value(EnumValue.of("enum"))
.build() | 8 | "ArrayType" | [9, "10", "enum"] | "v8"


}
}

0 comments on commit 2148cdf

Please sign in to comment.