Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows the messages from a scalar validation to be sent back to the user #1725

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/main/java/graphql/SerializationError.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import graphql.schema.CoercingSerializeException;

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

import static graphql.Assert.assertNotNull;
import static java.lang.String.format;
Expand All @@ -31,15 +32,14 @@ public CoercingSerializeException getException() {
return exception;
}


@Override
public String getMessage() {
return message;
}

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

@Override
Expand All @@ -52,6 +52,11 @@ public List<Object> getPath() {
return path;
}

@Override
public Map<String, Object> getExtensions() {
return exception.getExtensions();
}

@Override
public String toString() {
return "SerializationError{" +
Expand Down
25 changes: 13 additions & 12 deletions src/main/java/graphql/execution/ValuesResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import static graphql.schema.GraphQLTypeUtil.unwrapOne;
import static graphql.schema.visibility.DefaultGraphqlFieldVisibility.DEFAULT_FIELD_VISIBILITY;

@SuppressWarnings("rawtypes")
@Internal
public class ValuesResolver {

Expand All @@ -47,11 +48,10 @@ public class ValuesResolver {
* @param schema the schema
* @param variableDefinitions the variable definitions
* @param variableValues the supplied variables
*
* @return coerced variable values as a map
*/
public Map<String, Object> coerceVariableValues(GraphQLSchema schema, List<VariableDefinition> variableDefinitions, Map<String, Object> variableValues) {
GraphqlFieldVisibility fieldVisibility = schema.getFieldVisibility();
GraphqlFieldVisibility fieldVisibility = schema.getCodeRegistry().getFieldVisibility();
Map<String, Object> coercedValues = new LinkedHashMap<>();
for (VariableDefinition variableDefinition : variableDefinitions) {
String variableName = variableDefinition.getName();
Expand Down Expand Up @@ -154,10 +154,10 @@ private Object coerceValue(GraphqlFieldVisibility fieldVisibility, VariableDefin
if (value instanceof Map) {
return coerceValueForInputObjectType(fieldVisibility, variableDefinition, (GraphQLInputObjectType) graphQLType, (Map<String, Object>) value);
} else {
throw new CoercingParseValueException(
"Expected type 'Map' but was '" + value.getClass().getSimpleName() +
"'. Variables for input objects must be an instance of type 'Map'."
);
throw CoercingParseValueException.newCoercingParseValueException()
.message("Expected type 'Map' but was '" + value.getClass().getSimpleName() +
"'. Variables for input objects must be an instance of type 'Map'.")
.build();
}
} else {
return assertShouldNeverHappen("unhandled type %s", graphQLType);
Expand All @@ -166,13 +166,14 @@ private Object coerceValue(GraphqlFieldVisibility fieldVisibility, VariableDefin
if (e.getLocations() != null) {
throw e;
}

throw new CoercingParseValueException(
"Variable '" + inputName + "' has an invalid value. " + e.getMessage(),
e.getCause(),
variableDefinition.getSourceLocation()
);
throw CoercingParseValueException.newCoercingParseValueException()
.message("Variable '" + inputName + "' has an invalid value : " + e.getMessage())
.extensions(e.getExtensions())
.cause(e.getCause())
.sourceLocation(variableDefinition.getSourceLocation())
.build();
}

}

private Object coerceValueForInputObjectType(GraphqlFieldVisibility fieldVisibility, VariableDefinition variableDefinition, GraphQLInputObjectType inputObjectType, Map<String, Object> input) {
Expand Down
68 changes: 58 additions & 10 deletions src/main/java/graphql/schema/CoercingParseLiteralException.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,44 @@
package graphql.schema;

import java.util.Collections;
import java.util.List;

import graphql.ErrorType;
import graphql.GraphQLError;
import graphql.GraphQLException;
import graphql.PublicApi;
import graphql.language.SourceLocation;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@PublicApi
public class CoercingParseLiteralException extends GraphQLException implements GraphQLError {
private List<SourceLocation> sourceLocations;
public class CoercingParseLiteralException extends GraphQLException implements GraphQLError {
private final List<SourceLocation> sourceLocations;
private final Map<String, Object> extensions;

public CoercingParseLiteralException() {
this(newCoercingParseLiteralException());
}

public CoercingParseLiteralException(String message) {
super(message);
this(newCoercingParseLiteralException().message(message));
}

public CoercingParseLiteralException(String message, Throwable cause) {
super(message, cause);
this(newCoercingParseLiteralException().message(message).cause(cause));
}

public CoercingParseLiteralException(String message, Throwable cause, SourceLocation sourceLocation) {
super(message, cause);
this.sourceLocations = Collections.singletonList(sourceLocation);
this(newCoercingParseLiteralException().message(message).cause(cause).sourceLocation(sourceLocation));
}

public CoercingParseLiteralException(Throwable cause) {
super(cause);
this(newCoercingParseLiteralException().cause(cause));
}

private CoercingParseLiteralException(Builder builder) {
super(builder.message, builder.cause);
this.sourceLocations = builder.sourceLocations;
this.extensions = builder.extensions;
}

@Override
Expand All @@ -42,4 +50,44 @@ public List<SourceLocation> getLocations() {
public ErrorType getErrorType() {
return ErrorType.ValidationError;
}

@Override
public Map<String, Object> getExtensions() {
return extensions;
}

public static Builder newCoercingParseLiteralException() {
return new Builder();
}

public static class Builder {
private String message;
private Throwable cause;
private List<SourceLocation> sourceLocations;
private Map<String, Object> extensions;

public Builder message(String message) {
this.message = message;
return this;
}

public Builder cause(Throwable cause) {
this.cause = cause;
return this;
}

public Builder sourceLocation(SourceLocation sourceLocation) {
this.sourceLocations = sourceLocation == null ? null : Collections.singletonList(sourceLocation);
return this;
}

public Builder extensions(Map<String, Object> extensions) {
this.extensions = extensions;
return this;
}

public CoercingParseLiteralException build() {
return new CoercingParseLiteralException(this);
}
}
}
73 changes: 63 additions & 10 deletions src/main/java/graphql/schema/CoercingParseValueException.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,44 @@
package graphql.schema;

import java.util.Collections;
import java.util.List;

import graphql.ErrorType;
import graphql.GraphQLError;
import graphql.GraphQLException;
import graphql.PublicApi;
import graphql.language.SourceLocation;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@PublicApi
public class CoercingParseValueException extends GraphQLException implements GraphQLError {
private List<SourceLocation> sourceLocations;
private final List<SourceLocation> sourceLocations;
private final Map<String, Object> extensions;

public CoercingParseValueException() {
this(newCoercingParseValueException());
}

public CoercingParseValueException(String message) {
super(message);
this(newCoercingParseValueException().message(message));
}

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

public CoercingParseValueException(Throwable cause) {
this(newCoercingParseValueException().cause(cause));
}

public CoercingParseValueException(String message, Throwable cause, SourceLocation sourceLocation) {
super(message, cause);
this.sourceLocations = Collections.singletonList(sourceLocation);
this(newCoercingParseValueException().message(message).cause(cause).sourceLocation(sourceLocation));
}

public CoercingParseValueException(Throwable cause) {
super(cause);
private CoercingParseValueException(Builder builder) {
super(builder.message, builder.cause);
this.sourceLocations = builder.sourceLocations;
this.extensions = builder.extensions;
}

@Override
Expand All @@ -42,4 +50,49 @@ public List<SourceLocation> getLocations() {
public ErrorType getErrorType() {
return ErrorType.ValidationError;
}

@Override
public Map<String, Object> getExtensions() {
return extensions;
}

public static Builder newCoercingParseValueException() {
return new Builder();
}

public static class Builder {
private String message;
private Throwable cause;
private List<SourceLocation> sourceLocations;
private Map<String, Object> extensions;

public Builder message(String message) {
this.message = message;
return this;
}

public Builder cause(Throwable cause) {
this.cause = cause;
return this;
}

public Builder sourceLocation(SourceLocation sourceLocation) {
this.sourceLocations = sourceLocation == null ? null : Collections.singletonList(sourceLocation);
return this;
}

public Builder sourceLocations(List<SourceLocation> sourceLocations) {
this.sourceLocations = sourceLocations;
return this;
}

public Builder extensions(Map<String, Object> extensions) {
this.extensions = extensions;
return this;
}

public CoercingParseValueException build() {
return new CoercingParseValueException(this);
}
}
}
75 changes: 71 additions & 4 deletions src/main/java/graphql/schema/CoercingSerializeException.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,90 @@
package graphql.schema;

import graphql.ErrorClassification;
import graphql.ErrorType;
import graphql.GraphQLError;
import graphql.GraphQLException;
import graphql.PublicApi;
import graphql.language.SourceLocation;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@PublicApi
public class CoercingSerializeException extends GraphQLException {
public class CoercingSerializeException extends GraphQLException implements GraphQLError {
private final List<SourceLocation> sourceLocations;
private final Map<String, Object> extensions;

public CoercingSerializeException() {
this(newCoercingSerializeException());
}

public CoercingSerializeException(String message) {
super(message);
this(newCoercingSerializeException().message(message));
}

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

public CoercingSerializeException(Throwable cause) {
super(cause);
this(newCoercingSerializeException().cause(cause));
}

private CoercingSerializeException(Builder builder) {
super(builder.message, builder.cause);
this.sourceLocations = builder.sourceLocations;
this.extensions = builder.extensions;
}

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

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

@Override
public Map<String, Object> getExtensions() {
return extensions;
}

public static Builder newCoercingSerializeException() {
return new Builder();
}

public static class Builder {
private String message;
private Throwable cause;
private Map<String, Object> extensions;
private List<SourceLocation> sourceLocations;

public Builder message(String message) {
this.message = message;
return this;
}

public Builder cause(Throwable cause) {
this.cause = cause;
return this;
}

public Builder sourceLocation(SourceLocation sourceLocation) {
this.sourceLocations = sourceLocation == null ? null : Collections.singletonList(sourceLocation);
return this;
}

public Builder extensions(Map<String, Object> extensions) {
this.extensions = extensions;
return this;
}

public CoercingSerializeException build() {
return new CoercingSerializeException(this);
}
}
}