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.85k stars 1.33k forks source link

typescript-resolver adds an extra property for my custom scalars #9769

Closed davidgtu closed 6 months ago

davidgtu commented 11 months ago

Which packages are impacted by your issue?

"@graphql-codegen/typescript-resolvers"

Describe the bug

I just recently added the typescript-resolvers plugin to my codegen config, and while it does what I need, it seems to also add my custom scalars to the ResolversTypes.

The issue is that it seems to add an extra ['output'] property to these scalars. For example; I end up getting an error:

Property 'output' does not exist on type 'X'

Example:

// generated.ts
/** All built-in and custom scalars, mapped to their actual values */
export type Scalars = {
  ID: string;
  String: string;
  Boolean: boolean;
  Int: number;
  Float: number;
  DataURL: any;
  Date: any;
  DateTime: any;
  Email: any;
  JSON: any;
  PhoneNumber: any;
};

// ....

/** Mapping between all available schema types and the resolvers types */
export type ResolversTypes = {
  Boolean: ResolverTypeWrapper<Scalars['Boolean']['output']>;
  ID: ResolverTypeWrapper<Scalars['ID']['output']>;
  Float: ResolverTypeWrapper<Scalars['Float']['output']>;
  Int: ResolverTypeWrapper<Scalars['Int']['output']>;
  PhoneNumber: ResolverTypeWrapper<Scalars['PhoneNumber']['output']>; // <-- this one weirdly doesn't throw an error?
  String: ResolverTypeWrapper<Scalars['String']['output']>;

/** Mapping between all available schema types and the resolvers parents */
export type ResolversParentTypes = {
  Boolean: Scalars['Boolean']['output'];
  DataURL: Scalars['DataURL']['output']; // <-- this one also doesn't throw an error
  Date: Scalars['Date']['output']; // <-- this one also doesn't throw an error
  DateTime: Scalars['DateTime']['output']; // <-- this one also doesn't throw an error
  Float: Scalars['Float']['output'];
  ID: Scalars['ID']['output'];
  Int: Scalars['Int']['output'];
  String: Scalars['String']['output'];

Am I missing something from my config? Ideally I want to remove this weird ['output'] property

Your Example Website or App

N/A

Steps to Reproduce the Bug or Issue

N/A

Expected behavior

Ideally, I want to not have this ['output'] generated

Screenshots or Videos

No response

Platform

Codegen Config File

generates:
  ./src/generated/generated-mock.ts:
    plugins:
      - typescript-mock-data:
          typesFile: ./graphql.ts
          typeNames: pascal-case#pascalCase
  ./src/generated/graphql.ts:
    plugins:
      - typescript
      - typescript-operations
      - typescript-react-apollo
      - typescript-resolvers

Additional context

No response

rlucha commented 10 months ago

You'll need to downgrade your typescript-resolvers dependency to 3.x (in fact, align all of them to 3.x if possible) as in 4.0 Scalars are defined as


export type Scalars = {
  ID: { input: string; output: string; }
  String: { input: string; output: string; }
  Boolean: { input: boolean; output: boolean; }
  Int: { input: number; output: number; }
  ...
Veer0x1 commented 10 months ago

If I want to use the 4.x version then what changes I have to do ?

rlucha commented 10 months ago

If I want to use the 4.x version then what changes I have to do ?

You can start from this discussion and follow the issues related to it where suggestions have been made: https://github.com/dotansimha/graphql-code-generator/pull/9375#issuecomment-1603413081

eddeee888 commented 6 months ago

Hi @davidgtu,

Your Scalars type should also have input/output. For example:

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 };
  DataURL: { input: any, output: any };
  Date: { input: any, output: any };
  DateTime: { input: any, output: any };
  Email: { input: any, output: any };
  JSON: { input: any, output: any };
  PhoneNumber: { input: any, output: any };
};

I think @graphql-codegen/typescript should be bumped to v4 for it to work. Please try and let me know if it works

eddeee888 commented 6 months ago

Here's an example repo with codegen setup which doesn't have this issue: https://github.com/eddeee888/graphql-server-template.

Please bump your package versions. I'm more than happy to re-open this issue if it's still a problem.