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
217 changes: 134 additions & 83 deletions src/main/java/graphql/Scalars.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ private void handlePrimitives(List<GraphQLExecutionNodeValue> values, String fie
}

private Object coerce(GraphQLType type, Object value) {
if (value == null) return null;
if (type instanceof GraphQLEnumType) {
return ((GraphQLEnumType) type).getCoercing().serialize(value);
} else {
Expand Down
22 changes: 12 additions & 10 deletions src/main/java/graphql/schema/GraphQLEnumType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@


import graphql.AssertException;
import graphql.GraphQLException;
import graphql.Internal;
import graphql.PublicApi;
import graphql.language.EnumTypeDefinition;
import graphql.language.EnumValue;

Expand All @@ -10,8 +13,10 @@
import java.util.List;
import java.util.Map;

import static graphql.Assert.assertNotNull;
import static graphql.Assert.assertValidName;

@PublicApi
public class GraphQLEnumType implements GraphQLType, GraphQLInputType, GraphQLOutputType, GraphQLUnmodifiedType {

private final String name;
Expand Down Expand Up @@ -41,10 +46,12 @@ public Object parseLiteral(Object input) {
};


@Internal
public GraphQLEnumType(String name, String description, List<GraphQLEnumValueDefinition> values) {
this(name, description, values, null);
}

@Internal
public GraphQLEnumType(String name, String description, List<GraphQLEnumValueDefinition> values, EnumTypeDefinition definition) {
assertValidName(name);
this.name = name;
Expand Down Expand Up @@ -73,20 +80,14 @@ private void buildMap(List<GraphQLEnumValueDefinition> values) {
private Object getValueByName(Object value) {
GraphQLEnumValueDefinition enumValueDefinition = valueDefinitionMap.get(value.toString());
if (enumValueDefinition != null) return enumValueDefinition.getValue();
return null;
throw new GraphQLException("Invalid input for Enum '" + name + "'. No value found for name " + value.toString());
}

private Object getNameByValue(Object value) {
if (value == null) {
for (GraphQLEnumValueDefinition valueDefinition : valueDefinitionMap.values()) {
if (valueDefinition.getValue() == null) return valueDefinition.getName();
}
} else {
for (GraphQLEnumValueDefinition valueDefinition : valueDefinitionMap.values()) {
if (value.equals(valueDefinition.getValue())) return valueDefinition.getName();
}
for (GraphQLEnumValueDefinition valueDefinition : valueDefinitionMap.values()) {
if (value.equals(valueDefinition.getValue())) return valueDefinition.getName();
}
return null;
throw new GraphQLException("Invalid input for Enum '" + name + "'. Unknown value " + value);
}


Expand Down Expand Up @@ -143,6 +144,7 @@ public Builder value(String name, Object value, String description) {
}

public Builder value(String name, Object value) {
assertNotNull(value, "value can't be null");
values.add(new GraphQLEnumValueDefinition(name, null, value));
return this;
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/graphql/schema/GraphQLEnumValueDefinition.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package graphql.schema;


import static graphql.Assert.assertNotNull;
import graphql.PublicApi;

import static graphql.Assert.assertValidName;

@PublicApi
public class GraphQLEnumValueDefinition {

private final String name;
Expand All @@ -12,7 +14,7 @@ public class GraphQLEnumValueDefinition {
private final String deprecationReason;

public GraphQLEnumValueDefinition(String name, String description, Object value, String deprecationReason) {
assertValidName(name);
assertValidName(name);
this.name = name;
this.description = description;
this.value = value;
Expand Down
5 changes: 2 additions & 3 deletions src/test/groovy/graphql/GraphQLTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,8 @@ class GraphQLTest extends Specification {
def result = GraphQL.newGraphQL(schema).build().execute(query)

then:
// This should result in an ArgumentsOfCorrectType error
thrown(GraphQLException)
// result.errors.size() == 1
result.errors.size() == 1
result.errors[0].description.contains("has wrong type")
}

def "query with missing argument results in arguments map with value null"() {
Expand Down
85 changes: 85 additions & 0 deletions src/test/groovy/graphql/ScalarsBigDecimalTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package graphql

import graphql.language.BooleanValue
import graphql.language.FloatValue
import graphql.language.IntValue
import graphql.language.StringValue
import spock.lang.Specification
import spock.lang.Unroll

import java.util.concurrent.atomic.AtomicInteger

class ScalarsBigDecimalTest extends Specification {

@Unroll
def "BigDecimal parse literal #literal.value as #result"() {
expect:
Scalars.GraphQLBigDecimal.getCoercing().parseLiteral(literal) == result

where:
literal | result
new IntValue(12345678910 as BigInteger) | new BigDecimal("12345678910")
new StringValue("12345678911.12") | new BigDecimal("12345678911.12")
new FloatValue(new BigDecimal("42.42")) | new BigDecimal("42.42")

}

@Unroll
def "BigDecimal returns null for invalid #literal"() {
expect:
Scalars.GraphQLBigDecimal.getCoercing().parseLiteral(literal) == null

where:
literal | _
new BooleanValue(true) | _
new StringValue("not a number") | _
}

@Unroll
def "BigDecimal serialize #value into #result (#result.class)"() {
expect:
Scalars.GraphQLBigDecimal.getCoercing().serialize(value) == result
Scalars.GraphQLBigDecimal.getCoercing().parseValue(value) == result

where:
value | result
"42" | new BigDecimal("42")
"42.123" | new BigDecimal("42.123")
42.0000d | new BigDecimal("42.000")
new Integer(42) | new BigDecimal("42")
"-1" | new BigDecimal("-1")
new BigInteger(42) | new BigDecimal("42")
new BigDecimal("42") | new BigDecimal("42")
42.3f | new BigDecimal("42.3")
42.0d | new BigDecimal("42")
new Byte("42") | new BigDecimal("42")
new Short("42") | new BigDecimal("42")
1234567l | new BigDecimal("1234567")
new AtomicInteger(42) | new BigDecimal("42")
}

@Unroll
def "serialize/parseValue throws exception for invalid input #value"() {
expect:
try {
Scalars.GraphQLBigDecimal.getCoercing().serialize(value)
assert false: "no exception thrown"
} catch (GraphQLException e) {
// expected
}
try {
Scalars.GraphQLBigDecimal.getCoercing().parseValue(value)
assert false: "no exception thrown"
} catch (GraphQLException e) {
// expected
}

where:
value | _
"" | _
"not a number " | _
new Object() | _
}


}
84 changes: 84 additions & 0 deletions src/test/groovy/graphql/ScalarsBigIntegerTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package graphql

import graphql.language.BooleanValue
import graphql.language.FloatValue
import graphql.language.IntValue
import graphql.language.StringValue
import spock.lang.Specification
import spock.lang.Unroll

import java.util.concurrent.atomic.AtomicInteger

class ScalarsBigIntegerTest extends Specification {

@Unroll
def "BigInteger parse literal #literal.value as #result"() {
expect:
Scalars.GraphQLBigInteger.getCoercing().parseLiteral(literal) == result

where:
literal | result
new IntValue(12345678910 as BigInteger) | new BigInteger("12345678910")
new StringValue("12345678911") | new BigInteger("12345678911")
new FloatValue(new BigDecimal("42")) | new BigInteger("42")
}

@Unroll
def "BigInteger returns null for invalid #literal"() {
expect:
Scalars.GraphQLBigInteger.getCoercing().parseLiteral(literal) == null

where:
literal | _
new BooleanValue(true) | _
new StringValue("42.3") | _
new FloatValue(new BigDecimal("12.12")) | _
new StringValue("not a number") | _
}

@Unroll
def "BigInteger serialize #value into #result (#result.class)"() {
expect:
Scalars.GraphQLBigInteger.getCoercing().serialize(value) == result
Scalars.GraphQLBigInteger.getCoercing().parseValue(value) == result

where:
value | result
"42" | new BigInteger("42")
new Integer(42) | new BigInteger("42")
"-1" | new BigInteger("-1")
new BigInteger("42") | new BigInteger("42")
42.0d | new BigInteger("42")
new Byte("42") | new BigInteger("42")
new Short("42") | new BigInteger("42")
1234567l | new BigInteger("1234567")
new AtomicInteger(42) | new BigInteger("42")
}

@Unroll
def "serialize/parseValue throws exception for invalid input #value"() {
expect:
try {
Scalars.GraphQLBigInteger.getCoercing().serialize(value)
assert false: "no exception thrown"
} catch (GraphQLException e) {
// expected
}
try {
Scalars.GraphQLBigInteger.getCoercing().parseValue(value)
assert false: "no exception thrown"
} catch (GraphQLException e) {
// expected
}

where:
value | _
"" | _
"not a number " | _
new BigDecimal("12.12") | _
"12.12" | _
new Object() | _
}


}
82 changes: 82 additions & 0 deletions src/test/groovy/graphql/ScalarsBooleanTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package graphql

import graphql.language.BooleanValue
import graphql.language.FloatValue
import graphql.language.IntValue
import graphql.language.StringValue
import spock.lang.Specification
import spock.lang.Unroll

class ScalarsBooleanTest extends Specification {

@Unroll
def "Boolean parse literal #literal.value as #result"() {
expect:
Scalars.GraphQLBoolean.getCoercing().parseLiteral(literal) == result

where:
literal | result
new BooleanValue(true) | true
new BooleanValue(false) | false

}

@Unroll
def "Boolean returns null for invalid #literal"() {
expect:
Scalars.GraphQLBoolean.getCoercing().parseLiteral(literal) == null

where:
literal | _
new IntValue(12345678910 as BigInteger) | _
new StringValue("-1") | _
new FloatValue(42.3) | _
}

@Unroll
def "Boolean serialize #value into #result (#result.class)"() {
expect:
Scalars.GraphQLBoolean.getCoercing().serialize(value) == result
Scalars.GraphQLBoolean.getCoercing().parseValue(value) == result

where:
value | result
true | true
"false" | false
"true" | true
"True" | true
"some value not true" | false
"" | false
0 | false
1 | true
-1 | true
new Long(42345784398534785l) | true
new Double(42.3) | true
new Float(42.3) | true
Integer.MAX_VALUE + 1l | true
Integer.MIN_VALUE - 1l | true
}

@Unroll
def "serialize/parseValue throws exception for invalid input #value"() {
expect:
try {
Scalars.GraphQLBoolean.getCoercing().serialize(value)
assert false: "no exception thrown"
} catch (GraphQLException e) {
// expected
}
try {
Scalars.GraphQLBoolean.getCoercing().parseValue(value)
assert false: "no exception thrown"
} catch (GraphQLException e) {
// expected
}

where:
value | _
new Object() | _
}


}
Loading