Skip to content

Commit f747000

Browse files
committed
handle null values in valueFromAST
1 parent c765595 commit f747000

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

src/execution/__tests__/variables-test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,36 @@ describe('Execute: Handles inputs', () => {
159159
});
160160
});
161161

162+
it('properly parses null value to null', async () => {
163+
const doc = `
164+
{
165+
fieldWithObjectInput(input: {a: null, b: null, c: "C", d: null})
166+
}
167+
`;
168+
const ast = parse(doc);
169+
170+
return expect(await execute(schema, ast)).to.deep.equal({
171+
data: {
172+
fieldWithObjectInput: '{"a":null,"b":null,"c":"C","d":null}'
173+
}
174+
});
175+
});
176+
177+
it('properly parses null value in list', async () => {
178+
const doc = `
179+
{
180+
fieldWithObjectInput(input: {b: ["A",null,"C"], c: "C"})
181+
}
182+
`;
183+
const ast = parse(doc);
184+
185+
return expect(await execute(schema, ast)).to.deep.equal({
186+
data: {
187+
fieldWithObjectInput: '{"b":["A",null,"C"],"c":"C"}'
188+
}
189+
});
190+
});
191+
162192
it('does not use incorrect value', async () => {
163193
const doc = `
164194
{

src/utilities/valueFromAST.js

Lines changed: 13 additions & 9 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,14 +98,13 @@ 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-
}
101-
if (!isNullish(fieldValue)) {
102-
obj[fieldName] = fieldValue;
103-
}
103+
104+
// If no valid field value was provided, use the default value
105+
obj[fieldName] = fieldValue === undefined || fieldValue !== fieldValue ?
106+
field.defaultValue :
107+
fieldValue;
104108
return obj;
105109
}, {});
106110
}

0 commit comments

Comments
 (0)