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

Type generation skips `type` declarations inside `.graphql` schema #10168

Open KieranTH opened 6 days ago

KieranTH commented 6 days ago

Which packages are impacted by your issue?

@graphql-codegen/cli, @graphql-codegen/typescript

Describe the bug

When using the typescript plugin to attempt to generate types from a GraphQL Schema - it seems although the plugin ignores type declarations inside the schema.

type Test {
 name: String
}

input TestCreate {
name: String
}

Only the Input type would be generated in this example.

Here is the result:

/* eslint-disable */
export type Maybe<T> = T | null;
export type InputMaybe<T> = Maybe<T>;
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]> };
export type MakeEmpty<T extends { [key: string]: unknown }, K extends keyof T> = { [_ in K]?: never };
export type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never };
/** All built-in and custom scalars, mapped to their actual values */
export type Scalars = {
  ID: { input: string; output: string; }
  String: { input: string; output: string; }
  Boolean: { input: boolean; output: boolean; }
  Int: { input: number; output: number; }
  Float: { input: number; output: number; }
};

export type TestCreate = {
  name: Scalars['String']['input'];
};

Your Example Website or App

https://codesandbox.io/p/sandbox/nlpsfz

Steps to Reproduce the Bug or Issue

  1. Go to sandbox.
  2. Download directory
  3. run npm i && npm run compile:codegen
  4. Check the ./src/__gen__ folder and see that only the TestCreate type is generated and not Test

Expected behavior

Seeing as the plugin is described as generating types for a SCHEMA - the Schema also included type declarations. These should be generated too.

Screenshots or Videos

No response

Platform

Codegen Config File

const config = {
  schema: "./schema.graphql",

  generates: {
    "./src/__gen__/": {
      preset: "client",
      presetConfig: {
        gqlTagName: "gql",
        fragmentMasking: false,
      },
    },
  },
  ignoreNoDocuments: true,
};

export default config;

Additional context

No response

gutnidev commented 3 days ago

I encountered the same issue. When codegen generates types, it ignores the type declarations of specific entities.

I resolved the problem by downgrading the "@graphql-codegen/client-preset" package to version "4.3.3".

KieranTH commented 3 days ago

I encountered the same issue.

When codegen generates types, it ignores the type declarations of specific entities.

I resolved the problem by downgrading the "@graphql-codegen/client-preset" package to version "4.3.3".

Oh so this is a regression?!

Thank you for that solution - I'll try that and see if it helps

drzastwakamil commented 3 days ago

Yeah looks like it's the new default behaviour in 4.4.0 version. It seems like that's connected to how the client-preset is configuring the typescript plugin and only enabling enum and operation types 🤔

drzastwakamil commented 3 days ago

@KieranTH looks like there is already an open issue about this. Have a look -> the open issue