Skip to content

Commit

Permalink
Avoid wrapping args with partial when avoiding optionals (#9673)
Browse files Browse the repository at this point in the history
* Avoid wrapping args with partial when avoiding optionals

* changeset
  • Loading branch information
maclockard committed Jan 17, 2024
1 parent eaa3d60 commit 7718a81
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/late-olives-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-codegen/visitor-plugin-common': patch
'@graphql-codegen/typescript-resolvers': patch
---

Respect avoidOptionals when all arguments are optional
Original file line number Diff line number Diff line change
Expand Up @@ -1401,14 +1401,19 @@ export class BaseResolversVisitor<
)
: null;

const avoidInputsOptionals =
typeof this.config.avoidOptionals === 'object'
? this.config.avoidOptionals?.inputValue
: this.config.avoidOptionals === true;

if (argsType !== null) {
const argsToForceRequire = original.arguments.filter(
arg => !!arg.defaultValue || arg.type.kind === 'NonNullType'
);

if (argsToForceRequire.length > 0) {
argsType = this.applyRequireFields(argsType, argsToForceRequire);
} else if (original.arguments.length > 0) {
} else if (original.arguments.length > 0 && avoidInputsOptionals !== true) {
argsType = this.applyOptionalFields(argsType, original.arguments);
}
}
Expand All @@ -1428,7 +1433,7 @@ export class BaseResolversVisitor<

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

const avoidOptionals =
const avoidResolverOptionals =
typeof this.config.avoidOptionals === 'object'
? this.config.avoidOptionals?.resolvers
: this.config.avoidOptionals === true;
Expand All @@ -1439,7 +1444,7 @@ export class BaseResolversVisitor<
genericTypes: string[];
} = {
name: node.name as any,
modifier: avoidOptionals ? '' : '?',
modifier: avoidResolverOptionals ? '' : '?',
type: resolverType,
genericTypes: [mappedTypeKey, parentTypeSignature, contextType, argsType].filter(f => f),
};
Expand Down
37 changes: 37 additions & 0 deletions packages/plugins/typescript/resolvers/tests/ts-resolvers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3002,4 +3002,41 @@ export type ResolverFn<TResult, TParent, TContext, TArgs> = (
};
`);
});

it('#9438 - avoidOptionals should not wrap arguments with partial', async () => {
const testSchema = buildSchema(/* GraphQL */ `
type Query {
users(filter: UserFilterInput): [User!]!
}
input UserFilterInput {
status: String = "ACTIVE"
}
type User {
id: ID!
}
`);

const output = (await plugin(
testSchema,
[],
{
avoidOptionals: {
defaultValue: true,
field: true,
inputValue: true,
object: true,
resolvers: false,
},
} as any,
{ outputFile: 'graphql.ts' }
)) as Types.ComplexPluginOutput;

expect(output.content).toBeSimilarStringTo(`
export type QueryResolvers<ContextType = any, ParentType extends ResolversParentTypes['Query'] = ResolversParentTypes['Query']> = {
users?: Resolver<Array<ResolversTypes['User']>, ParentType, ContextType, QueryUsersArgs>;
};
`);
});
});

0 comments on commit 7718a81

Please sign in to comment.