dotansimha / graphql-code-generator

A tool for generating code based on a GraphQL schema and GraphQL operations (query/mutation/subscription), with flexible support for custom plugins.
https://the-guild.dev/graphql/codegen/
MIT License
10.82k stars 1.33k forks source link

Moving typescript-operations 1->2 generates Maybe-wrapped custom types #6639

Open strazan opened 3 years ago

strazan commented 3 years ago

Describe the bug

Upgrading typescript-operations from v1.x to v2.x causes custom types in codegen to be generated as Maybe<...> wrapped around them.

Downgrading with this diff "solves" the problem:

diff --git a/analytics-app/package.json b/analytics-app/package.json
index 9153ad69..e95b29b7 100644
--- a/analytics-app/package.json
+++ b/analytics-app/package.json
@@ -99,7 +99,7 @@
     "@graphql-codegen/schema-ast": "^2.0.0",
     "@graphql-codegen/typed-document-node": "^2.1.3",
     "@graphql-codegen/typescript": "^1.23.0",
-    "@graphql-codegen/typescript-operations": "^2.1.3",
+    "@graphql-codegen/typescript-operations": "^1.18.4",
     "@graphql-typed-document-node/core": "^3.1.0",
     "@next/bundle-analyzer": "^11.1.0",
     "@storybook/addon-actions": "^6.3.6",
diff --git a/analytics-app/yarn.lock b/analytics-app/yarn.lock
index ffe9f787..c86516ea 100644
--- a/analytics-app/yarn.lock
+++ b/analytics-app/yarn.lock
@@ -1727,14 +1727,14 @@
     change-case-all "1.0.14"
     tslib "~2.3.0"

-"@graphql-codegen/typescript-operations@^2.1.3":
-  version "2.1.3"
-  resolved "https://registry.yarnpkg.com/@graphql-codegen/typescript-operations/-/typescript-operations-2.1.3.tgz#34f94e323c69006788e7eb0f4d039522a95dd31f"
-  integrity sha512-CGRc2mu7NVW+U6J+a66YdLn7UAZFrVO1axBSzgQOCR9OaSJ9Kr2K3Iq/R8PX31YHWu6hDI93Nf8zc2TIiRLDwQ==
+"@graphql-codegen/typescript-operations@^1.18.4":
+  version "1.18.4"
+  resolved "https://registry.yarnpkg.com/@graphql-codegen/typescript-operations/-/typescript-operations-1.18.4.tgz#78149af3a949b760a7af7526593f2b7269a6841a"
+  integrity sha512-bxeRaCCwu2rUXkRj6WwMVazlMignemeUJfDjrK7d4z9o9tyjlrGWnbsjeZI7M17GNCARU9Vkr6XH94wEyooSsA==
   dependencies:
-    "@graphql-codegen/plugin-helpers" "^2.1.1"
-    "@graphql-codegen/typescript" "^2.2.0"
-    "@graphql-codegen/visitor-plugin-common" "2.2.0"
+    "@graphql-codegen/plugin-helpers" "^1.18.8"
+    "@graphql-codegen/typescript" "^1.23.0"
+    "@graphql-codegen/visitor-plugin-common" "1.22.0"
     auto-bind "~4.0.0"
     tslib "~2.3.0"

@@ -1748,16 +1748,6 @@
     auto-bind "~4.0.0"
     tslib "~2.3.0"

-"@graphql-codegen/typescript@^2.2.0":
-  version "2.2.1"
-  resolved "https://registry.yarnpkg.com/@graphql-codegen/typescript/-/typescript-2.2.1.tgz#5655feba721a6b9eb587d40a522dcd729b6a1e51"
-  integrity sha512-U3sJwgw+x9pascy9BTdD+MiLLyvDfldQYahaJBBmaA3OuTihn+GMiOC5+zaoizf5PN5M6pjs2H4aejXbvUWw/A==
-  dependencies:
-    "@graphql-codegen/plugin-helpers" "^2.1.1"
-    "@graphql-codegen/visitor-plugin-common" "2.2.0"
-    auto-bind "~4.0.0"
-    tslib "~2.3.0"
-

To Reproduce Steps to reproduce the behavior:

Configure the world like detailed below, and upgrade @graphql-codegen/typescript with the inverse diff above.

  1. My GraphQL schema:
export type Maybe<T> = T | null;
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
/** All built-in and custom scalars, mapped to their actual values */
export type Scalars = {
  ID: string;
  String: string;
  Boolean: boolean;
  Int: number;
  Float: number;
  _float4: number;
  _float8: number;
  _int2: number;
  _int4: number;
  _numeric: number;
  _uuid: string;
  bigint: string;
  bpchar: string;
  bytea: string;
  date: string;
  float8: number;
  interval: string;
  jsonb: Record<string, unknown> | null;
  numeric: number;
  timestamp: string;
  timestamptz: string;
  uuid: string;
};
export type ModelRuns = {
  readonly __typename?: 'modelRuns';
  readonly appId: Scalars['uuid'];
  readonly id: Scalars['bigint']
}
  1. My GraphQL operations:
query LatestModelRun($appId: uuid!) {
   run: modelRuns {
    id
  }
}
  1. My codegen.yml config file:
overwrite: true

schema:
- http://localhost:8080/v1/graphql:
    headers:
      x-hasura-admin-secret: x

documents:
- src/**/*.graphql
- '!src/schema/graphql.graphql'

generates:
  # basic types from the db
  # https://www.graphql-code-generator.com/docs/plugins/typescript
  src/schema/types.ts:
    plugins:
    - typescript
    config:
      immutableTypes: true
      enumsAsTypes: true
      strictScalars: true
      scalars:
        _numeric: number
        numeric: number
        _int2: number
        _int4: number
        _float8: number
        bigint: string
        bpchar: string
        date: string
        float8: number
        _float4: number
        interval: string
        jsonb: Record<string, unknown> | null
        timestamptz: string
        timestamp: string
        _uuid: string
        uuid: string
        bytea: string

  # can be used to provide typed introspection when setting up Apollo Client
  src/schema/graphql.schema.json:
    plugins:
    - introspection

  # https://www.graphql-code-generator.com/docs/presets/near-operation-file
  src/:
    preset: near-operation-file
    presetConfig:
      baseTypesPath: schema/types.ts
    plugins:
    - typescript-operations

    # https://www.graphql-code-generator.com/docs/plugins/named-operations-object
    # { refetchQueries: [namedOperations.Query.GetConnectors] }
    - named-operations-object

    # https://the-guild.dev/blog/typed-document-node
    - typed-document-node

  # https://www.graphql-code-generator.com/docs/plugins/schema-ast
  src/schema/graphql.graphql:
    plugins:
      - schema-ast
    config:
      includeDirectives: true

Expected behavior v1 output is good enough/working:

export type LatestModelRunQuery = (
  { __typename?: 'query_root' }
  & { run: Array<(
    { __typename?: 'modelRuns' }
    & Pick<Types.ModelRuns, 'id'>
  )> }
);

v2 output:

export type LatestModelRunQuery = { __typename?: 'query_root', run: Array<{ __typename?: 'modelRuns', id: any }> };

Environment:

Additional context Sorry for not providing an actual repro.

n1ru4l commented 3 years ago

Can you create a failing test case for this?

fcpauldiaz commented 3 years ago

@strazan were you able to fix this ?

strazan commented 3 years ago

@strazan were you able to fix this ?

I just use the @graphql-codegen/typescript-operations": "^1.18.4"