Graphql-code-generator: [typescript] Generated QueryResolvers has invalid arguments to RequiredFields type

Created on 12 Jul 2019  ยท  5Comments  ยท  Source: dotansimha/graphql-code-generator

Describe the bug

I've provided the source grapqhl as well as the broken output below, along with comments and emojis.

As you will see the implementation of the query resolvers has an invalid implementation of the generated RequireFields type.

To Reproduce

  1. My GraphQL schema:
type Query {
  organisations(limit: Int = 10, offset: Int = 0): OrganisationPagedResult
}
  1. My codegen.yml config file:
    plugins:
      - add: '/* eslint-disable */'
      - typescript
      - typescript-resolvers
  1. Generated code:
// Generated type expects 2 type arguments ๐Ÿ‘€
//                          ๐Ÿ‘‡
export type RequireFields<T, K extends keyof T> = {
  [X in Exclude<keyof T, K>]?: T[X]
} &
  { [P in K]-?: NonNullable<T[P]> };

export type QueryResolvers<
  ContextType = GraphqlContext,
  ParentType = ResolversParentTypes['Query']
> = {
  organisations?: Resolver<
    Maybe<ResolversTypes['OrganisationPagedResult']>,
    ParentType,
    ContextType,
    // โŒ But is getting provided 3 arguments ๐Ÿ˜ฑ
    //                  ๐Ÿ‘‡                  ๐Ÿ‘‡      ๐Ÿ‘‡
    RequireFields<QueryOrganisationsArgs, 'limit', 'offset'>
    // โœ… Should be:
    // RequireFields<QueryOrganisationsArgs, 'limit' | 'offset'>
  >;
}

Expected behavior

Generated code should rather be of form:

RequireFields<QueryOrganisationsArgs, 'limit' | 'offset'>

Environment:

  • OSX
  • @graphql-codegen/typescript: 1.4.0
  • NodeJS: 10
bug plugins waiting-for-release

Most helpful comment

I have nothing but unending love and gratitude to you for being such an amazing steward to this project.

All 5 comments

You are right, @ctrlplusb , my bad :)
Fixed in: https://github.com/dotansimha/graphql-code-generator/pull/2169

@dotansimha additionally, if you have other, required variables without default value, the [X in Exclude<keyof T, K>]?: T[X] will mark it as optional when it shouldn't be.

For instance, given this schema:

type Query {
  organisations(accountId: ID!, limit: Int = 10, offset: Int = 0): OrganisationPagedResult
}

In the resolver, both accountId, limit and offset will have a value, but the generator only makes the arguments with default values required.

That is a breaking change for our project.

@jeroenvisser101 can you please explain? The accountId will be generated as required field any way.

Test it with:

      type Query {
        something(arg: String = "default_value", arg2: String!): String
      }

And the output was:


// from typescript plugin
     export type QuerySomethingArgs = {
        arg?: Maybe<Scalars['String']>,
        arg2: Scalars['String']
      };

// override of `arg` field in resolvers plugin
     export type QueryResolvers<ContextType = any, ParentType = ResolversParentTypes['Query']> = {
        something?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType, RequireFields<QuerySomethingArgs, 'arg'>>,
      };

Fixed in 1.5.0 ๐ŸŽ‰

I have nothing but unending love and gratitude to you for being such an amazing steward to this project.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

edorivai picture edorivai  ยท  3Comments

jackhkmatthews picture jackhkmatthews  ยท  3Comments

NickClark picture NickClark  ยท  3Comments

leebenson picture leebenson  ยท  3Comments

iamdanthedev picture iamdanthedev  ยท  3Comments