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

Commit 7a63b82

Browse files
fracmakhwillson
authored andcommitted
Fixes refetchQueries with string using wrong variables (#2422)
* Fixes issue where operatons map isn't updated on updateQuery * Changelog update
1 parent e1974df commit 7a63b82

File tree

3 files changed

+121
-4
lines changed

3 files changed

+121
-4
lines changed

Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
- Mutation errors are now properly returned as a render prop, when using
2424
a default `errorPolicy` of `all`. <br/>
2525
[@amacleay](https://github.com/amacleay) in [#2374](https://github.com/apollographql/react-apollo/pull/2374)
26+
- `<Mutation />` `refetchQueries` triggered by name (string) will now use the correct variables. <br/>
27+
[@fracmal](https://github.com/fracmak) in [#2422](https://github.com/apollographql/react-apollo/pull/2422)
2628

2729
## 2.2.2 (September 28, 2018)
2830

src/Query.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,18 +286,26 @@ export default class Query<TData = any, TVariables = OperationVariables> extends
286286
private initializeQueryObservable(props: QueryProps<TData, TVariables>) {
287287
const opts = this.extractOptsFromProps(props);
288288
// save for backwards compat of refetcherQueries without a recycler
289+
this.setOperations(opts);
290+
this.queryObservable = this.client.watchQuery(opts);
291+
}
292+
293+
private setOperations(props: QueryProps<TData, TVariables>) {
289294
if (this.context!.operations) {
290295
this.context!.operations!.set(this.operation!.name, {
291-
query: opts.query,
292-
variables: opts.variables,
296+
query: props.query,
297+
variables: props.variables,
293298
});
294299
}
295-
this.queryObservable = this.client.watchQuery(opts);
296300
}
297301

298302
private updateQuery(props: QueryProps<TData, TVariables>) {
299303
// if we skipped initially, we may not have yet created the observable
300-
if (!this.queryObservable) this.initializeQueryObservable(props);
304+
if (!this.queryObservable) {
305+
this.initializeQueryObservable(props);
306+
} else {
307+
this.setOperations(props);
308+
}
301309

302310
this.queryObservable!.setOptions(this.extractOptsFromProps(props))
303311
// The error will be passed to the child container, so we don't

test/client/Mutation.test.tsx

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,113 @@ it('allows a refetchQueries prop', done => {
911911
);
912912
});
913913

914+
it('allows a refetchQueries prop as string and variables have updated', done => {
915+
const query = gql`
916+
query people($first: Int) {
917+
allPeople(first: $first) {
918+
people {
919+
name
920+
}
921+
}
922+
}
923+
`;
924+
925+
const peopleData1 = {
926+
allPeople: { people: [{ name: 'Luke Skywalker', __typename: 'Person' }], __typename: 'People' },
927+
};
928+
const peopleData2 = {
929+
allPeople: { people: [{ name: 'Han Solo', __typename: 'Person' }], __typename: 'People' },
930+
};
931+
const peopleData3 = {
932+
allPeople: { people: [{ name: 'Lord Vader', __typename: 'Person' }], __typename: 'People' },
933+
};
934+
const peopleMocks = [
935+
...mocks,
936+
{
937+
request: { query, variables: { first: 1 } },
938+
result: { data: peopleData1 },
939+
},
940+
{
941+
request: { query, variables: { first: 2 } },
942+
result: { data: peopleData2 },
943+
},
944+
{
945+
request: { query, variables: { first: 2 } },
946+
result: { data: peopleData3 },
947+
},
948+
];
949+
950+
const refetchQueries = ['people'];
951+
952+
let count = 0;
953+
class Component extends React.Component {
954+
state = {
955+
variables: {
956+
first: 1,
957+
},
958+
};
959+
componentDidMount() {
960+
setTimeout(() => {
961+
this.setState({
962+
variables: {
963+
first: 2,
964+
},
965+
});
966+
}, 50);
967+
}
968+
render() {
969+
const { variables } = this.state;
970+
971+
return (
972+
<Mutation mutation={mutation} refetchQueries={refetchQueries}>
973+
{(createTodo, resultMutation) => (
974+
<Query query={query} variables={variables}>
975+
{resultQuery => {
976+
if (count === 0) {
977+
// initial loading
978+
expect(resultQuery.loading).toBe(true);
979+
} else if (count === 1) {
980+
// initial loaded
981+
expect(resultQuery.loading).toBe(false);
982+
} else if (count === 2) {
983+
// first: 2 loading
984+
expect(resultQuery.loading).toBe(true);
985+
} else if (count === 3) {
986+
// first: 2 loaded
987+
expect(resultQuery.loading).toBe(false);
988+
setTimeout(() => {
989+
createTodo();
990+
});
991+
} else if (count === 4) {
992+
// mutation loading
993+
expect(resultMutation.loading).toBe(true);
994+
} else if (count === 5) {
995+
// mutation loaded
996+
expect(resultMutation.loading).toBe(false);
997+
} else if (count === 6) {
998+
// query refetched
999+
expect(resultQuery.loading).toBe(false);
1000+
expect(resultMutation.loading).toBe(false);
1001+
expect(stripSymbols(resultQuery.data)).toEqual(peopleData3);
1002+
done();
1003+
}
1004+
count++;
1005+
return null;
1006+
}}
1007+
</Query>
1008+
)}
1009+
</Mutation>
1010+
);
1011+
}
1012+
}
1013+
1014+
mount(
1015+
<MockedProvider mocks={peopleMocks}>
1016+
<Component />
1017+
</MockedProvider>,
1018+
);
1019+
});
1020+
9141021
it('allows refetchQueries to be passed to the mutate function', done => {
9151022
const query = gql`
9161023
query getTodo {

0 commit comments

Comments
 (0)