typescriptgraphqlgraphql-codegen

The query is always of type unknown when created with @graphql-codegen


I have a this graphql query:

import { graphql } from '../__generated__/gql';

// typeof IndexPageQuery is unknown
const IndexPageQuery = graphql(`
  query IndexPageQuery {
    user {
      ...UserInputFragment
    }
  }
  ${UserInputFragment}
`);

And when i run this code:

const { data } = await apolloClient.query(IndexPageQuery);

TS throw error:

Argument of type 'unknown' is not assignable to parameter of type 'QueryOptions<OperationVariables, any>'.ts(2345)

Why IndexPageQuery always has type unknown? Does anyone know what I'm doing wrong? Thanks


Solution

  • I'm Charly, from The Guild, working on GraphQL Code Generator.

    The preset: 'client' does not require to include fragments documents in the GraphQL operation definition (it does it for you automatically).

    To fix your issue, please replace your IndexPageQuery with the following:

    const IndexPageQuery = graphql(`
      query IndexPageQuery {
        user {
          ...UserInputFragment
        }
      }
    `);