Skip to content

Commit 0767861

Browse files
authored
Fix/optimize value coercion check for OneOf type (#4181)
Where a JavaScript value is coerced to a GraphQL OneOf Input Type, a null value should only be reported when the "one of" condition is satisfied. The code block starting at line 158 [here](https://github.com/graphql/graphql-js/blob/1dbdadc6f46d2c97c71c7a4f5c61a2c75d1536ae/src/utilities/coerceInputValue.ts#L158) that accesses `keys[0]` should not be executed if `keys` is empty or contains more than one item. The PR fixes this by adding an else statement. Alternatively, the "if/else" branches could be reversed, and the `keys.length !== 1` check should become a `keys.length === 1` check.
1 parent bb8e574 commit 0767861

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

Diff for: src/utilities/coerceInputValue.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,16 @@ function coerceInputValueImpl(
199199
`Exactly one key must be specified for OneOf type "${type}".`,
200200
),
201201
);
202-
}
203-
204-
const key = keys[0];
205-
const value = coercedValue[key];
206-
if (value === null) {
207-
onError(
208-
pathToArray(path).concat(key),
209-
value,
210-
new GraphQLError(`Field "${key}" must be non-null.`),
211-
);
202+
} else {
203+
const key = keys[0];
204+
const value = coercedValue[key];
205+
if (value === null) {
206+
onError(
207+
pathToArray(path).concat(key),
208+
value,
209+
new GraphQLError(`Field "${key}" must be non-null.`),
210+
);
211+
}
212212
}
213213
}
214214

0 commit comments

Comments
 (0)