Skip to content

Commit

Permalink
Avoid wrapping args with partial when avoiding optionals
Browse files Browse the repository at this point in the history
  • Loading branch information
maclockard committed Sep 14, 2023
1 parent 9df3644 commit f4447e3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
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 f4447e3

Please sign in to comment.