Skip to content
This repository was archived by the owner on Jul 6, 2020. It is now read-only.

Normalise null args on fields by removing them #85

Merged
merged 1 commit into from
Sep 20, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/ast/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@ export const getFieldArguments = (
}

const args = Object.create(null);
let argsSize = 0;

for (let i = 0, l = node.arguments.length; i < l; i++) {
const arg = node.arguments[i];
args[getName(arg)] = valueFromASTUntyped(arg.value, vars);
const value = valueFromASTUntyped(arg.value, vars);
if (value !== undefined && value !== null) {
args[getName(arg)] = value;
argsSize++;
}
}

return args;
return argsSize > 0 ? args : null;
};

/** Returns a normalized form of variables with defaulted values */
Expand Down
34 changes: 34 additions & 0 deletions src/test-utils/examples-1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,40 @@ it('passes the "getting-started" example', () => {
});
});

it('resolves missing, nullable arguments on fields', () => {
const store = new Store();

const GetWithVariables = gql`
query {
todo(first: null) {
__typename
id
}
}
`;

const GetWithoutVariables = gql`
query {
todo {
__typename
id
}
}
`;

const writeData = {
__typename: 'Query',
todo: {
__typename: 'Todo',
id: '123',
},
};

write(store, { query: GetWithVariables }, writeData);
const { data } = query(store, { query: GetWithoutVariables });
expect(data).toEqual(writeData);
});

it('respects property-level resolvers when given', () => {
const store = new Store(undefined, {
Todo: { text: () => 'hi' },
Expand Down