2

I'm using graphql codegen hooks, which results are of the type: ApolloReactCommon.QueryResult, meaning it's an object that has data, loading and error fields.

Most generally, I'll use the data in other components, where I need to define its type. I currently need to write something like this to get data type:

type MyPost = MyPostQueryHookResult['data']

I would have expected to get this MyPost type created for me. Is there a way to have it generated automatically?

My config:

generates:
  src/generated/graphql.tsx:
    plugins:
      - "typescript"
      - "typescript-operations"
      - "typescript-react-apollo"
    config:
      withHooks: true
Tbaut
  • 306
  • 1
  • 7

1 Answers1

0

The best way to do this is to use fragments when building the query. Types will be generated for each fragment and they can then be used directly.

source: from the main contributor of graphql-codegen

Tbaut
  • 306
  • 1
  • 7
  • The owner didn't say which is the best way. It's more like your choice. Personally, I'm against using fragments because it's more complex. Using bridges is more convenient. – deathemperor Feb 10 '22 at 09:22