3

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

Pavel Perevezencev
  • 2,596
  • 3
  • 28
  • 49

1 Answers1

3

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
    }
  }
`);
Charly Poly
  • 384
  • 1
  • 8
  • I have the exact same problem, but leaving out the inclusion does not fully solve it for me. I then get the error `UnknownFragment: "UserInputFragment"` from useQuery(). If I do not refer to the fragment by including the variable anymore, where/how do I properly define the fragment so that it is definitely considered and auto-included by the GraphQL Code Generator? – PhilJay Aug 04 '23 at 08:26
  • Ok I could solve it now by also including *.graphql files in the `documents: []` filter in my `CodegenConfig` and then just created a .graphql file where I define my fragments. (The rest of my schema I fetch from an GQL Endpoint where I do not want the fragments to be included) – PhilJay Aug 18 '23 at 11:32