graphql-code-generator icon indicating copy to clipboard operation
graphql-code-generator copied to clipboard

Skip specific fields by directive name

Open ThePlenkov opened this issue 2 years ago • 1 comments

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!

ThePlenkov avatar Sep 07 '22 14:09 ThePlenkov

Basically I don't want to have these lines in the generated code: image

ThePlenkov avatar Sep 07 '22 14:09 ThePlenkov

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... image

Do you spot something I'm doing wrong? Is there some other way to ignore those deprecated fields?

treystout avatar Nov 29 '22 19:11 treystout

@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

ThePlenkov avatar Nov 29 '22 19:11 ThePlenkov

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 avatar Nov 29 '22 20:11 ThePlenkov

@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?

treystout avatar Dec 05 '22 15:12 treystout

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;

treystout avatar Dec 05 '22 19:12 treystout

oh nice! I didn't know that! Well done, man!

ThePlenkov avatar Dec 07 '22 08:12 ThePlenkov