Split typescript types from resolvers #8375
-
I have a big set of files with different types and resolvers and for several reasons, I'd like to split up the generated typescript files for the graphql types from the resolvers, so let's say we have this schema and resolvers (very simplified):
Now currently I generate one big file for typescript:
But I'd like to split it, something like :
In this case the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi @remko79, Splitting types and resolvers types can be achieved by using the codegen.ts (but also works as import { CodegenConfig } from "@graphql-codegen/cli";
const config: CodegenConfig = {
schema: "schema.graphql",
documents: ["src/**/*.ts"],
generates: {
"types.ts": {
plugins: ["typescript"],
},
"resolvers-types.ts": {
preset: "import-types-preset",
presetConfig: {
typesPath: "./types.js", // .js for ESM support
},
plugins: ["typescript-resolvers"],
},
},
};
export default config; |
Beta Was this translation helpful? Give feedback.
-
Thanks for the answer. After some testing and diving into the code of the import-types-preset plugin, I see that that only works if you have the "documents" set in the configuration. We're not using that, just the schema. So after more searching, I've actually found someone with exactly the same issue: https://github.com/dotansimha/graphql-code-generator/issues/5775 So adding the plugin https://www.the-guild.dev/graphql/codegen/plugins/other/add as described in that issue and the import-types-preset, it's working for me now. Thanks for the quick reply 👍 |
Beta Was this translation helpful? Give feedback.
Hi @remko79,
Splitting types and resolvers types can be achieved by using the
import-types-preset
plugin as showcased in this working example: https://github.com/charlypoly/codegen-repros/blob/master/resolvers-import-types/codegen.tscodegen.ts (but also works as
.yml
,.json
or.js
config)