2

I am trying to leverage GraphQL code-gen for my React application (typescript, apollo). This is what the config object looks like in my codegen file:

const config: CodegenConfig = {
  overwrite: true,
  schema: path.join(__dirname, "./schema.graphql"),
  documents: ["src/**/*.tsx", "src/**/*.ts"], // All queries are under "src/queries/*.ts". 
  generates: {
    "src/queries/__generated__/": {
      preset: "client",
      plugins: ["typescript"],
    },
    "./graphql.schema.json": {
      plugins: ["introspection"],
    },
  },
};

This generates the anticipated files including the gql file. There are 14 queries in total, and thus the gql file has 14 overloads for the generated graphql function. However, the very first graphql function definition looks like this:

 *
 * The query argument is unknown!
 * Please regenerate the types.
 */
export function graphql(source: string): unknown;

{...14 other graphql function overloads }

As the comment indicates, the original has a type unknown. I am not able to use this, because Apollo's useQuery hooks complain about a type mismatch and suggest: Argument of type 'unknown' is not assignable to parameter of type 'DocumentNode | TypedDocumentNode<any, {...queryVariables }>'..

What am I doing wrong here? An arbitrary example of how my queries have been written is listed below:

import { graphql } from "./__generated__"; 
export const GET_DATA = graphql(`
  query getData($Criteria: CriteriaInputType) {
    dataSet(criteria: $Criteria) {
          pagedResult {
              ...FragmentTypeA
              ...FragmentTypeB
          }
    },
    validations {
      key
      value
    }
  }
  ...FragmentTypeA,
  ...FragmentTypeB
`);
3
  • This is straight out of the Apollo setup and GraphQL Codegen examples but I can't get either method working, either! Commented Sep 21, 2023 at 20:14
  • What I have tried: using older package versions (@graphql-codegen/cli down to 3, @graphql-codegen/client-preset down to 3, @graphql-typed-document-node/core down to 2, graphql down to 15), running the codegen against the example schema.graphql. Nothing seems to work, I still end up with type unknown Commented Sep 21, 2023 at 20:30
  • I also tried with Node versions down to 16 Commented Sep 21, 2023 at 20:43

1 Answer 1

1

graphql-codegen only generates types for queries that it finds in your tree. Write your queries, then regenerate the types.

You can use graphql-codegen -w to run a watch task.

It's ok that it says

The query argument is unknown! Please regenerate the types.

This message appears even when the types are working -- unknown will still be listed as the type next to the default (source: string) gql function. When all is working, you should see overloaded functions with specific strings (one for each of the queries in your project).

Sign up to request clarification or add additional context in comments.

2 Comments

I see overloaded functions with specific strings for each of the queries in my project. But when I import this graphql function into a query, it defaults to this one, with the unkown type and then useApollo hook just doesn't like it and throws a type mismatch error. Is there a way to import the query specific graphql function?
@user1020069 not that I know of. This happens even after you're re-run the codegen? In that case I'm not sure

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.