Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugin/typescript-resolvers): Add allowSemanticNonNull option #10159

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,10 @@ export class BaseResolversVisitor<
return `ParentType extends ${parentType} = ${parentType}`;
}

protected tranceformSemanticNonNull(type: string): string {
return type;
}

FieldDefinition(node: FieldDefinitionNode, key: string | number, parent: any): FieldDefinitionPrintFn {
const hasArguments = node.arguments && node.arguments.length > 0;
const declarationKind = 'type';
Expand Down Expand Up @@ -1457,7 +1461,13 @@ export class BaseResolversVisitor<
parentType,
parentTypeSignature: this.getParentTypeForSignature(node),
});
const mappedTypeKey = isSubscriptionType ? `${mappedType}, "${node.name}"` : mappedType;

const isSemanticNonNull = node.directives.find(directive => (directive.name as any) === 'semanticNonNull');
const mappedTypeKey = isSubscriptionType
? `${mappedType}, "${node.name}"`
: isSemanticNonNull
? this.tranceformSemanticNonNull(mappedType)
: mappedType;

const directiveMappings =
node.directives
Expand Down
22 changes: 22 additions & 0 deletions packages/plugins/typescript/resolvers/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,26 @@ export interface TypeScriptResolversPluginConfig extends RawResolversConfig {
* ```
*/
makeResolverTypeCallable?: boolean;
/**
* @description If set to `true`, field resolvers annotated by `@semanticNonNull` directive get prohibited to return nullish value
*
* @exampleMarkdown
* ```ts filename="codegen.ts"
* import type { CodegenConfig } from '@graphql-codegen/cli';
*
* const config: CodegenConfig = {
* // ...
* generates: {
* 'path/to/file.ts': {
* plugins: ['typescript', 'typescript-resolvers'],
* config: {
* allowSemanticNonNull: true
* },
* },
* },
* };
* export default config;
* ```
*/
allowSemanticNonNull?: boolean;
}
9 changes: 9 additions & 0 deletions packages/plugins/typescript/resolvers/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface ParsedTypeScriptResolversConfig extends ParsedResolversConfig {
wrapFieldDefinitions: boolean;
allowParentTypeOverride: boolean;
optionalInfoArgument: boolean;
allowSemanticNonNull: boolean;
}

export class TypeScriptResolversVisitor extends BaseResolversVisitor<
Expand All @@ -40,6 +41,7 @@ export class TypeScriptResolversVisitor extends BaseResolversVisitor<
wrapFieldDefinitions: getConfigValue(pluginConfig.wrapFieldDefinitions, false),
allowParentTypeOverride: getConfigValue(pluginConfig.allowParentTypeOverride, false),
optionalInfoArgument: getConfigValue(pluginConfig.optionalInfoArgument, false),
allowSemanticNonNull: getConfigValue(pluginConfig.allowSemanticNonNull, false),
} as ParsedTypeScriptResolversConfig,
schema
);
Expand Down Expand Up @@ -75,6 +77,13 @@ export class TypeScriptResolversVisitor extends BaseResolversVisitor<
return `ParentType extends ${parentType} = ${parentType}`;
}

protected tranceformSemanticNonNull(type: string): string {
if (this.config.allowSemanticNonNull) {
return this.clearOptional(super.tranceformSemanticNonNull(type));
}
return super.tranceformSemanticNonNull(type);
}

protected formatRootResolver(schemaTypeName: string, resolverType: string, declarationKind: DeclarationKind): string {
const avoidOptionals = this.config.avoidOptionals.resolvers;
return `${schemaTypeName}${avoidOptionals ? '' : '?'}: ${resolverType}${this.getPunctuation(declarationKind)}`;
Expand Down
24 changes: 24 additions & 0 deletions packages/plugins/typescript/resolvers/tests/ts-resolvers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,30 @@ export type MyTypeResolvers<ContextType = any, ParentType extends ResolversParen

await resolversTestingValidate(result);
});

it('allowSemanticNonNull - should build strict type if annotated by @semanticNonNull directive', async () => {
const testingSchema = buildSchema(/* GraphQL */ `
directive @semanticNonNull(levels: [Int] = [0]) on FIELD_DEFINITION
type TestingType {
nullableField: String
nullableList: [String]
semanticNonNullField: String @semanticNonNull
semanticNonNullList: [String] @semanticNonNull
}
`);

const result = await plugin(testingSchema, [], { allowSemanticNonNull: true }, { outputFile: '' });

expect(result.content).toBeSimilarStringTo(`
export type TestingTypeResolvers<ContextType = any, ParentType extends ResolversParentTypes['TestingType'] = ResolversParentTypes['TestingType']> = {
nullableField?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
nullableList?: Resolver<Maybe<Array<Maybe<ResolversTypes['String']>>>, ParentType, ContextType>;
semanticNonNullField?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
semanticNonNullList?: Resolver<Array<Maybe<ResolversTypes['String']>>, ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};
`);
});
});

it('directiveResolverMappings - should generate correct types (import definition)', async () => {
Expand Down
Loading