Skip to content

Commit ae664c0

Browse files
committed
clarify validation
1 parent 80ab31b commit ae664c0

File tree

11 files changed

+31
-170
lines changed

11 files changed

+31
-170
lines changed

src/main/java/graphql/execution/ValuesResolver.java

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import graphql.normalized.NormalizedInputValue;
2323
import graphql.schema.Coercing;
2424
import graphql.schema.CoercingParseValueException;
25-
import graphql.schema.CoercingValueToLiteralException;
2625
import graphql.schema.GraphQLArgument;
2726
import graphql.schema.GraphQLCodeRegistry;
2827
import graphql.schema.GraphQLEnumType;
@@ -131,6 +130,8 @@ public static Value<?> valueToLiteral(Object value, ValueState valueState, Graph
131130
/**
132131
* Takes a value which can be in different states (internal, literal, external value) and converts into Literal
133132
*
133+
* This assumes the value is valid!
134+
*
134135
* @return
135136
*/
136137
public static Value<?> valueToLiteral(GraphqlFieldVisibility fieldVisibility, Object value, ValueState valueState, GraphQLType type) {
@@ -160,16 +161,12 @@ public static Object valueToInternalValue(Object value, ValueState valueState, G
160161
return Assert.assertShouldNeverHappen("unexpected value state " + valueState);
161162
}
162163

163-
private Value externalValueToLiteral(GraphqlFieldVisibility fieldVisibility, Object value, GraphQLInputType type) {
164-
// the value is not validated
164+
private Value externalValueToLiteral(GraphqlFieldVisibility fieldVisibility, @Nullable Object value, GraphQLInputType type) {
165+
if (value == null) {
166+
return newNullValue().build();
167+
}
165168
if (GraphQLTypeUtil.isNonNull(type)) {
166-
if (value == null) {
167-
throw new CoercingValueToLiteralException(String.format("expected non null value for type %s", GraphQLTypeUtil.simplePrint(type)));
168-
}
169-
Value literal = externalValueToLiteral(fieldVisibility, value, (GraphQLInputType) unwrapNonNull(type));
170-
if (literal instanceof NullValue) {
171-
throw new CoercingValueToLiteralException(String.format("expected non null value for type %s", GraphQLTypeUtil.simplePrint(type)));
172-
}
169+
return externalValueToLiteral(fieldVisibility, value, (GraphQLInputType) unwrapNonNull(type));
173170
}
174171
if (type instanceof GraphQLScalarType) {
175172
return externalValueToLiteralForScalar((GraphQLScalarType) type, value);
@@ -209,22 +206,10 @@ private ArrayValue externalValueToLiteralForList(GraphqlFieldVisibility fieldVis
209206
private ObjectValue externalValueToLiteralForObject(GraphqlFieldVisibility fieldVisibility,
210207
GraphQLInputObjectType inputObjectType,
211208
Object inputValue) {
212-
if (!(inputValue instanceof Map)) {
213-
throw CoercingValueToLiteralException.newCoercingValueToLiteralException()
214-
.message("Expected type 'Map' but was '" + inputValue.getClass().getSimpleName() +
215-
"'. Values for input objects must be an instance of type 'Map'.")
216-
.build();
217-
}
209+
assertTrue(inputValue instanceof Map, () -> "Expect Map as input");
218210
Map<String, Object> inputMap = (Map<String, Object>) inputValue;
219211
List<GraphQLInputObjectField> fieldDefinitions = fieldVisibility.getFieldDefinitions(inputObjectType);
220-
List<String> fieldNames = map(fieldDefinitions, GraphQLInputObjectField::getName);
221-
for (String providedFieldName : inputMap.keySet()) {
222-
if (!fieldNames.contains(providedFieldName)) {
223-
throw new InputMapDefinesTooManyFieldsException(inputObjectType, providedFieldName);
224-
}
225-
}
226212

227-
//TODO: what about normalized literals??
228213

229214
List<ObjectField> objectFields = new ArrayList<>();
230215
for (GraphQLInputObjectField inputFieldDefinition : fieldDefinitions) {
@@ -235,8 +220,6 @@ private ObjectValue externalValueToLiteralForObject(GraphqlFieldVisibility field
235220
if (!hasValue && inputFieldDefinition.hasSetDefaultValue()) {
236221
Value defaultValueLiteral = valueToLiteral(fieldVisibility, inputFieldDefinition.getInputFieldDefaultValue(), inputFieldDefinition.getDefaultValueState(), fieldType);
237222
objectFields.add(newObjectField().value(defaultValueLiteral).build());
238-
} else if (isNonNull(fieldType) && (!hasValue || fieldValue == null)) {
239-
throw new RuntimeException("non nullable field");
240223
} else if (hasValue) {
241224
if (fieldValue == null) {
242225
objectFields.add(newObjectField().value(newNullValue().build()).build());

src/main/java/graphql/scalar/GraphqlBooleanCoercing.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
import graphql.schema.CoercingParseLiteralException;
88
import graphql.schema.CoercingParseValueException;
99
import graphql.schema.CoercingSerializeException;
10-
import graphql.schema.CoercingValueToLiteralException;
1110

1211
import java.math.BigDecimal;
1312

13+
import static graphql.Assert.assertNotNull;
1414
import static graphql.Assert.assertShouldNeverHappen;
1515
import static graphql.scalar.CoercingUtil.isNumberIsh;
1616
import static graphql.scalar.CoercingUtil.typeName;
@@ -71,13 +71,8 @@ public Boolean parseLiteral(Object input) {
7171
}
7272

7373
@Override
74-
public Value valueToLiteral(Object input) throws CoercingValueToLiteralException {
75-
Boolean result = convertImpl(input);
76-
if (result == null) {
77-
throw new CoercingValueToLiteralException(
78-
"Expected 'Boolean' but was '" + typeName(input) + "'."
79-
);
80-
}
74+
public Value valueToLiteral(Object input) {
75+
Boolean result = assertNotNull(convertImpl(input));
8176
return BooleanValue.newBooleanValue(result).build();
8277
}
8378
}

src/main/java/graphql/scalar/GraphqlFloatCoercing.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
import graphql.schema.CoercingParseLiteralException;
99
import graphql.schema.CoercingParseValueException;
1010
import graphql.schema.CoercingSerializeException;
11-
import graphql.schema.CoercingValueToLiteralException;
1211

1312
import java.math.BigDecimal;
1413

14+
import static graphql.Assert.assertNotNull;
1515
import static graphql.scalar.CoercingUtil.isNumberIsh;
1616
import static graphql.scalar.CoercingUtil.typeName;
1717

@@ -70,13 +70,8 @@ public Double parseLiteral(Object input) {
7070
}
7171

7272
@Override
73-
public Value valueToLiteral(Object input) throws CoercingValueToLiteralException {
74-
Double result = convertImpl(input);
75-
if (result == null) {
76-
throw new CoercingValueToLiteralException(
77-
"Expected 'Float' but was '" + typeName(input) + "'."
78-
);
79-
}
73+
public Value valueToLiteral(Object input) {
74+
Double result = assertNotNull(convertImpl(input));
8075
return FloatValue.newFloatValue(BigDecimal.valueOf(result)).build();
8176
}
8277
}

src/main/java/graphql/scalar/GraphqlIDCoercing.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
import graphql.schema.CoercingParseLiteralException;
99
import graphql.schema.CoercingParseValueException;
1010
import graphql.schema.CoercingSerializeException;
11-
import graphql.schema.CoercingValueToLiteralException;
1211

1312
import java.math.BigInteger;
1413
import java.util.UUID;
1514

15+
import static graphql.Assert.assertNotNull;
1616
import static graphql.scalar.CoercingUtil.typeName;
1717

1818
@Internal
@@ -74,13 +74,8 @@ public String parseLiteral(Object input) {
7474
}
7575

7676
@Override
77-
public Value valueToLiteral(Object input) throws CoercingValueToLiteralException {
78-
String result = convertImpl(input);
79-
if (result == null) {
80-
throw new CoercingValueToLiteralException(
81-
"Expected 'ID' but was '" + typeName(input) + "'."
82-
);
83-
}
77+
public Value valueToLiteral(Object input) {
78+
String result = assertNotNull(convertImpl(input));
8479
return StringValue.newStringValue(result).build();
8580
}
8681
}

src/main/java/graphql/scalar/GraphqlIntCoercing.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
import graphql.schema.CoercingParseLiteralException;
88
import graphql.schema.CoercingParseValueException;
99
import graphql.schema.CoercingSerializeException;
10-
import graphql.schema.CoercingValueToLiteralException;
1110

1211
import java.math.BigDecimal;
1312
import java.math.BigInteger;
1413

14+
import static graphql.Assert.assertNotNull;
1515
import static graphql.scalar.CoercingUtil.isNumberIsh;
1616
import static graphql.scalar.CoercingUtil.typeName;
1717

@@ -80,13 +80,8 @@ public Integer parseLiteral(Object input) {
8080
}
8181

8282
@Override
83-
public Value valueToLiteral(Object input) throws CoercingValueToLiteralException {
84-
Integer result = convertImpl(input);
85-
if (result == null) {
86-
throw new CoercingValueToLiteralException(
87-
"Expected 'Integer' but was '" + typeName(input) + "'."
88-
);
89-
}
83+
public Value valueToLiteral(Object input) {
84+
Integer result = assertNotNull(convertImpl(input));
9085
return IntValue.newIntValue(BigInteger.valueOf(result)).build();
9186
}
9287
}

src/main/java/graphql/scalar/GraphqlStringCoercing.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import graphql.language.Value;
66
import graphql.schema.Coercing;
77
import graphql.schema.CoercingParseLiteralException;
8-
import graphql.schema.CoercingValueToLiteralException;
98

109
import static graphql.scalar.CoercingUtil.typeName;
1110

@@ -32,7 +31,7 @@ public String parseLiteral(Object input) {
3231
}
3332

3433
@Override
35-
public Value valueToLiteral(Object input) throws CoercingValueToLiteralException {
34+
public Value valueToLiteral(Object input) {
3635
return StringValue.newStringValue(input.toString()).build();
3736
}
3837
}

src/main/java/graphql/schema/Coercing.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,15 @@ public interface Coercing<I, O> {
9696

9797

9898
/**
99-
* Converts an internal input value to a literal (Ast Value)
99+
* Converts an external input value to a literal (Ast Value).
100+
*
101+
* IMPORTANT: the argument is validated before by calling {@link #parseValue(Object)}.
100102
*
101103
* @param input an external input value
102104
*
103105
* @return The literal matching the external input value.
104-
*
105-
* @throws CoercingValueToLiteralException
106106
*/
107-
default @NotNull Value valueToLiteral(@NotNull Object input) throws CoercingValueToLiteralException {
108-
throw new CoercingValueToLiteralException("This is not implemented by this Scalar " + this.getClass());
107+
default @NotNull Value valueToLiteral(@NotNull Object input) {
108+
throw new UnsupportedOperationException("This is not implemented by this Scalar " + this.getClass());
109109
}
110110
}

src/main/java/graphql/schema/CoercingLiteralToValueException.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/main/java/graphql/schema/CoercingValueToLiteralException.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/main/java/graphql/schema/GraphQLEnumType.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,8 @@ public Object parseLiteral(Object input) {
126126
@Internal
127127
public Value valueToLiteral(Object input) {
128128
GraphQLEnumValueDefinition enumValueDefinition = valueDefinitionMap.get(input.toString());
129-
if (enumValueDefinition != null) {
130-
return EnumValue.newEnumValue(enumValueDefinition.getName()).build();
131-
}
132-
throw new CoercingValueToLiteralException("Invalid input for Enum '" + name + "'. No value found for name '" + input.toString() + "'");
129+
assertNotNull(enumValueDefinition, () -> "Invalid input for Enum '" + name + "'. No value found for name '" + input.toString() + "'");
130+
return EnumValue.newEnumValue(enumValueDefinition.getName()).build();
133131

134132
}
135133

@@ -318,7 +316,7 @@ public Builder(GraphQLEnumType existing) {
318316
this.definition = existing.getDefinition();
319317
this.extensionDefinitions = existing.getExtensionDefinitions();
320318
this.values.putAll(getByName(existing.getValues(), GraphQLEnumValueDefinition::getName));
321-
DirectivesUtil.enforceAddAll(this.directives,existing.getDirectives());
319+
DirectivesUtil.enforceAddAll(this.directives, existing.getDirectives());
322320
}
323321

324322
@Override

0 commit comments

Comments
 (0)