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

Commit

Permalink
Merge pull request #376 from apollostack/358-skip-to-no-skip
Browse files Browse the repository at this point in the history
Ensure that we always change options, even when unsubbed
  • Loading branch information
Tom Coleman authored Dec 22, 2016
2 parents 7868957 + 7eae575 commit 4cbbd39
Show file tree
Hide file tree
Showing 3 changed files with 251 additions and 197 deletions.
44 changes: 22 additions & 22 deletions src/graphql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export default function graphql(
if (this.type === DocumentType.Mutation) return;

if (!this.shouldSkip(this.props)) {
this.subscribeToQuery(this.props);
this.subscribeToQuery();
}
}

Expand All @@ -222,8 +222,8 @@ export default function graphql(
return;
}

// we got new props, we need to unsubscribe and re-subscribe with the new data
this.subscribeToQuery(nextProps);
this.updateQuery(nextProps);
this.subscribeToQuery();
}

shouldComponentUpdate(nextProps, nextState, nextContext) {
Expand Down Expand Up @@ -315,6 +315,24 @@ export default function graphql(
}
}

updateQuery(props) {
const opts = this.calculateOptions(props) as QueryOptions;

// if we skipped initially, we may not have yet created the observable
if (!this.queryObservable) {
this.createQuery(opts);
}

if (this.queryObservable._setOptionsNoResult) {
// Since we don't care about the result, use a hacky version to
// work around https://github.com/apollostack/apollo-client/pull/694
// This workaround is only present in Apollo Client 0.4.21
this.queryObservable._setOptionsNoResult(opts);
} else {
this.queryObservable.setOptions(opts);
}
}

// For server-side rendering (see server.ts)
fetchData(): Promise<ApolloQueryResult> | boolean {
if (this.shouldSkip()) return false;
Expand All @@ -336,29 +354,11 @@ export default function graphql(
}
}

subscribeToQuery(props): boolean {
const opts = this.calculateOptions(props) as QueryOptions;

// We've subscribed already, just update with our new options and
// take the latest result
subscribeToQuery() {
if (this.querySubscription) {
if (this.queryObservable._setOptionsNoResult) {
// Since we don't care about the result, use a hacky version to
// work around https://github.com/apollostack/apollo-client/pull/694
// This workaround is only present in Apollo Client 0.4.21
this.queryObservable._setOptionsNoResult(opts);
} else {
this.queryObservable.setOptions(opts);
}

return;
}

// if we skipped initially, we may not have yet created the observable
if (!this.queryObservable) {
this.createQuery(opts);
}

const next = (results: any) => {
if (this.type === DocumentType.Subscription) {
// Subscriptions don't currently support `currentResult`, so we
Expand Down
55 changes: 55 additions & 0 deletions test/react-web/client/graphql/queries.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,61 @@ describe('queries', () => {
renderer.create(<ApolloProvider client={client}><Parent /></ApolloProvider>);
});

it('allows you to skip then unskip a query with new options (top-level syntax)', (done) => {
const query = gql`query people($first: Int) { allPeople(first: $first) { people { name } } }`;
const dataOne = { allPeople: { people: [ { name: 'Luke Skywalker' } ] } };
const dataTwo = { allPeople: { people: [ { name: 'Leia Skywalker' } ] } };
const networkInterface = mockNetworkInterface(
{ request: { query, variables: { first: 1 } }, result: { data: dataOne } },
{ request: { query, variables: { first: 2 } }, result: { data: dataTwo } },
{ request: { query, variables: { first: 2 } }, result: { data: dataTwo } },
);
const client = new ApolloClient({ networkInterface, addTypename: false });

let hasSkipped = false;
@graphql(query, { skip: ({ skip }) => skip })
class Container extends React.Component<any, any> {8
componentWillReceiveProps(newProps) {
if (newProps.skip) {
hasSkipped = true;
// change back to skip: false, with a different variable
this.props.setState({ skip: false, first: 2 });
} else {
if (hasSkipped) {
if (!newProps.data.loading) {
expect(newProps.data.allPeople).toEqual(dataTwo.allPeople);
done();
}
} else {
expect(newProps.data.allPeople).toEqual(dataOne.allPeople);
this.props.setState({ skip: true });
}
}
}
render() {
return null;
}
};

class Parent extends React.Component<any, any> {
constructor() {
super();
this.state = { skip: false, first: 1 };
}
render() {
return (
<Container
skip={this.state.skip}
first={this.state.first}
setState={(state) => this.setState(state)}
/>
);
}
};

renderer.create(<ApolloProvider client={client}><Parent /></ApolloProvider>);
});

it('allows you to skip then unskip a query with opts syntax', (done) => {
const query = gql`query people { allPeople(first: 1) { people { name } } }`;
const data = { allPeople: { people: [ { name: 'Luke Skywalker' } ] } };
Expand Down
Loading

0 comments on commit 4cbbd39

Please sign in to comment.