Skip to content

Commit

Permalink
Allow context option in refetchQueries (#3852)
Browse files Browse the repository at this point in the history
* Allow `context` option in `refetchQueries`

These changes adjust `mutate`'s `refetchQueries` option
to allow queries to include a custom `context` option.
This `context` will then be used when refetching the query,
overridding any `context` that was previsouly set with
the query, when it first ran.

These changes are a continuation of #3580. They do
not include automatically setting the `context` of
refetched queries to say the `context` used for
the parent mutation. A `context` has to be specified
with each refetched query, if needed. This was done
to maintain backwards compatibilty (e.g. preventing
issues that might arise by all of a sudden running
refetched queries with a mutation `context`, that
wasn't explicitly requested).

* PR link update
  • Loading branch information
hwillson authored Aug 26, 2018
1 parent 4414154 commit ebf5297
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 9 deletions.
35 changes: 33 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,41 @@

## vNext

### Apollo Client (2.4.1)
### Apollo Client (vNext)

- `mutate`'s `refetchQueries` option now allows queries to include a custom
`context` option. This `context` will be used when refetching the query.
For example:

```js
context = {
headers: {
token: 'some auth token',
},
};
client.mutate({
mutation: UPDATE_CUSTOMER_MUTATION,
variables: {
userId: user.id,
firstName,
...
},
refetchQueries: [{
query: CUSTOMER_MESSAGES_QUERY,
variables: { userId: user.id },
context,
}],
context,
});
```

The `CUSTOMER_MESSAGES_QUERY` above will be refetched using `context`.
Normally queries are refetched using the original context they were first
started with, but this provides a way to override the context, if needed. <br/>
[@hwillson](https://github.com/hwillson) in [#3852](https://github.com/apollographql/apollo-client/pull/3852)

- Documentation updates. <br/>
[@hwillson](https://github.com/hwillson) in [#](https://github.com/apollographql/apollo-client/pull/)
[@hwillson](https://github.com/hwillson) in [#3841](https://github.com/apollographql/apollo-client/pull/3841)


## 2.4.0 (August 17, 2018)
Expand Down
18 changes: 11 additions & 7 deletions packages/apollo-client/src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,17 @@ export class QueryManager<TStore> {
continue;
}

refetchQueryPromises.push(
this.query({
query: refetchQuery.query,
variables: refetchQuery.variables,
fetchPolicy: 'network-only',
}),
);
const queryOptions: QueryOptions = {
query: refetchQuery.query,
variables: refetchQuery.variables,
fetchPolicy: 'network-only',
};

if (refetchQuery.context) {
queryOptions.context = refetchQuery.context;
}

refetchQueryPromises.push(this.query(queryOptions));
}

if (awaitRefetchQueries) {
Expand Down
85 changes: 85 additions & 0 deletions packages/apollo-client/src/core/__tests__/QueryManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4442,6 +4442,91 @@ describe('QueryManager', () => {
);
});

it('should refetch using the specified context, if provided', () => {
const mutation = gql`
mutation changeAuthorName {
changeAuthorName(newName: "Jack Smith") {
firstName
lastName
}
}
`;
const mutationData = {
changeAuthorName: {
firstName: 'Jack',
lastName: 'Smith',
},
};
const query = gql`
query getAuthors($id: ID!) {
author(id: $id) {
firstName
lastName
}
}
`;
const data = {
author: {
firstName: 'John',
lastName: 'Smith',
},
};
const secondReqData = {
author: {
firstName: 'Jane',
lastName: 'Johnson',
},
};
const variables = { id: '1234' };
const queryManager = mockQueryManager(
{
request: { query, variables },
result: { data },
},
{
request: { query, variables },
result: { data: secondReqData },
},
{
request: { query: mutation },
result: { data: mutationData },
},
);

const observable = queryManager.watchQuery<any>({
query,
variables,
notifyOnNetworkStatusChange: false,
});

const headers = {
someHeader: 'some value',
};

return observableToPromise(
{ observable },
result => {
queryManager.mutate({
mutation,
refetchQueries: [
{
query,
variables,
context: {
headers,
},
},
],
});
},
result => {
const context = queryManager.link.operation.getContext();
expect(context.headers).not.toBeUndefined();
expect(context.headers.someHeader).toEqual(headers.someHeader);
},
);
});

afterEach(done => {
// restore standard method
console.warn = oldWarn;
Expand Down
1 change: 1 addition & 0 deletions packages/apollo-client/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type OperationVariables = { [key: string]: any };
export type PureQueryOptions = {
query: DocumentNode;
variables?: { [key: string]: any };
context?: any;
};

export type ApolloQueryResult<T> = {
Expand Down

0 comments on commit ebf5297

Please sign in to comment.