Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix refetchQueries context #3580

Merged
merged 9 commits into from
Jul 20, 2018
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
[@Gamezpedia](https://github.com/Gamezpedia) in [#3612](https://github.com/apollographql/apollo-client/pull/3612)
[@jinxac](https://github.com/jinxac) in [#3647](https://github.com/apollographql/apollo-client/pull/3647)
[@abernix](https://github.com/abernix) in [#3705](https://github.com/apollographql/apollo-client/pull/3705)
[@dandv](https://github.com/dandv) in [#3703](https://github.com/apollographql/apollo-client/pull/3703)
[@dandv](https://github.com/dandv) in [#3703](https://github.com/apollographql/apollo-client/pull/3703)
[@hwillson](https://github.com/hwillson) in [#3580](https://github.com/apollographql/apollo-client/pull/3580)
- Updated `graphql` `peerDependencies` to handle 14.x versions.
[@ivank](https://github.com/ivank) in [#3598](https://github.com/apollographql/apollo-client/pull/3598)
- Add optional generic type params for variables on low level methods.
Expand Down
8 changes: 7 additions & 1 deletion packages/apollo-client/src/__mocks__/mockLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ import {

import { print } from 'graphql/language/printer';

interface MockApolloLink extends ApolloLink {
operation?: Operation;
}

// Pass in multiple mocked responses, so that you can test flows that end up
// making multiple queries to the server
export function mockSingleLink(
...mockedResponses: MockedResponse[]
): ApolloLink {
): MockApolloLink {
return new MockLink(mockedResponses);
}

Expand Down Expand Up @@ -40,6 +44,7 @@ export interface MockedSubscription {
}

export class MockLink extends ApolloLink {
public operation: Operation;
private mockedResponsesByKey: { [key: string]: MockedResponse[] } = {};

constructor(mockedResponses: MockedResponse[]) {
Expand All @@ -60,6 +65,7 @@ export class MockLink extends ApolloLink {
}

public request(operation: Operation) {
this.operation = operation;
const key = requestToKey(operation);
const responses = this.mockedResponsesByKey[key];
if (!responses || responses.length === 0) {
Expand Down
79 changes: 79 additions & 0 deletions packages/apollo-client/src/core/__tests__/QueryManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4362,6 +4362,85 @@ describe('QueryManager', () => {
);
});

it('should refetch using the original query context (if any)', () => {
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 headers = {
someHeader: 'some value',
};
const observable = queryManager.watchQuery<any>({
query,
variables,
context: {
headers,
},
notifyOnNetworkStatusChange: false,
});

return observableToPromise(
{ observable },
result => {
queryManager.mutate({
mutation,
refetchQueries: ['getAuthors'],
});
},
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
9 changes: 8 additions & 1 deletion packages/apollo-client/src/core/watchQueryOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,14 @@ export interface MutationOptions<
mutation: DocumentNode;

/**
* Context to be passed to link execution chain
* The context to be passed to the link execution chain. This context will
* only be used with the mutation. It will not be used with
* `refetchQueries`. Refetched queries use the context they were
* initialized with (since the intitial context is stored as part of the
* `ObservableQuery` instance). If a specific context is needed when
* refetching queries, make sure it is configured (via the
* [`query` `context` option](/docs/react/api/apollo-client.html#ApolloClient.query))
* when the query is first initialized/run.
*/
context?: any;

Expand Down