Skip to content

Commit bef4376

Browse files
GreenGremlinJonathan Felchlincharlypoly
authored
fix: RequireFields generic making all other fields optional (#7430)
* fix: RequireFields generic making all other fields optional * Fixing failing tests * Create tricky-chefs-shout.md * fix(resolvers-visitor): `RequireFields<>` should not be used to make args optionals * update changeset Co-authored-by: Jonathan Felchlin <jonathan@xgecko.com@outreach.io> Co-authored-by: Charly POLY <1252066+charlypoly@users.noreply.github.com> Co-authored-by: Charly POLY <cpoly55@gmail.com>
1 parent be7cb3a commit bef4376

File tree

9 files changed

+19
-27
lines changed

9 files changed

+19
-27
lines changed

‎.changeset/tricky-chefs-shout.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@graphql-codegen/typescript-resolvers": minor
3+
"@graphql-codegen/visitor-plugin-common": minor
4+
---
5+
6+
fix: RequireFields generic making all other fields optional

‎dev-test/modules/types.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K]
55
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };
66
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
77
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
8-
export type RequireFields<T, K extends keyof T> = { [X in Exclude<keyof T, K>]?: T[X] } & {
9-
[P in K]-?: NonNullable<T[P]>;
10-
};
8+
export type RequireFields<T, K extends keyof T> = Omit<T, K> & { [P in K]-?: NonNullable<T[P]> };
119
/** All built-in and custom scalars, mapped to their actual values */
1210
export type Scalars = {
1311
ID: string;
@@ -238,12 +236,7 @@ export type MutationResolvers<
238236
ContextType = any,
239237
ParentType extends ResolversParentTypes['Mutation'] = ResolversParentTypes['Mutation']
240238
> = {
241-
donate?: Resolver<
242-
Maybe<ResolversTypes['Donation']>,
243-
ParentType,
244-
ContextType,
245-
RequireFields<MutationDonateArgs, never>
246-
>;
239+
donate?: Resolver<Maybe<ResolversTypes['Donation']>, ParentType, ContextType, Partial<MutationDonateArgs>>;
247240
pong?: Resolver<Maybe<ResolversTypes['Int']>, ParentType, ContextType>;
248241
};
249242

‎dev-test/test-schema/resolvers-root.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ export type InputMaybe<T> = Maybe<T>;
44
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
55
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };
66
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
7-
export type RequireFields<T, K extends keyof T> = { [X in Exclude<keyof T, K>]?: T[X] } & {
8-
[P in K]-?: NonNullable<T[P]>;
9-
};
7+
export type RequireFields<T, K extends keyof T> = Omit<T, K> & { [P in K]-?: NonNullable<T[P]> };
108
/** All built-in and custom scalars, mapped to their actual values */
119
export type Scalars = {
1210
ID: string;

‎dev-test/test-schema/resolvers-types.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ export type InputMaybe<T> = Maybe<T>;
44
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
55
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };
66
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
7-
export type RequireFields<T, K extends keyof T> = { [X in Exclude<keyof T, K>]?: T[X] } & {
8-
[P in K]-?: NonNullable<T[P]>;
9-
};
7+
export type RequireFields<T, K extends keyof T> = Omit<T, K> & { [P in K]-?: NonNullable<T[P]> };
108
/** All built-in and custom scalars, mapped to their actual values */
119
export type Scalars = {
1210
ID: string;

‎dev-test/test-schema/typings.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ export type InputMaybe<T> = Maybe<T>;
44
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
55
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };
66
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
7-
export type RequireFields<T, K extends keyof T> = { [X in Exclude<keyof T, K>]?: T[X] } & {
8-
[P in K]-?: NonNullable<T[P]>;
9-
};
7+
export type RequireFields<T, K extends keyof T> = Omit<T, K> & { [P in K]-?: NonNullable<T[P]> };
108
/** All built-in and custom scalars, mapped to their actual values */
119
export type Scalars = {
1210
ID: string;

‎packages/plugins/other/visitor-plugin-common/src/base-resolvers-visitor.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,8 +1027,7 @@ export class BaseResolversVisitor<
10271027
}
10281028

10291029
protected applyOptionalFields(argsType: string, _fields: readonly InputValueDefinitionNode[]): string {
1030-
this._globalDeclarations.add(REQUIRE_FIELDS_TYPE);
1031-
return `RequireFields<${argsType}, never>`;
1030+
return `Partial<${argsType}>`;
10321031
}
10331032

10341033
ObjectTypeDefinition(node: ObjectTypeDefinitionNode): string {

‎packages/plugins/other/visitor-plugin-common/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ export function stripMapperTypeInterpolation(identifier: string): string {
356356
}
357357

358358
export const OMIT_TYPE = 'export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;';
359-
export const REQUIRE_FIELDS_TYPE = `export type RequireFields<T, K extends keyof T> = { [X in Exclude<keyof T, K>]?: T[X] } & { [P in K]-?: NonNullable<T[P]> };`;
359+
export const REQUIRE_FIELDS_TYPE = `export type RequireFields<T, K extends keyof T> = Omit<T, K> & { [P in K]-?: NonNullable<T[P]> };`;
360360

361361
/**
362362
* merge selection sets into a new selection set without mutating the inputs.

‎packages/plugins/typescript/resolvers/tests/__snapshots__/ts-resolvers.spec.ts.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?:
88
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
99
import { GraphQLResolveInfo, GraphQLScalarType, GraphQLScalarTypeConfig } from 'graphql';
1010
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
11-
export type RequireFields<T, K extends keyof T> = { [X in Exclude<keyof T, K>]?: T[X] } & { [P in K]-?: NonNullable<T[P]> };
11+
export type RequireFields<T, K extends keyof T> = Omit<T, K> & { [P in K]-?: NonNullable<T[P]> };
1212
/** All built-in and custom scalars, mapped to their actual values */
1313
export type Scalars = {
1414
ID: string;
@@ -262,7 +262,7 @@ export type DirectiveResolvers<ContextType = any> = ResolversObject<{
262262
exports[`TypeScript Resolvers Plugin Config namespacedImportName - should work correctly with imported namespaced type 1`] = `
263263
"import { GraphQLResolveInfo, GraphQLScalarType, GraphQLScalarTypeConfig } from 'graphql';
264264
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
265-
export type RequireFields<T, K extends keyof T> = { [X in Exclude<keyof T, K>]?: T[X] } & { [P in K]-?: NonNullable<T[P]> };
265+
export type RequireFields<T, K extends keyof T> = Omit<T, K> & { [P in K]-?: NonNullable<T[P]> };
266266
export type WithIndex<TObject> = TObject & Record<string, any>;
267267
export type ResolversObject<TObject> = WithIndex<TObject>;
268268
@@ -456,7 +456,7 @@ export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?:
456456
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
457457
import { GraphQLResolveInfo, GraphQLScalarType, GraphQLScalarTypeConfig } from 'graphql';
458458
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
459-
export type RequireFields<T, K extends keyof T> = { [X in Exclude<keyof T, K>]?: T[X] } & { [P in K]-?: NonNullable<T[P]> };
459+
export type RequireFields<T, K extends keyof T> = Omit<T, K> & { [P in K]-?: NonNullable<T[P]> };
460460
/** All built-in and custom scalars, mapped to their actual values */
461461
export type Scalars = {
462462
ID: string;

‎packages/plugins/typescript/resolvers/tests/ts-resolvers.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,7 +1284,7 @@ export type ResolverFn<TResult, TParent, TContext, TArgs> = (
12841284
const o = await validate(result, config, testSchema);
12851285

12861286
expect(o).toContain(
1287-
`f?: Resolver<Maybe<TResolversTypes['String']>, ParentType, ContextType, RequireFields<TMyTypeFArgs, never>>;`
1287+
`f?: Resolver<Maybe<TResolversTypes['String']>, ParentType, ContextType, Partial<TMyTypeFArgs>>;`
12881288
);
12891289
});
12901290

@@ -1294,7 +1294,7 @@ export type ResolverFn<TResult, TParent, TContext, TArgs> = (
12941294
const result = await plugin(testSchema, [], {}, { outputFile: '' });
12951295

12961296
expect(result.content).toBeSimilarStringTo(
1297-
`f?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType, RequireFields<MyTypeFArgs, never>>;`
1297+
`f?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType, Partial<MyTypeFArgs>>;`
12981298
);
12991299
await validate(result, {}, testSchema);
13001300
});
@@ -2085,7 +2085,7 @@ export type ResolverFn<TResult, TParent, TContext, TArgs> = (
20852085
);
20862086
expect(o).toContain(`me?: Resolver<Maybe<ResolversTypesQL['User']>, ParentType, ContextType>;`);
20872087
expect(o).toContain(
2088-
`user2?: Resolver<Maybe<ResolversTypesQL['User']>, ParentType, ContextType, RequireFields<QueryUser2ArgsQL, never>>;`
2088+
`user2?: Resolver<Maybe<ResolversTypesQL['User']>, ParentType, ContextType, Partial<QueryUser2ArgsQL>>;`
20892089
);
20902090
});
20912091
it('should work correctly with enumPrefix: false - issue #2679', async () => {

0 commit comments

Comments
 (0)