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

1

1

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

Dung answered 14/10, 2022 at 10:47 Comment(0)
C
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
    }
  }
`);
Cobweb answered 17/10, 2022 at 9:10 Comment(2)
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?Mohammedanism
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)Mohammedanism

© 2022 - 2024 — McMap. All rights reserved.