I’m working in a SvelteKit project and want to use gql codegen, but all queries are typed as unknown.
Ts file:
import { graphql } from '$lib/gql/index.js';
export const getPostById = graphql(`
query GET_ARTICLE($id: bigint!) {
articles_by_pk(id: $id) {
content
created_at
slug
updated_at
title
}
}
`);
codegen.ts
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
overwrite: true,
schema: "https:...myap",
debug: true,
verbose:true,
watch:true,
ignoreNoDocuments: true,
emitLegacyCommonJSImports: false,
documents:["./src/**/*.svelte", "./src/**/*.ts"],
generates: {
"src/lib/gql/": {
preset: 'client',
plugins: []
},
"./graphql.schema.json": {
plugins: ["introspection"]
}
}
};
export default config;
The const documents = [];
in the gql.ts file is empty (I think it shouldn’t).
The package.json script includes: "codegen": "graphql-codegen-esm --config codegen.ts"
Running the script does not throw any errors. I have no idea where to start looking.
2
1 Answer
I had exactly the same problem, but with React. The solution was the following: there is a documents
property in codegen.ts
. It shows the path to your queries and mutations. If Codegen can’t find your queries and mutations files it doesn’t throw any error (unfortunately). It just silently generates empty types, and you spending time trying to figure out why type is "unknown".
My queries and mutations where in another folder so Codegen could not find them. I changed the path in documents
and it helped.
In the generated file, there should be a type definition for the
graphql
function. It uses TypeScripts overload syntax. If there is not one case that looks roughly linke this:function graphql(document: "n query GET_ARTICLE($id: bigint!) {n ..."): TypedDocumentNode<...>
, then your codegen script does not seem to work. Check, if your glob patterns correctly match your file`.May 30 at 20:59
Im on sveltekit wich uses "=" in its folder names. The code generator ignores these folders. Issue : github.com/dotansimha/graphql-code-generator/issues/8978
May 31 at 14:18