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

Commit

Permalink
Normalise null args on fields by removing them (#85)
Browse files Browse the repository at this point in the history
This means that `todo(x: null) {}` is assumed to be
the same as `todo {}`
  • Loading branch information
kitten authored Sep 20, 2019
1 parent 8acdbac commit cbe5bde
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
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

0 comments on commit cbe5bde

Please sign in to comment.