Skip to content

Commit b2d6ce2

Browse files
committed
handle null values in valueFromAST
1 parent a5819c3 commit b2d6ce2

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/utilities/valueFromAST.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import type {
4242
* | String | String |
4343
* | Int / Float | Number |
4444
* | Enum Value | Mixed |
45+
* | NullValue | null |
4546
*
4647
*/
4748
export function valueFromAST(
@@ -57,13 +58,17 @@ export function valueFromAST(
5758
}
5859

5960
if (!valueAST) {
61+
return;
62+
}
63+
64+
if (valueAST.kind === Kind.NULL) {
6065
return null;
6166
}
6267

6368
if (valueAST.kind === Kind.VARIABLE) {
6469
const variableName = (valueAST: Variable).name.value;
6570
if (!variables || !variables.hasOwnProperty(variableName)) {
66-
return null;
71+
return;
6772
}
6873
// Note: we're not doing any checking that this variable is correct. We're
6974
// assuming that this query has been validated and the variable usage here
@@ -83,7 +88,7 @@ export function valueFromAST(
8388

8489
if (type instanceof GraphQLInputObjectType) {
8590
if (valueAST.kind !== Kind.OBJECT) {
86-
return null;
91+
return;
8792
}
8893
const fields = type.getFields();
8994
const fieldASTs = keyMap(
@@ -93,13 +98,12 @@ export function valueFromAST(
9398
return Object.keys(fields).reduce((obj, fieldName) => {
9499
const field = fields[fieldName];
95100
const fieldAST = fieldASTs[fieldName];
96-
let fieldValue =
101+
const fieldValue =
97102
valueFromAST(fieldAST && fieldAST.value, field.type, variables);
98-
if (isNullish(fieldValue)) {
99-
fieldValue = field.defaultValue;
100-
}
101103
if (!isNullish(fieldValue)) {
102104
obj[fieldName] = fieldValue;
105+
} else if (fieldValue !== null) {
106+
obj[fieldName] = field.defaultValue;
103107
}
104108
return obj;
105109
}, {});
@@ -111,6 +115,7 @@ export function valueFromAST(
111115
);
112116

113117
const parsed = type.parseLiteral(valueAST);
118+
// TODO: Should be this condition ommited?
114119
if (!isNullish(parsed)) {
115120
return parsed;
116121
}

0 commit comments

Comments
 (0)