Skip to content

Commit 7718a81

Browse files
authored
Avoid wrapping args with partial when avoiding optionals (#9673)
* Avoid wrapping args with partial when avoiding optionals * changeset
1 parent eaa3d60 commit 7718a81

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

.changeset/late-olives-pull.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@graphql-codegen/visitor-plugin-common': patch
3+
'@graphql-codegen/typescript-resolvers': patch
4+
---
5+
6+
Respect avoidOptionals when all arguments are optional

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,14 +1401,19 @@ export class BaseResolversVisitor<
14011401
)
14021402
: null;
14031403

1404+
const avoidInputsOptionals =
1405+
typeof this.config.avoidOptionals === 'object'
1406+
? this.config.avoidOptionals?.inputValue
1407+
: this.config.avoidOptionals === true;
1408+
14041409
if (argsType !== null) {
14051410
const argsToForceRequire = original.arguments.filter(
14061411
arg => !!arg.defaultValue || arg.type.kind === 'NonNullType'
14071412
);
14081413

14091414
if (argsToForceRequire.length > 0) {
14101415
argsType = this.applyRequireFields(argsType, argsToForceRequire);
1411-
} else if (original.arguments.length > 0) {
1416+
} else if (original.arguments.length > 0 && avoidInputsOptionals !== true) {
14121417
argsType = this.applyOptionalFields(argsType, original.arguments);
14131418
}
14141419
}
@@ -1428,7 +1433,7 @@ export class BaseResolversVisitor<
14281433

14291434
const resolverType = isSubscriptionType ? 'SubscriptionResolver' : directiveMappings[0] ?? 'Resolver';
14301435

1431-
const avoidOptionals =
1436+
const avoidResolverOptionals =
14321437
typeof this.config.avoidOptionals === 'object'
14331438
? this.config.avoidOptionals?.resolvers
14341439
: this.config.avoidOptionals === true;
@@ -1439,7 +1444,7 @@ export class BaseResolversVisitor<
14391444
genericTypes: string[];
14401445
} = {
14411446
name: node.name as any,
1442-
modifier: avoidOptionals ? '' : '?',
1447+
modifier: avoidResolverOptionals ? '' : '?',
14431448
type: resolverType,
14441449
genericTypes: [mappedTypeKey, parentTypeSignature, contextType, argsType].filter(f => f),
14451450
};

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3002,4 +3002,41 @@ export type ResolverFn<TResult, TParent, TContext, TArgs> = (
30023002
};
30033003
`);
30043004
});
3005+
3006+
it('#9438 - avoidOptionals should not wrap arguments with partial', async () => {
3007+
const testSchema = buildSchema(/* GraphQL */ `
3008+
type Query {
3009+
users(filter: UserFilterInput): [User!]!
3010+
}
3011+
3012+
input UserFilterInput {
3013+
status: String = "ACTIVE"
3014+
}
3015+
3016+
type User {
3017+
id: ID!
3018+
}
3019+
`);
3020+
3021+
const output = (await plugin(
3022+
testSchema,
3023+
[],
3024+
{
3025+
avoidOptionals: {
3026+
defaultValue: true,
3027+
field: true,
3028+
inputValue: true,
3029+
object: true,
3030+
resolvers: false,
3031+
},
3032+
} as any,
3033+
{ outputFile: 'graphql.ts' }
3034+
)) as Types.ComplexPluginOutput;
3035+
3036+
expect(output.content).toBeSimilarStringTo(`
3037+
export type QueryResolvers<ContextType = any, ParentType extends ResolversParentTypes['Query'] = ResolversParentTypes['Query']> = {
3038+
users?: Resolver<Array<ResolversTypes['User']>, ParentType, ContextType, QueryUsersArgs>;
3039+
};
3040+
`);
3041+
});
30053042
});

0 commit comments

Comments
 (0)