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

Commit

Permalink
fix updateQuery early usage (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
James Baxley committed Sep 9, 2016
1 parent 49f9a9e commit 4042b7e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 7 deletions.
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.4

- Bug: Created better reference to updateQuery when bound early. It will also throw if called before it should be.

### v0.5.3

- Bug: Fixed issue with updateQuery not being present during componentWillMount [#203](https://github.com/apollostack/react-apollo/pull/203)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-apollo",
"version": "0.5.3",
"version": "0.5.4",
"description": "React data container for Apollo Client",
"main": "index.js",
"scripts": {
Expand Down
13 changes: 8 additions & 5 deletions src/graphql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ export default function graphql(
// request / action storage
private queryObservable: ObservableQuery | Object;
private querySubscription: Subscription | Object;
private updateQueryMethod: any;

// calculated switches to control rerenders
private haveOwnPropsChanged: boolean;
Expand Down Expand Up @@ -352,7 +351,14 @@ export default function graphql(
};

// XXX type this better
queryData.updateQuery = (mapFn: any) => this.updateQueryMethod = mapFn;
// this is a stub for early binding of updateQuery before data
queryData.updateQuery = (mapFn: any) => {
invariant(!!(this.queryObservable as ObservableQuery).updateQuery, `
Update query has been called before query has been created
`);

(this.queryObservable as ObservableQuery).updateQuery(mapFn);
};

if (!forceFetch) {
try {
Expand Down Expand Up @@ -421,9 +427,6 @@ export default function graphql(
const observableQuery = watchQuery(queryOptions);
const { queryId } = observableQuery;

// bind any eariler set updateQuery method
if (this.updateQueryMethod) observableQuery.updateQuery(this.updateQueryMethod);

// the shape of the query has changed
if (previousQuery.queryId && previousQuery.queryId !== queryId) {
this.data = assign(this.data, { loading: true });
Expand Down
65 changes: 64 additions & 1 deletion test/react-web/client/graphql/queries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,6 @@ describe('queries', () => {
componentWillMount() { // tslint:disable-line
expect(this.props.data.updateQuery).to.be.exist;
expect(this.props.data.updateQuery).to.be.instanceof(Function);
expect(this.props.data.updateQuery).to.not.throw;
done();
}
render() {
Expand All @@ -1195,6 +1194,70 @@ describe('queries', () => {
mount(<ProviderMock client={client}><Container /></ProviderMock>);
});

it('updateQuery throws if called before data has returned', (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> {
componentWillMount() { // tslint:disable-line
expect(this.props.data.updateQuery).to.be.exist;
expect(this.props.data.updateQuery).to.be.instanceof(Function);
try {
this.props.data.updateQuery();
done(new Error('should have thrown'))
} catch (e) {
expect(e).to.match(/Invariant Violation:/);
done();
}
}
render() {
return null;
}
};

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

it('allows updating query results after query has finished (early binding)', (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> {
public updateQuery: any;
componentWillMount() {
this.updateQuery = this.props.data.updateQuery;
}
componentWillReceiveProps(props) {
if (isUpdated) {
expect(props.data.allPeople).to.deep.equal(data2.allPeople);
done();
return;
} else {
isUpdated = true;
this.updateQuery((prev) => {
return data2;
});
}
}
render() {
return null;
}
};

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' } ] } };
Expand Down

0 comments on commit 4042b7e

Please sign in to comment.