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
type Query {
organisations(limit: Int = 10, offset: Int = 0): OrganisationPagedResult
}
codegen.yml config file: plugins:
- add: '/* eslint-disable */'
- typescript
- typescript-resolvers
// 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:
@graphql-codegen/typescript: 1.4.0 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.
Most helpful comment
I have nothing but unending love and gratitude to you for being such an amazing steward to this project.