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
247 changes: 194 additions & 53 deletions src/main/java/graphql/Scalars.java

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions src/main/java/graphql/SerializationError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package graphql;


import graphql.language.SourceLocation;
import graphql.schema.CoercingSerializeException;

import java.util.List;

@PublicApi
public class SerializationError implements GraphQLError {

private final CoercingSerializeException exception;

public SerializationError(CoercingSerializeException exception) {
this.exception = exception;
}

public CoercingSerializeException getException() {
return exception;
}


@Override
public String getMessage() {
return "Can't serialize value: " + exception.getMessage();
}

@Override
public List<SourceLocation> getLocations() {
return null;
}

@Override
public ErrorType getErrorType() {
return ErrorType.DataFetchingException;
}

@Override
public String toString() {
return "ExceptionWhileDataFetching{" +
"exception=" + exception +
'}';
}

@Override
public boolean equals(Object o) {
return Helper.equals(this, o);
}

@Override
public int hashCode() {
return Helper.hashCode(this);
}
}
28 changes: 22 additions & 6 deletions src/main/java/graphql/execution/ExecutionStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import graphql.ExecutionResultImpl;
import graphql.GraphQLException;
import graphql.PublicSpi;
import graphql.SerializationError;
import graphql.TypeResolutionEnvironment;
import graphql.execution.instrumentation.Instrumentation;
import graphql.execution.instrumentation.InstrumentationContext;
import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters;
import graphql.execution.instrumentation.parameters.InstrumentationFieldParameters;
import graphql.language.Field;
import graphql.schema.CoercingSerializeException;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.DataFetchingEnvironmentImpl;
Expand Down Expand Up @@ -141,9 +143,9 @@ protected ExecutionResult completeValue(ExecutionContext executionContext, Execu
} else if (fieldType instanceof GraphQLList) {
return completeValueForList(executionContext, parameters, fields, toIterable(result));
} else if (fieldType instanceof GraphQLScalarType) {
return completeValueForScalar((GraphQLScalarType) fieldType, parameters, result);
return completeValueForScalar((GraphQLScalarType) fieldType, parameters, result, executionContext);
} else if (fieldType instanceof GraphQLEnumType) {
return completeValueForEnum((GraphQLEnumType) fieldType, parameters, result);
return completeValueForEnum((GraphQLEnumType) fieldType, parameters, result, executionContext);
}


Expand Down Expand Up @@ -219,14 +221,28 @@ protected GraphQLObjectType resolveTypeForUnion(TypeResolutionParameters params)
return result;
}

protected ExecutionResult completeValueForEnum(GraphQLEnumType enumType, ExecutionStrategyParameters parameters, Object result) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we make the signature
protected ExecutionResult completeValueForEnum(GraphQLEnumType enumType, ExecutionContext
context, ExecutionStrategyParameters parameters, Object result) {

That way its sympathetic to the other methods that have ExecutionContext
context, ExecutionStrategyParameters parameters as parameters

In fact you could argue for consistency they should be the 1st two parameters (since this is a breaking change)

Object serialized = enumType.getCoercing().serialize(result);
protected ExecutionResult completeValueForEnum(GraphQLEnumType enumType, ExecutionStrategyParameters parameters, Object result, ExecutionContext context) {
Object serialized;
try {
serialized = enumType.getCoercing().serialize(result);
} catch (CoercingSerializeException e) {
context.addError(new SerializationError(e));
serialized = null;
}
serialized = parameters.nonNullFieldValidator().validate(serialized);
return new ExecutionResultImpl(serialized, null);
}

protected ExecutionResult completeValueForScalar(GraphQLScalarType scalarType, ExecutionStrategyParameters parameters, Object result) {
Object serialized = scalarType.getCoercing().serialize(result);
protected ExecutionResult completeValueForScalar(GraphQLScalarType scalarType, ExecutionStrategyParameters parameters, Object result, ExecutionContext context) {
Object serialized;
try {
serialized = scalarType.getCoercing().serialize(result);
} catch (CoercingSerializeException e) {
serialized = null;
context.addError(new SerializationError(e));
}

// TODO: fix that: this should not be handled here
//6.6.1 http://facebook.github.io/graphql/#sec-Field-entries
if (serialized instanceof Double && ((Double) serialized).isNaN()) {
serialized = null;
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/graphql/schema/Coercing.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,23 @@ public interface Coercing<I, O> {
/**
* Called to convert a result of a DataFetcher to a valid runtime value.
*
* @param input is never null
* @param dataFetcherResult is never null
* @return never null
* @throws graphql.GraphQLException if value input can't be serialized
* @throws graphql.schema.CoercingSerializeException if value input can't be serialized
*/
O serialize(Object input);
O serialize(Object dataFetcherResult);

/**
* Called to resolve a input from a variable.
* Null if not possible.
*
* @param input is never null
* @return never null
* @throws graphql.GraphQLException if value input can't be serialized
* @throws graphql.schema.CoercingParseValueException if value input can't be parsed
*/
I parseValue(Object input);

/**
* Called to convert a AST node
* Called to convert an input AST node
*
* @param input is never null
* @return A null value indicates that the literal is not valid. See {@link graphql.validation.ValidationUtil#isValidLiteralValue}
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/graphql/schema/CoercingParseValueException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package graphql.schema;

import graphql.GraphQLException;

public class CoercingParseValueException extends GraphQLException {

public CoercingParseValueException() {
}

public CoercingParseValueException(String message) {
super(message);
}

public CoercingParseValueException(String message, Throwable cause) {
super(message, cause);
}

public CoercingParseValueException(Throwable cause) {
super(cause);
}
}
21 changes: 21 additions & 0 deletions src/main/java/graphql/schema/CoercingSerializeException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package graphql.schema;

import graphql.GraphQLException;

public class CoercingSerializeException extends GraphQLException {

public CoercingSerializeException() {
}

public CoercingSerializeException(String message) {
super(message);
}

public CoercingSerializeException(String message, Throwable cause) {
super(message, cause);
}

public CoercingSerializeException(Throwable cause) {
super(cause);
}
}
5 changes: 2 additions & 3 deletions src/main/java/graphql/schema/GraphQLEnumType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


import graphql.AssertException;
import graphql.GraphQLException;
import graphql.Internal;
import graphql.PublicApi;
import graphql.language.EnumTypeDefinition;
Expand Down Expand Up @@ -80,14 +79,14 @@ private void buildMap(List<GraphQLEnumValueDefinition> values) {
private Object getValueByName(Object value) {
GraphQLEnumValueDefinition enumValueDefinition = valueDefinitionMap.get(value.toString());
if (enumValueDefinition != null) return enumValueDefinition.getValue();
throw new GraphQLException("Invalid input for Enum '" + name + "'. No value found for name " + value.toString());
throw new CoercingParseValueException("Invalid input for Enum '" + name + "'. No value found for name '" + value.toString() + "'");
}

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


Expand Down
34 changes: 20 additions & 14 deletions src/test/groovy/graphql/ScalarsBigDecimalTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import graphql.language.BooleanValue
import graphql.language.FloatValue
import graphql.language.IntValue
import graphql.language.StringValue
import graphql.schema.CoercingParseValueException
import graphql.schema.CoercingSerializeException
import spock.lang.Specification
import spock.lang.Unroll

Expand Down Expand Up @@ -59,20 +61,11 @@ class ScalarsBigDecimalTest extends Specification {
}

@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
}
def "serialize throws exception for invalid input #value"() {
when:
Scalars.GraphQLBigDecimal.getCoercing().serialize(value)
then:
thrown(CoercingSerializeException)

where:
value | _
Expand All @@ -81,5 +74,18 @@ class ScalarsBigDecimalTest extends Specification {
new Object() | _
}

@Unroll
def "parseValue throws exception for invalid input #value"() {
when:
Scalars.GraphQLBigDecimal.getCoercing().parseValue(value)
then:
thrown(CoercingParseValueException)

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

}
36 changes: 22 additions & 14 deletions src/test/groovy/graphql/ScalarsBigIntegerTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import graphql.language.BooleanValue
import graphql.language.FloatValue
import graphql.language.IntValue
import graphql.language.StringValue
import graphql.schema.CoercingParseValueException
import graphql.schema.CoercingSerializeException
import spock.lang.Specification
import spock.lang.Unroll

Expand Down Expand Up @@ -56,20 +58,11 @@ class ScalarsBigIntegerTest extends Specification {
}

@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
}
def "serialize throws exception for invalid input #value"() {
when:
Scalars.GraphQLBigInteger.getCoercing().serialize(value)
then:
thrown(CoercingSerializeException)

where:
value | _
Expand All @@ -80,5 +73,20 @@ class ScalarsBigIntegerTest extends Specification {
new Object() | _
}

@Unroll
def "parseValue throws exception for invalid input #value"() {
when:
Scalars.GraphQLBigInteger.getCoercing().parseValue(value)
then:
thrown(CoercingParseValueException)

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

}
32 changes: 18 additions & 14 deletions src/test/groovy/graphql/ScalarsBooleanTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import graphql.language.BooleanValue
import graphql.language.FloatValue
import graphql.language.IntValue
import graphql.language.StringValue
import graphql.schema.CoercingParseValueException
import graphql.schema.CoercingSerializeException
import spock.lang.Specification
import spock.lang.Unroll

Expand Down Expand Up @@ -58,25 +60,27 @@ class ScalarsBooleanTest extends Specification {
}

@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
}
def "serialize throws exception for invalid input #value"() {
when:
Scalars.GraphQLBoolean.getCoercing().serialize(value)
then:
thrown(CoercingSerializeException)

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

@Unroll
def "parseValue throws exception for invalid input #value"() {
when:
Scalars.GraphQLBoolean.getCoercing().parseValue(value)
then:
thrown(CoercingParseValueException)

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

}
Loading