Improve typings by providing `mutation`, `query`, `subscription` instead of `graphql`
See original GitHub issueCurrently, HOCs for apollo-react
generated using graphql
which is quite obvious, but it actually has some flaws described in this issue:
https://github.com/apollographql/react-apollo/issues/2568
Long story short, both Mutation
and Query
function arguments are merged using |
so both mutation
and data
end up in props
arguments object.
To provide better typing you can import query
, mutation
, subscription
from react-apollo
and use them instead of graphql
E.g
import { mutation } from 'react-apollo/mutation-hoc' // import directly, since not exported from root.
export function CreateMessageHOC<TProps, TChildProps = any>(
operationOptions:
| ReactApollo.OperationOption<
TProps,
CreateMessageMutation,
CreateMessageVariables,
CreateMessageProps<TChildProps>
>
| undefined,
) {
return mutation< // mutation here instead of graphql
TProps,
CreateMessageMutation,
CreateMessageVariables,
CreateMessageProps<TChildProps>
>(CreateMessageDocument, operationOptions)
This way typings are a little more correct.
Although this is hopefully will be addressed from react-apollo
side as well if this will be merged
https://github.com/apollographql/react-apollo/pull/2573
It will change signature to withMutation
etc. And will be exported from the root, so ReactApollo.withMutation
will be possible.
You can fix it right away now, or you can postpone it until PR above is not merged and released.
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (4 by maintainers)
Top GitHub Comments
@dotansimha @ardatan https://github.com/apollographql/react-apollo/pull/2573 PR mentioned above has been merged. So
with*
thing will be real very soon.@ardatan I think you can use the codegen core to expose
hasQueries
,hasMutations
andhasSubscriptions
here: https://github.com/dotansimha/graphql-code-generator/blob/master/packages/graphql-codegen-core/src/operations/transform-document.ts#L110-L111 and then it will make it easier to know which imports do you actually need.