question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve typings by providing `mutation`, `query`, `subscription` instead of `graphql`

See original GitHub issue

Currently, 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:closed
  • Created 5 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
RIP21commented, Dec 5, 2018

@dotansimha @ardatan https://github.com/apollographql/react-apollo/pull/2573 PR mentioned above has been merged. So with* thing will be real very soon.

1reaction
dotansimhacommented, Dec 4, 2018

@ardatan I think you can use the codegen core to expose hasQueries, hasMutations and hasSubscriptions 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript with Apollo Client - Apollo GraphQL Docs
In previous versions of Apollo Client, render prop components ( Query , Mutation and Subscription ) could be extended to add additional type...
Read more >
TypedDocumentNode: the next generation of GraphQL and ...
TypedDocumentNode is a development tool for creating fully typed DocumentNode objects. It means that just by passing the GraphQL query/mutation ...
Read more >
A First Look at GraphQL Queries, Mutations and Subscriptions
In this post, we give you insight into GraphQL features covering queries, mutations, and subscriptions.
Read more >
Your Guide to GraphQL with TypeScript - Hasura
In the case of our GraphQL tooling, we'll provide an API with a couple ... it has three properties -- query , mutation...
Read more >
GraphQL vs. REST APIs: Why you shouldn't use GraphQL
For example, you cannot have more depth than what is specified in the schema or in the query, schemas that you can modify...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found