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

allow optional variables #200

Merged
merged 1 commit into from
Sep 7, 2016
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
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Expect active development and potentially significant breaking changes in the `0

### vNext

### v0.5.2

- Feature: Allow optional variables by passing null value on behalf of the variable

### v0.5.1

- 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)
Expand Down
8 changes: 7 additions & 1 deletion src/graphql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,20 @@ export default function graphql(
if (opts.variables || !operation.variables.length) return opts;

let variables = {};
for (let { variable } of operation.variables) {
for (let { variable, type } of operation.variables) {
if (!variable.name || !variable.name.value) continue;

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

// allow optional props
if (type.kind !== 'NonNullType') {
variables[variable.name.value] = null;
continue;
}

invariant(typeof props[variable.name.value] !== 'undefined',
`The operation '${operation.name}' wrapping '${getDisplayName(WrappedComponent)}' ` +
`is expecting a variable: '${variable.name.value}' but it was not found in the props ` +
Expand Down
26 changes: 25 additions & 1 deletion test/react-web/client/graphql/queries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ describe('queries', () => {
mount(<ProviderMock client={client}><Container first={null} /></ProviderMock>);
});

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

let error = null;
try {
mount(<ProviderMock client={client}><Container frst={1} /></ProviderMock>);
} catch (e) { error = e; }

expect(error).to.be.null;

});

it('errors if the passed props don\'t contain the needed variables', () => {
const query = gql`
query people($first: Int!) {
allPeople(first: $first) { people { name } }
}
`;
const data = { allPeople: { people: [ { name: 'Luke Skywalker' } ] } };
const variables = { first: 1 };
const networkInterface = mockNetworkInterface({
request: { query, variables },
result: { data },
});
const client = new ApolloClient({ networkInterface });
const Container = graphql(query)(() => null);

try {
mount(<ProviderMock client={client}><Container frst={1} /></ProviderMock>);
} catch (e) {
Expand Down