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

Expose updateQuery #152

Closed
wants to merge 5 commits into from
Closed
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"apollo-client": "^0.4.0"
},
"devDependencies": {
"apollo-client": "^0.4.0",
"apollo-client": "^0.4.11",
"babel-jest": "^14.1.0",
"babel-preset-react-native": "^1.9.0",
"browserify": "^13.0.0",
Expand Down
10 changes: 7 additions & 3 deletions src/graphql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ export default function graphql(
fetchMore,
startPolling,
stopPolling,
updateQuery,
oldData = {};

const next = ({ data = oldData, loading, error }: any) => {
Expand All @@ -429,13 +430,14 @@ export default function graphql(
'refetch' in data ||
'fetchMore' in data ||
'startPolling' in data ||
'stopPolling' in data
'stopPolling' in data ||
'updateQuery' in data
);

invariant(!resultKeyConflict,
`the result of the '${graphQLDisplayName}' operation contains keys that ` +
`conflict with the return object. 'errors', 'loading', ` +
`'startPolling', 'stopPolling', 'fetchMore', and 'refetch' cannot be ` +
`'startPolling', 'stopPolling', 'fetchMore', 'updateQuery', and 'refetch' cannot be ` +
`returned keys`
);

Expand All @@ -453,6 +455,7 @@ export default function graphql(
startPolling,
stopPolling,
fetchMore,
updateQuery,
error,
}, data);

Expand Down Expand Up @@ -499,10 +502,11 @@ export default function graphql(
fetchMore = createBoundRefetch((this.queryObservable as any).fetchMore);
startPolling = (this.queryObservable as any).startPolling;
stopPolling = (this.queryObservable as any).stopPolling;
updateQuery = (this.queryObservable as any).updateQuery;

// XXX the tests seem to be keeping the error around?
delete this.data.error;
this.data = assign(this.data, { refetch, startPolling, stopPolling, fetchMore, variables });
this.data = assign(this.data, { refetch, startPolling, stopPolling, updateQuery, fetchMore, variables });
}

forceRenderChildren() {
Expand Down
57 changes: 57 additions & 0 deletions test/react-web/client/graphql/queries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,28 @@ describe('queries', () => {
mount(<ProviderMock client={client}><Container /></ProviderMock>);
});

it('exposes updateQuery as part of the props api', (done) => {
const query = gql`query people { allPeople(first: 1) { people { name } } }`;
const data = { allPeople: { people: [ { name: 'Luke Skywalker' } ] } };
const networkInterface = mockNetworkInterface({ request: { query }, result: { data } });
const client = new ApolloClient({ networkInterface });

@graphql(query)
class Container extends React.Component<any, any> {
componentWillReceiveProps({ data }) { // tslint:disable-line
expect(data.updateQuery).to.be.exist;
expect(data.updateQuery).to.be.instanceof(Function);
expect(data.updateQuery).to.not.throw;
done();
}
render() {
return null;
}
};

mount(<ProviderMock client={client}><Container /></ProviderMock>);
});


it('resets the loading state after a refetched query', (done) => {
const query = gql`query people { allPeople(first: 1) { people { name } } }`;
Expand Down Expand Up @@ -829,4 +851,39 @@ describe('queries', () => {
mount(<ProviderMock client={client}><Container /></ProviderMock>);
});


it('allows updating query results after query has finished', (done) => {
const query = gql`query people { allPeople(first: 1) { people { name } } }`;
const data = { allPeople: { people: [ { name: 'Luke Skywalker' } ] } };
const data2 = { allPeople: { people: [ { name: 'Leia Skywalker' } ] } };
const networkInterface = mockNetworkInterface(
{ request: { query }, result: { data } },
{ request: { query }, result: { data: data2 } }
);
const client = new ApolloClient({ networkInterface });

let isUpdated;
@graphql(query)
class Container extends React.Component<any, any> {
componentWillReceiveProps(props) {
if (isUpdated) {
expect(props.data.allPeople).to.deep.equal(data2.allPeople);
done();
return;
}
else {
isUpdated = true;
props.data.updateQuery((prev) => {
return data2;
});
}
}
render() {
return null;
}
};

mount(<ProviderMock client={client}><Container /></ProviderMock>);
});

});