Skip to content

Commit

Permalink
fix: RequireFields generic making all other fields optional (#7430)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
4 people committed Feb 2, 2022
1 parent be7cb3a commit bef4376
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 27 deletions.
6 changes: 6 additions & 0 deletions .changeset/tricky-chefs-shout.md
@@ -0,0 +1,6 @@
---
"@graphql-codegen/typescript-resolvers": minor
"@graphql-codegen/visitor-plugin-common": minor
---

fix: RequireFields generic making all other fields optional
11 changes: 2 additions & 9 deletions dev-test/modules/types.ts
Expand Up @@ -5,9 +5,7 @@ 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 Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
export type RequireFields<T, K extends keyof T> = { [X in Exclude<keyof T, K>]?: T[X] } & {
[P in K]-?: NonNullable<T[P]>;
};
export type RequireFields<T, K extends keyof T> = Omit<T, K> & { [P in K]-?: NonNullable<T[P]> };
/** All built-in and custom scalars, mapped to their actual values */
export type Scalars = {
ID: string;
Expand Down Expand Up @@ -238,12 +236,7 @@ export type MutationResolvers<
ContextType = any,
ParentType extends ResolversParentTypes['Mutation'] = ResolversParentTypes['Mutation']
> = {
donate?: Resolver<
Maybe<ResolversTypes['Donation']>,
ParentType,
ContextType,
RequireFields<MutationDonateArgs, never>
>;
donate?: Resolver<Maybe<ResolversTypes['Donation']>, ParentType, ContextType, Partial<MutationDonateArgs>>;
pong?: Resolver<Maybe<ResolversTypes['Int']>, ParentType, ContextType>;
};

Expand Down
4 changes: 1 addition & 3 deletions dev-test/test-schema/resolvers-root.ts
Expand Up @@ -4,9 +4,7 @@ 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 RequireFields<T, K extends keyof T> = { [X in Exclude<keyof T, K>]?: T[X] } & {
[P in K]-?: NonNullable<T[P]>;
};
export type RequireFields<T, K extends keyof T> = Omit<T, K> & { [P in K]-?: NonNullable<T[P]> };
/** All built-in and custom scalars, mapped to their actual values */
export type Scalars = {
ID: string;
Expand Down
4 changes: 1 addition & 3 deletions dev-test/test-schema/resolvers-types.ts
Expand Up @@ -4,9 +4,7 @@ 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 RequireFields<T, K extends keyof T> = { [X in Exclude<keyof T, K>]?: T[X] } & {
[P in K]-?: NonNullable<T[P]>;
};
export type RequireFields<T, K extends keyof T> = Omit<T, K> & { [P in K]-?: NonNullable<T[P]> };
/** All built-in and custom scalars, mapped to their actual values */
export type Scalars = {
ID: string;
Expand Down
4 changes: 1 addition & 3 deletions dev-test/test-schema/typings.ts
Expand Up @@ -4,9 +4,7 @@ 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 RequireFields<T, K extends keyof T> = { [X in Exclude<keyof T, K>]?: T[X] } & {
[P in K]-?: NonNullable<T[P]>;
};
export type RequireFields<T, K extends keyof T> = Omit<T, K> & { [P in K]-?: NonNullable<T[P]> };
/** All built-in and custom scalars, mapped to their actual values */
export type Scalars = {
ID: string;
Expand Down
Expand Up @@ -1027,8 +1027,7 @@ export class BaseResolversVisitor<
}

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

ObjectTypeDefinition(node: ObjectTypeDefinitionNode): string {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/other/visitor-plugin-common/src/utils.ts
Expand Up @@ -356,7 +356,7 @@ export function stripMapperTypeInterpolation(identifier: string): string {
}

export const OMIT_TYPE = 'export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;';
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]> };`;
export const REQUIRE_FIELDS_TYPE = `export type RequireFields<T, K extends keyof T> = Omit<T, K> & { [P in K]-?: NonNullable<T[P]> };`;

/**
* merge selection sets into a new selection set without mutating the inputs.
Expand Down
Expand Up @@ -8,7 +8,7 @@ export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?:
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
import { GraphQLResolveInfo, GraphQLScalarType, GraphQLScalarTypeConfig } from 'graphql';
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
export type RequireFields<T, K extends keyof T> = { [X in Exclude<keyof T, K>]?: T[X] } & { [P in K]-?: NonNullable<T[P]> };
export type RequireFields<T, K extends keyof T> = Omit<T, K> & { [P in K]-?: NonNullable<T[P]> };
/** All built-in and custom scalars, mapped to their actual values */
export type Scalars = {
ID: string;
Expand Down Expand Up @@ -262,7 +262,7 @@ export type DirectiveResolvers<ContextType = any> = ResolversObject<{
exports[`TypeScript Resolvers Plugin Config namespacedImportName - should work correctly with imported namespaced type 1`] = `
"import { GraphQLResolveInfo, GraphQLScalarType, GraphQLScalarTypeConfig } from 'graphql';
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
export type RequireFields<T, K extends keyof T> = { [X in Exclude<keyof T, K>]?: T[X] } & { [P in K]-?: NonNullable<T[P]> };
export type RequireFields<T, K extends keyof T> = Omit<T, K> & { [P in K]-?: NonNullable<T[P]> };
export type WithIndex<TObject> = TObject & Record<string, any>;
export type ResolversObject<TObject> = WithIndex<TObject>;
Expand Down Expand Up @@ -456,7 +456,7 @@ export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?:
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
import { GraphQLResolveInfo, GraphQLScalarType, GraphQLScalarTypeConfig } from 'graphql';
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
export type RequireFields<T, K extends keyof T> = { [X in Exclude<keyof T, K>]?: T[X] } & { [P in K]-?: NonNullable<T[P]> };
export type RequireFields<T, K extends keyof T> = Omit<T, K> & { [P in K]-?: NonNullable<T[P]> };
/** All built-in and custom scalars, mapped to their actual values */
export type Scalars = {
ID: string;
Expand Down
Expand Up @@ -1284,7 +1284,7 @@ export type ResolverFn<TResult, TParent, TContext, TArgs> = (
const o = await validate(result, config, testSchema);

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

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

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

1 comment on commit bef4376

@vercel
Copy link

@vercel vercel bot commented on bef4376 Feb 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.