graphql-code-generator
graphql-code-generator copied to clipboard
Skip specific fields by directive name
Hi!
Use case I want to generate GraphQL SDK for Gitlab API. I don't want @deprecated fields to be even generated
Proposed solution
We may introduce a new parameter skipDirectives: @deprecated
which which will skip specific fields.
What do you think of this?
Thank you!
Basically I don't want to have these lines in the generated code:
Would you mind posting your codegen.ts
you use with gitlab's GQL API? I'm generating the same thing with the config below, but unable to compile as it contains ~100 duplicates
codegen.ts
import type { CodegenConfig } from "@graphql-codegen/cli";
const config: CodegenConfig = {
schema: "https://gitlab.com/api/graphql",
documents: ["src/gql/**/*.gql"],
debug: true,
verbose: true,
overwrite: true,
ignoreNoDocuments: true,
generates: {
"./src/generated/api.ts": {
preset: "client",
config: {
gqlImport: "@urql/vue#gql",
useTypeImports: true,
dedupeFragments: true,
optimizeDocumentNode: true,
emitLegacyCommonJSImports: false,
},
plugins: ["typescript-vue-urql"],
},
},
hooks: {
afterOneFileWrite: "prettier --write",
},
};
export default config;
When I try to use a generated option from that file I get tons of warnings like so...
Do you spot something I'm doing wrong? Is there some other way to ignore those deprecated fields?
@treystout sure. my source code is here: https://github.com/theplenkov-npm/gitlab-graphql-types/blob/main/codegen.yml
and it generates typescript file like this: https://npm.runkit.com/gitlab-graphql-types
you can browse file graphql.d.ts
meanwhile i've changed my approach now.. I don't use now this large file with all declarations - but generate sdk only for needed queries. Here is example: https://gitlab.com/theplenkov-npm/gitlab-ci-cli/-/blob/main/packages/sdk/codegen.yml it generates me ready-to-use sdk with needed resolvers: https://npm.runkit.com/@gitlaber/sdk/src/generated/sdk.d.ts
However it doesn't deny that fact that generated files contain deprecated attributes. I'd like codegen to skip them somehow
@ThePlenkov thank you for posting your examples, do you not get a ton of duplicate field errors in the enums when you generate? Maybe something in my eslint is wrong? See my screenshot with all the "Duplicate identifier" lines?
update which solved the issue for me was adding ?remove_deprecated=true
when fetching the schema. Updated config...
import type { CodegenConfig } from "@graphql-codegen/cli";
const config: CodegenConfig = {
schema: "https://gitlab.com/api/graphql?remove_deprecated=true",
documents: ["src/gql/**/*.gql"],
debug: true,
verbose: true,
overwrite: true,
ignoreNoDocuments: true,
generates: {
"./src/generated/api.ts": {
config: {
gqlImport: "@urql/vue#gql",
skipTypeName: true,
onlyOperationTypes: true,
useTypeImports: true,
dedupeFragments: true,
optimizeDocumentNode: true,
emitLegacyCommonJSImports: false,
},
plugins: ["typescript", "typescript-operations", "typescript-vue-urql"],
},
},
hooks: {
afterOneFileWrite: "prettier --write",
},
};
export default config;
oh nice! I didn't know that! Well done, man!