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

Fix Regression where variables passed in graphql HOC options were not merged with mutation variables #2216

Merged
merged 7 commits into from
Sep 21, 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: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
[@olistic](https://github.com/olistic) in [#2190](https://github.com/apollographql/react-apollo/pull/2190)
- Import `lodash/flowRight` using ES import to allow for treeshaking. <br/>
[@Pajn](https://github.com/Pajn) in [#2332](https://github.com/apollographql/react-apollo/pull/2332)
- Fixed a regression where `variables` passed in `graphql` HOC `options` were
not merged with mutation `variables`. <br/>
[@samginn](https://github.com/samginn) in [#2216](https://github.com/apollographql/react-apollo/pull/2216)

## 2.1.11 (August 9, 2018)

Expand Down
13 changes: 9 additions & 4 deletions src/Mutation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,9 @@ class Mutation<TData = any, TVariables = OperationVariables> extends React.Compo
context = {},
awaitRefetchQueries = false,
} = this.props;
let refetchQueries = options.refetchQueries || this.props.refetchQueries;
const mutateOptions = { ...options };

let refetchQueries = mutateOptions.refetchQueries || this.props.refetchQueries;
// XXX this will be removed in the 3.0 of Apollo Client. Currently, we
// support refectching of named queries which just pulls the latest
// variables to match. This forces us to either a) keep all queries around
Expand All @@ -204,18 +206,21 @@ class Mutation<TData = any, TVariables = OperationVariables> extends React.Compo
return this.context.operations.get(x) || x;
return x;
});
delete options.refetchQueries;
delete mutateOptions.refetchQueries;
}

const mutateVariables = Object.assign({}, variables, mutateOptions.variables);
delete mutateOptions.variables;

return this.client.mutate({
mutation,
variables,
optimisticResponse,
refetchQueries,
awaitRefetchQueries,
update,
context,
...options,
variables: mutateVariables,
...mutateOptions,
});
};

Expand Down
41 changes: 41 additions & 0 deletions test/client/graphql/mutations/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,45 @@ describe('graphql(mutation)', () => {
</ApolloProvider>,
);
});

it('can execute a mutation with variables from BOTH options and arguments', done => {
const queryWithVariables = gql`
mutation addPerson($first: Int!, $second: Int!) {
allPeople(first: $first) {
people {
name
}
}
}
`;
client = createClient(expectedData, queryWithVariables, { first: 1, second: 2 });

interface Props {}

const Container = graphql<Props>(queryWithVariables, {
options: () => ({
variables: { first: 1 },
}),
})(
class extends React.Component<ChildProps<Props>> {
componentDidMount() {
this.props.mutate!({
variables: { second: 2 },
}).then(result => {
expect(stripSymbols(result && result.data)).toEqual(expectedData);
done();
});
}
render() {
return null;
}
},
);

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