Skip to content
This repository was archived by the owner on Apr 13, 2023. It is now read-only.

Commit 6a597f4

Browse files
author
James Baxley
authored
allow optional variables (#200)
1 parent 0fa1b90 commit 6a597f4

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

Changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ Expect active development and potentially significant breaking changes in the `0
44

55
### vNext
66

7+
### v0.5.2
8+
9+
- Feature: Allow optional variables by passing null value on behalf of the variable
10+
711
### v0.5.1
812

913
- Feature: Added link to [recompose](https://github.com/acdlite/recompose) to use the `compose` function. This makes it easy to combine multiple queries on a single component. [#194](https://github.com/apollostack/react-apollo/pull/194)

src/graphql.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,20 @@ export default function graphql(
177177
if (opts.variables || !operation.variables.length) return opts;
178178

179179
let variables = {};
180-
for (let { variable } of operation.variables) {
180+
for (let { variable, type } of operation.variables) {
181181
if (!variable.name || !variable.name.value) continue;
182182

183183
if (typeof props[variable.name.value] !== 'undefined') {
184184
variables[variable.name.value] = props[variable.name.value];
185185
continue;
186186
}
187187

188+
// allow optional props
189+
if (type.kind !== 'NonNullType') {
190+
variables[variable.name.value] = null;
191+
continue;
192+
}
193+
188194
invariant(typeof props[variable.name.value] !== 'undefined',
189195
`The operation '${operation.name}' wrapping '${getDisplayName(WrappedComponent)}' ` +
190196
`is expecting a variable: '${variable.name.value}' but it was not found in the props ` +

test/react-web/client/graphql/queries.tsx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ describe('queries', () => {
474474
mount(<ProviderMock client={client}><Container first={null} /></ProviderMock>);
475475
});
476476

477-
it('errors if the passed props don\'t contain the needed variables', () => {
477+
it('don\'t error on optional required props', () => {
478478
const query = gql`
479479
query people($first: Int) {
480480
allPeople(first: $first) { people { name } }
@@ -489,6 +489,30 @@ describe('queries', () => {
489489
const client = new ApolloClient({ networkInterface });
490490
const Container = graphql(query)(() => null);
491491

492+
let error = null;
493+
try {
494+
mount(<ProviderMock client={client}><Container frst={1} /></ProviderMock>);
495+
} catch (e) { error = e; }
496+
497+
expect(error).to.be.null;
498+
499+
});
500+
501+
it('errors if the passed props don\'t contain the needed variables', () => {
502+
const query = gql`
503+
query people($first: Int!) {
504+
allPeople(first: $first) { people { name } }
505+
}
506+
`;
507+
const data = { allPeople: { people: [ { name: 'Luke Skywalker' } ] } };
508+
const variables = { first: 1 };
509+
const networkInterface = mockNetworkInterface({
510+
request: { query, variables },
511+
result: { data },
512+
});
513+
const client = new ApolloClient({ networkInterface });
514+
const Container = graphql(query)(() => null);
515+
492516
try {
493517
mount(<ProviderMock client={client}><Container frst={1} /></ProviderMock>);
494518
} catch (e) {

0 commit comments

Comments
 (0)