From b7b1b67d7aac263fd73f4fff4a7bc5005fcdce4d Mon Sep 17 00:00:00 2001 From: Eddy Nguyen Date: Thu, 13 Jun 2024 21:21:33 +1000 Subject: [PATCH 1/7] Use meta from generated types for resolver type names --- .../addVirtualTypesFileToTsMorphProject.ts | 13 ++++++++-- .../src/generateResolverFiles/types.ts | 8 +++++++ .../generateResolverFiles/visitNamedType.ts | 4 ++-- .../parseGraphQLSchema/parseGraphQLSchema.ts | 21 +++++++++++----- .../typescript-resolver-files/src/preset.ts | 24 ++++++++++--------- 5 files changed, 49 insertions(+), 21 deletions(-) diff --git a/packages/typescript-resolver-files/src/addVirtualTypesFileToTsMorphProject/addVirtualTypesFileToTsMorphProject.ts b/packages/typescript-resolver-files/src/addVirtualTypesFileToTsMorphProject/addVirtualTypesFileToTsMorphProject.ts index 7ffd66d5..9f036566 100644 --- a/packages/typescript-resolver-files/src/addVirtualTypesFileToTsMorphProject/addVirtualTypesFileToTsMorphProject.ts +++ b/packages/typescript-resolver-files/src/addVirtualTypesFileToTsMorphProject/addVirtualTypesFileToTsMorphProject.ts @@ -6,6 +6,7 @@ import * as typeScriptResolversPlugin from '@graphql-codegen/typescript-resolver import type { GraphQLSchema } from 'graphql'; import type { SourceFile, Project } from 'ts-morph'; import type { ServerConfig } from '@eddeee888/gcg-server-config'; +import type { GeneratedTypesFileMeta } from '../generateResolverFiles'; export const addVirtualTypesFileToTsMorphProject = async ({ tsMorphProject, @@ -19,7 +20,10 @@ export const addVirtualTypesFileToTsMorphProject = async ({ resolverTypesPath: string; resolverTypesConfig: ServerConfig; addConfig?: AddPluginConfig; -}): Promise => { +}): Promise<{ + typesSourceFile: SourceFile; + meta: GeneratedTypesFileMeta; +}> => { const typesFile = await generateVirtualTypesFile({ schemaAst, resolverTypesPath, @@ -33,7 +37,7 @@ export const addVirtualTypesFileToTsMorphProject = async ({ { overwrite: true } ); - return typesSourceFile; + return { typesSourceFile, meta: typesFile.meta }; }; /** @@ -53,6 +57,7 @@ const generateVirtualTypesFile = async ({ }): Promise<{ filePath: string; content: string; + meta: GeneratedTypesFileMeta; }> => { const [typescriptResult, typescriptResolversResult, addResult] = await Promise.all([ @@ -75,6 +80,10 @@ const generateVirtualTypesFile = async ({ ${addResultAsComplextOutput.content} ${addResultAsComplextOutput.append?.join('\n')} `, + meta: { + generatedResolverTypes: + typescriptResolversResult.meta?.generatedResolverTypes || {}, + }, }; }; diff --git a/packages/typescript-resolver-files/src/generateResolverFiles/types.ts b/packages/typescript-resolver-files/src/generateResolverFiles/types.ts index 70ddcd7d..32ca961c 100644 --- a/packages/typescript-resolver-files/src/generateResolverFiles/types.ts +++ b/packages/typescript-resolver-files/src/generateResolverFiles/types.ts @@ -1,4 +1,5 @@ import type { SourceFile, Project } from 'ts-morph'; +import type * as typeScriptResolversPlugin from '@graphql-codegen/typescript-resolvers'; import type { GraphQLObjectTypeResolversToGenerate } from '../getGraphQLObjectTypeResolversToGenerate'; import type { TypeMappersMap } from '../parseTypeMappers'; import type { ImportLineMeta, RootObjectType } from '../utils'; @@ -6,6 +7,12 @@ import type { ParsedPresetConfig } from '../validatePresetConfig'; import type { NormalizedResolverName } from '../parseGraphQLSchema'; import type { ParsedGraphQLSchemaMeta } from '../parseGraphQLSchema'; +export type GeneratedTypesFileMeta = { + generatedResolverTypes: NonNullable< + Awaited>['meta'] + >['generatedResolverTypes']; +}; + interface BaseVirtualFile { __filetype: string; content: string; @@ -136,6 +143,7 @@ export interface GenerateResolverFilesContext { graphQLObjectTypeResolversToGenerate: GraphQLObjectTypeResolversToGenerate; fixObjectTypeResolvers: ParsedPresetConfig['fixObjectTypeResolvers']; emitLegacyCommonJSImports: boolean; + generatedTypesFileMeta: GeneratedTypesFileMeta; }; result: { files: Record; diff --git a/packages/typescript-resolver-files/src/generateResolverFiles/visitNamedType.ts b/packages/typescript-resolver-files/src/generateResolverFiles/visitNamedType.ts index 6373866c..86a24f2c 100644 --- a/packages/typescript-resolver-files/src/generateResolverFiles/visitNamedType.ts +++ b/packages/typescript-resolver-files/src/generateResolverFiles/visitNamedType.ts @@ -56,8 +56,8 @@ export const visitNamedType = ( resolversTypeMeta: { module: relativePathToResolverTypesFile, moduleType: 'file', - typeNamedImport, - typeString, + typeNamedImport: typeNamedImport(ctx.config.generatedTypesFileMeta), + typeString: typeString(ctx.config.generatedTypesFileMeta), }, }, ctx diff --git a/packages/typescript-resolver-files/src/parseGraphQLSchema/parseGraphQLSchema.ts b/packages/typescript-resolver-files/src/parseGraphQLSchema/parseGraphQLSchema.ts index 82bd2e52..6f2ba8f9 100644 --- a/packages/typescript-resolver-files/src/parseGraphQLSchema/parseGraphQLSchema.ts +++ b/packages/typescript-resolver-files/src/parseGraphQLSchema/parseGraphQLSchema.ts @@ -28,6 +28,7 @@ import { } from '../utils'; import { parseLocationForOutputDir } from './parseLocationForOutputDir'; import { normalizeResolverName } from './normalizeResolverName'; +import type { GeneratedTypesFileMeta } from '../generateResolverFiles'; interface ParseGraphQLSchemaParams { schemaAst: GraphQLSchema; @@ -54,8 +55,8 @@ export interface ResolverDetails { }; relativePathFromBaseToModule: string[]; normalizedResolverName: ReturnType; - typeNamedImport: string; - typeString: string; + typeNamedImport: (generatedTypesFileMeta: GeneratedTypesFileMeta) => string; + typeString: (generatedTypesFileMeta: GeneratedTypesFileMeta) => string; relativePathToResolverTypesFile: string; } @@ -517,10 +518,18 @@ const createResolverDetails = ({ }, relativePathFromBaseToModule, normalizedResolverName, - typeNamedImport: `${schemaType}Resolvers`, // TODO: use from `typescript-resolvers`'s `meta` - typeString: belongsToRootObject - ? `${schemaType}Resolvers['${resolverName}']` - : `${schemaType}Resolvers`, // TODO: use from `typescript-resolvers`'s `meta` + typeNamedImport: ({ generatedResolverTypes }) => + generatedResolverTypes[schemaType]?.name || `${schemaType}Resolvers`, + typeString: ({ generatedResolverTypes }) => { + if (belongsToRootObject) { + return generatedResolverTypes[schemaType] + ? `${generatedResolverTypes[schemaType].name}['${resolverName}']` + : `${schemaType}Resolvers['${resolverName}']`; + } + return ( + generatedResolverTypes[schemaType]?.name || `${schemaType}Resolvers` + ); + }, relativePathToResolverTypesFile: relativeModulePath( resolversOutputDir, resolverTypesPath diff --git a/packages/typescript-resolver-files/src/preset.ts b/packages/typescript-resolver-files/src/preset.ts index 58cdc99e..822f1fae 100644 --- a/packages/typescript-resolver-files/src/preset.ts +++ b/packages/typescript-resolver-files/src/preset.ts @@ -144,17 +144,18 @@ export const preset: Types.OutputPreset = { // typesSourceFile is the virtual `types.generated.ts` // This is useful when we need to do static analysis as most types come from this file // e.g. comparing mappers field type vs schema object field type - const typesSourceFile = await profiler.run( - () => - addVirtualTypesFileToTsMorphProject({ - tsMorphProject, - schemaAst, - resolverTypesConfig, - resolverTypesPath, - addConfig: normalizedAdd?.[resolverTypesPath], - }), - createProfilerRunName('addVirtualTypesFileToTsMorphProject') - ); + const { typesSourceFile, meta: generatedTypesFileMeta } = + await profiler.run( + () => + addVirtualTypesFileToTsMorphProject({ + tsMorphProject, + schemaAst, + resolverTypesConfig, + resolverTypesPath, + addConfig: normalizedAdd?.[resolverTypesPath], + }), + createProfilerRunName('addVirtualTypesFileToTsMorphProject') + ); const graphQLObjectTypeResolversToGenerate = await profiler.run( async () => @@ -244,6 +245,7 @@ export const preset: Types.OutputPreset = { ...mergedConfig.externalResolvers, }, emitLegacyCommonJSImports, + generatedTypesFileMeta, }, result, }), From 0b9e2d4ee82f35eb5a57c4814c218faf9e2a2981 Mon Sep 17 00:00:00 2001 From: Eddy Nguyen Date: Thu, 13 Jun 2024 21:24:59 +1000 Subject: [PATCH 2/7] Update e2e tests --- .../modules/base/rslvrs/PaginationResult.ts | 4 ++-- .../modules/base/rslvrs/StandardError.ts | 4 ++-- .../modules/topic/rslvrs/Mutation/topicCreate.ts | 4 ++-- .../modules/topic/rslvrs/Mutation/topicEdit.ts | 4 ++-- .../modules/topic/rslvrs/Query/topicById.ts | 4 ++-- .../modules/topic/rslvrs/Query/topicsCreatedByUser.ts | 4 ++-- .../src/test-config-overrides/modules/topic/rslvrs/Topic.ts | 4 ++-- .../modules/topic/rslvrs/TopicByIdResult.ts | 4 ++-- .../modules/topic/rslvrs/TopicCreateResult.ts | 4 ++-- .../modules/topic/rslvrs/TopicEditResult.ts | 4 ++-- .../modules/topic/rslvrs/TopicsCreatedByUserResult.ts | 4 ++-- .../src/test-config-overrides/modules/user/rslvrs/Profile.ts | 4 ++-- .../src/test-config-overrides/modules/user/rslvrs/Query/me.ts | 4 ++-- .../modules/user/rslvrs/Query/userByAccountName.ts | 4 ++-- .../modules/user/rslvrs/Subscription/profileChanges.ts | 4 ++-- .../src/test-config-overrides/modules/user/rslvrs/User.ts | 4 ++-- .../test-config-overrides/modules/user/rslvrs/UserResult.ts | 4 ++-- 17 files changed, 34 insertions(+), 34 deletions(-) diff --git a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/base/rslvrs/PaginationResult.ts b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/base/rslvrs/PaginationResult.ts index 6dc5602e..45a1f81d 100644 --- a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/base/rslvrs/PaginationResult.ts +++ b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/base/rslvrs/PaginationResult.ts @@ -1,4 +1,4 @@ -import type { PaginationResultResolvers } from './../../types.gen'; -export const PaginationResult: PaginationResultResolvers = { +import type { PAGINATIONRESULTRESOLVERS } from './../../types.gen'; +export const PaginationResult: PAGINATIONRESULTRESOLVERS = { /* Implement PaginationResult resolver logic here */ }; diff --git a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/base/rslvrs/StandardError.ts b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/base/rslvrs/StandardError.ts index 24bc44a4..2b6a7e65 100644 --- a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/base/rslvrs/StandardError.ts +++ b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/base/rslvrs/StandardError.ts @@ -1,4 +1,4 @@ -import type { StandardErrorResolvers } from './../../types.gen'; -export const StandardError: StandardErrorResolvers = { +import type { STANDARDERRORRESOLVERS } from './../../types.gen'; +export const StandardError: STANDARDERRORRESOLVERS = { /* Implement StandardError resolver logic here */ }; diff --git a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/Mutation/topicCreate.ts b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/Mutation/topicCreate.ts index 2751cfb4..4504159b 100644 --- a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/Mutation/topicCreate.ts +++ b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/Mutation/topicCreate.ts @@ -1,6 +1,6 @@ -import type { MutationResolvers } from './../../../types.gen'; +import type { MUTATIONRESOLVERS } from './../../../types.gen'; export const topicCreate: NonNullable< - MutationResolvers['topicCreate'] + MUTATIONRESOLVERS['topicCreate'] > = async (_parent, _arg, _ctx) => { /* Implement Mutation.topicCreate resolver logic here */ }; diff --git a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/Mutation/topicEdit.ts b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/Mutation/topicEdit.ts index 72f499b9..cfcabba4 100644 --- a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/Mutation/topicEdit.ts +++ b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/Mutation/topicEdit.ts @@ -1,5 +1,5 @@ -import type { MutationResolvers } from './../../../types.gen'; -export const topicEdit: NonNullable = async ( +import type { MUTATIONRESOLVERS } from './../../../types.gen'; +export const topicEdit: NonNullable = async ( _parent, _arg, _ctx diff --git a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/Query/topicById.ts b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/Query/topicById.ts index a45858c2..d713deba 100644 --- a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/Query/topicById.ts +++ b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/Query/topicById.ts @@ -1,5 +1,5 @@ -import type { QueryResolvers } from './../../../types.gen'; -export const topicById: NonNullable = async ( +import type { QUERYRESOLVERS } from './../../../types.gen'; +export const topicById: NonNullable = async ( _parent, _arg, _ctx diff --git a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/Query/topicsCreatedByUser.ts b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/Query/topicsCreatedByUser.ts index 2631a319..f61c563d 100644 --- a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/Query/topicsCreatedByUser.ts +++ b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/Query/topicsCreatedByUser.ts @@ -1,6 +1,6 @@ -import type { QueryResolvers } from './../../../types.gen'; +import type { QUERYRESOLVERS } from './../../../types.gen'; export const topicsCreatedByUser: NonNullable< - QueryResolvers['topicsCreatedByUser'] + QUERYRESOLVERS['topicsCreatedByUser'] > = async (_parent, _arg, _ctx) => { /* Implement Query.topicsCreatedByUser resolver logic here */ }; diff --git a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/Topic.ts b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/Topic.ts index 40d2c27e..502e4ee6 100644 --- a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/Topic.ts +++ b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/Topic.ts @@ -1,4 +1,4 @@ -import type { TopicResolvers } from './../../types.gen'; -export const Topic: TopicResolvers = { +import type { TOPICRESOLVERS } from './../../types.gen'; +export const Topic: TOPICRESOLVERS = { /* Implement Topic resolver logic here */ }; diff --git a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicByIdResult.ts b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicByIdResult.ts index b55305be..349214c7 100644 --- a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicByIdResult.ts +++ b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicByIdResult.ts @@ -1,4 +1,4 @@ -import type { TopicByIdResultResolvers } from './../../types.gen'; -export const TopicByIdResult: TopicByIdResultResolvers = { +import type { TOPICBYIDRESULTRESOLVERS } from './../../types.gen'; +export const TopicByIdResult: TOPICBYIDRESULTRESOLVERS = { /* Implement TopicByIdResult resolver logic here */ }; diff --git a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicCreateResult.ts b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicCreateResult.ts index ed7e07e6..ef6971ef 100644 --- a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicCreateResult.ts +++ b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicCreateResult.ts @@ -1,4 +1,4 @@ -import type { TopicCreateResultResolvers } from './../../types.gen'; -export const TopicCreateResult: TopicCreateResultResolvers = { +import type { TOPICCREATERESULTRESOLVERS } from './../../types.gen'; +export const TopicCreateResult: TOPICCREATERESULTRESOLVERS = { /* Implement TopicCreateResult resolver logic here */ }; diff --git a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicEditResult.ts b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicEditResult.ts index a9fbb1ab..18c10d1c 100644 --- a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicEditResult.ts +++ b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicEditResult.ts @@ -1,4 +1,4 @@ -import type { TopicEditResultResolvers } from './../../types.gen'; -export const TopicEditResult: TopicEditResultResolvers = { +import type { TOPICEDITRESULTRESOLVERS } from './../../types.gen'; +export const TopicEditResult: TOPICEDITRESULTRESOLVERS = { /* Implement TopicEditResult resolver logic here */ }; diff --git a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicsCreatedByUserResult.ts b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicsCreatedByUserResult.ts index 7fa197c2..16bd01d3 100644 --- a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicsCreatedByUserResult.ts +++ b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicsCreatedByUserResult.ts @@ -1,4 +1,4 @@ -import type { TopicsCreatedByUserResultResolvers } from './../../types.gen'; -export const TopicsCreatedByUserResult: TopicsCreatedByUserResultResolvers = { +import type { TOPICSCREATEDBYUSERRESULTRESOLVERS } from './../../types.gen'; +export const TopicsCreatedByUserResult: TOPICSCREATEDBYUSERRESULTRESOLVERS = { /* Implement TopicsCreatedByUserResult resolver logic here */ }; diff --git a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/user/rslvrs/Profile.ts b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/user/rslvrs/Profile.ts index ed9ec60f..d5b7bfc4 100644 --- a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/user/rslvrs/Profile.ts +++ b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/user/rslvrs/Profile.ts @@ -1,4 +1,4 @@ -import type { ProfileResolvers } from './../../types.gen'; -export const Profile: ProfileResolvers = { +import type { PROFILERESOLVERS } from './../../types.gen'; +export const Profile: PROFILERESOLVERS = { /* Implement Profile resolver logic here */ }; diff --git a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/user/rslvrs/Query/me.ts b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/user/rslvrs/Query/me.ts index c7d6fda1..400cd073 100644 --- a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/user/rslvrs/Query/me.ts +++ b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/user/rslvrs/Query/me.ts @@ -1,5 +1,5 @@ -import type { QueryResolvers } from './../../../types.gen'; -export const me: NonNullable = async ( +import type { QUERYRESOLVERS } from './../../../types.gen'; +export const me: NonNullable = async ( _parent, _arg, _ctx diff --git a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/user/rslvrs/Query/userByAccountName.ts b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/user/rslvrs/Query/userByAccountName.ts index 3ae04351..42941bcb 100644 --- a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/user/rslvrs/Query/userByAccountName.ts +++ b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/user/rslvrs/Query/userByAccountName.ts @@ -1,6 +1,6 @@ -import type { QueryResolvers } from './../../../types.gen'; +import type { QUERYRESOLVERS } from './../../../types.gen'; export const userByAccountName: NonNullable< - QueryResolvers['userByAccountName'] + QUERYRESOLVERS['userByAccountName'] > = async (_parent, _arg, _ctx) => { /* Implement Query.userByAccountName resolver logic here */ }; diff --git a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/user/rslvrs/Subscription/profileChanges.ts b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/user/rslvrs/Subscription/profileChanges.ts index 538e1348..eecc371c 100644 --- a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/user/rslvrs/Subscription/profileChanges.ts +++ b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/user/rslvrs/Subscription/profileChanges.ts @@ -1,6 +1,6 @@ -import type { SubscriptionResolvers } from './../../../types.gen'; +import type { SUBSCRIPTIONRESOLVERS } from './../../../types.gen'; export const profileChanges: NonNullable< - SubscriptionResolvers['profileChanges'] + SUBSCRIPTIONRESOLVERS['profileChanges'] > = { subscribe: async (_parent, _arg, _ctx) => { /* Implement Subscription.profileChanges resolver logic here */ diff --git a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/user/rslvrs/User.ts b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/user/rslvrs/User.ts index 73fb05a4..1175afc4 100644 --- a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/user/rslvrs/User.ts +++ b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/user/rslvrs/User.ts @@ -1,4 +1,4 @@ -import type { UserResolvers } from './../../types.gen'; -export const User: UserResolvers = { +import type { USERRESOLVERS } from './../../types.gen'; +export const User: USERRESOLVERS = { /* Implement User resolver logic here */ }; diff --git a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/user/rslvrs/UserResult.ts b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/user/rslvrs/UserResult.ts index 824ac2a2..4c4132d3 100644 --- a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/user/rslvrs/UserResult.ts +++ b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/user/rslvrs/UserResult.ts @@ -1,4 +1,4 @@ -import type { UserResultResolvers } from './../../types.gen'; -export const UserResult: UserResultResolvers = { +import type { USERRESULTRESOLVERS } from './../../types.gen'; +export const UserResult: USERRESULTRESOLVERS = { /* Implement UserResult resolver logic here */ }; From 3cd1e38524a746705aa53004861b28004d986985 Mon Sep 17 00:00:00 2001 From: Eddy Nguyen Date: Thu, 13 Jun 2024 21:26:28 +1000 Subject: [PATCH 3/7] Add changeset --- .changeset/hot-spoons-tie.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/hot-spoons-tie.md diff --git a/.changeset/hot-spoons-tie.md b/.changeset/hot-spoons-tie.md new file mode 100644 index 00000000..88c0d99b --- /dev/null +++ b/.changeset/hot-spoons-tie.md @@ -0,0 +1,5 @@ +--- +'@eddeee888/gcg-typescript-resolver-files': minor +--- + +Use reported generated type names from @graphql-codegen/typescript-resolvers meta to support all naming convention From 5eb1bfc599eb6ec7022f36b8b905a32b950fe1d8 Mon Sep 17 00:00:00 2001 From: Eddy Nguyen Date: Thu, 13 Jun 2024 21:41:48 +1000 Subject: [PATCH 4/7] Fix lint/unit test --- .../validateAndMergeParsedConfigs.spec.ts | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/typescript-resolver-files/src/validateAndMergeParsedConfigs/validateAndMergeParsedConfigs.spec.ts b/packages/typescript-resolver-files/src/validateAndMergeParsedConfigs/validateAndMergeParsedConfigs.spec.ts index 70758316..bc91809d 100644 --- a/packages/typescript-resolver-files/src/validateAndMergeParsedConfigs/validateAndMergeParsedConfigs.spec.ts +++ b/packages/typescript-resolver-files/src/validateAndMergeParsedConfigs/validateAndMergeParsedConfigs.spec.ts @@ -32,8 +32,8 @@ describe('validateAndMergeParsedConfigs', () => { path: 'user/User.ts', isOnFilesystem: false, }, - typeNamedImport: 'User', - typeString: 'UserResolver', + typeNamedImport: () => 'User', + typeString: () => 'UserResolver', }, }, }, @@ -41,8 +41,8 @@ describe('validateAndMergeParsedConfigs', () => { DateTime: { moduleName: 'base', schemaType: 'DateTime', - typeString: 'DateTimeResolver', - typeNamedImport: 'DateTimeResolver', + typeString: () => 'DateTimeResolver', + typeNamedImport: () => 'DateTimeResolver', resolverFile: { name: 'DateTime', path: 'base/DateTime.ts', @@ -107,8 +107,8 @@ describe('validateAndMergeParsedConfigs', () => { path: 'user/User.ts', isOnFilesystem: false, }, - typeNamedImport: 'User', - typeString: 'UserResolver', + typeNamedImport: expect.any(Function), + typeString: expect.any(Function), }, }, }, @@ -116,8 +116,8 @@ describe('validateAndMergeParsedConfigs', () => { DateTime: { moduleName: 'base', schemaType: 'DateTime', - typeString: 'DateTimeResolver', - typeNamedImport: 'DateTimeResolver', + typeString: expect.any(Function), + typeNamedImport: expect.any(Function), resolverFile: { name: 'DateTime', path: 'base/DateTime.ts', @@ -167,8 +167,8 @@ describe('validateAndMergeParsedConfigs', () => { path: 'user/User.ts', isOnFilesystem: false, }, - typeNamedImport: 'User', - typeString: 'UserResolver', + typeNamedImport: () => 'User', + typeString: () => 'UserResolver', }, }, }, @@ -176,8 +176,8 @@ describe('validateAndMergeParsedConfigs', () => { DateTime: { moduleName: 'base', schemaType: 'DateTime', - typeString: 'DateTimeResolver', - typeNamedImport: 'DateTimeResolver', + typeString: () => 'DateTimeResolver', + typeNamedImport: () => 'DateTimeResolver', resolverFile: { name: 'DateTime', path: 'base/DateTime.ts', From dd9b00aa4661c96ab6d3539062e96929de55d5e6 Mon Sep 17 00:00:00 2001 From: Eddy Nguyen Date: Thu, 13 Jun 2024 21:55:42 +1000 Subject: [PATCH 5/7] Update e2e tests for namingConvention to capture all possible types --- .../src/test-config-overrides/codegen.ts | 1 + .../modules/base/rslvrs/Currency.ts | 5 +++++ .../modules/base/rslvrs/Error.ts | 4 ++++ .../modules/base/rslvrs/ErrorType.ts | 7 +++++++ .../modules/resolvers.gen.ts | 16 ++++++++++++++++ .../modules/topic/rslvrs/TopicByIdPayload.ts | 4 ++++ .../modules/topic/rslvrs/TopicCreatePayload.ts | 4 ++++ .../modules/topic/rslvrs/TopicEditPayload.ts | 4 ++++ .../topic/rslvrs/TopicsCreatedByUserPayload.ts | 4 ++++ .../modules/user/rslvrs/UserPayload.ts | 4 ++++ 10 files changed, 53 insertions(+) create mode 100644 packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/base/rslvrs/Currency.ts create mode 100644 packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/base/rslvrs/Error.ts create mode 100644 packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/base/rslvrs/ErrorType.ts create mode 100644 packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicByIdPayload.ts create mode 100644 packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicCreatePayload.ts create mode 100644 packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicEditPayload.ts create mode 100644 packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicsCreatedByUserPayload.ts create mode 100644 packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/user/rslvrs/UserPayload.ts diff --git a/packages/typescript-resolver-files-e2e/src/test-config-overrides/codegen.ts b/packages/typescript-resolver-files-e2e/src/test-config-overrides/codegen.ts index 23374299..a31a42fb 100644 --- a/packages/typescript-resolver-files-e2e/src/test-config-overrides/codegen.ts +++ b/packages/typescript-resolver-files-e2e/src/test-config-overrides/codegen.ts @@ -14,6 +14,7 @@ const config: CodegenConfig = { resolverRelativeTargetDir: 'rslvrs', resolverMainFile: 'resolvers.gen.ts', typeDefsFilePath: false, + resolverGeneration: 'all', scalarsOverrides: { BigInt: { resolver: diff --git a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/base/rslvrs/Currency.ts b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/base/rslvrs/Currency.ts new file mode 100644 index 00000000..4bdcbb88 --- /dev/null +++ b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/base/rslvrs/Currency.ts @@ -0,0 +1,5 @@ +import type { CURRENCYRESOLVERS } from './../../types.gen'; +export const Currency: CURRENCYRESOLVERS = { + USD: 'USD', + AUD: 'AUD', +}; diff --git a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/base/rslvrs/Error.ts b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/base/rslvrs/Error.ts new file mode 100644 index 00000000..a0a4db7c --- /dev/null +++ b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/base/rslvrs/Error.ts @@ -0,0 +1,4 @@ +import type { ERRORRESOLVERS } from './../../types.gen'; +export const Error: ERRORRESOLVERS = { + /* Implement Error interface logic here */ +}; diff --git a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/base/rslvrs/ErrorType.ts b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/base/rslvrs/ErrorType.ts new file mode 100644 index 00000000..a1b02066 --- /dev/null +++ b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/base/rslvrs/ErrorType.ts @@ -0,0 +1,7 @@ +import type { ERRORTYPERESOLVERS } from './../../types.gen'; +export const ErrorType: ERRORTYPERESOLVERS = { + NOT_FOUND: 'NOT_FOUND', + INPUT_VALIDATION_ERROR: 'INPUT_VALIDATION_ERROR', + FORBIDDEN_ERROR: 'FORBIDDEN_ERROR', + UNEXPECTED_ERROR: 'UNEXPECTED_ERROR', +}; diff --git a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/resolvers.gen.ts b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/resolvers.gen.ts index 84092757..680dae03 100644 --- a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/resolvers.gen.ts +++ b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/resolvers.gen.ts @@ -19,6 +19,14 @@ import { User } from './user/rslvrs/User'; import { UserResult } from './user/rslvrs/UserResult'; import { SomeOtherScalars } from './base/rslvrs/SomeOtherScalars'; import { WithInputOutput } from './base/rslvrs/WithInputOutput'; +import { Error } from './base/rslvrs/Error'; +import { TopicByIdPayload } from './topic/rslvrs/TopicByIdPayload'; +import { TopicCreatePayload } from './topic/rslvrs/TopicCreatePayload'; +import { TopicEditPayload } from './topic/rslvrs/TopicEditPayload'; +import { TopicsCreatedByUserPayload } from './topic/rslvrs/TopicsCreatedByUserPayload'; +import { UserPayload } from './user/rslvrs/UserPayload'; +import { Currency } from './base/rslvrs/Currency'; +import { ErrorType } from './base/rslvrs/ErrorType'; import CustomBigIntResolver from './base/CustomBigIntResolver'; import { DateTimeResolver } from 'graphql-scalars'; export const resolvers: Resolvers = { @@ -45,6 +53,14 @@ export const resolvers: Resolvers = { UserResult: UserResult, SomeOtherScalars: SomeOtherScalars, WithInputOutput: WithInputOutput, + Error: Error, + TopicByIdPayload: TopicByIdPayload, + TopicCreatePayload: TopicCreatePayload, + TopicEditPayload: TopicEditPayload, + TopicsCreatedByUserPayload: TopicsCreatedByUserPayload, + UserPayload: UserPayload, + Currency: Currency, + ErrorType: ErrorType, BigInt: CustomBigIntResolver, DateTime: DateTimeResolver, }; diff --git a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicByIdPayload.ts b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicByIdPayload.ts new file mode 100644 index 00000000..33d41b5d --- /dev/null +++ b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicByIdPayload.ts @@ -0,0 +1,4 @@ +import type { TOPICBYIDPAYLOADRESOLVERS } from './../../types.gen'; +export const TopicByIdPayload: TOPICBYIDPAYLOADRESOLVERS = { + /* Implement TopicByIdPayload union logic here */ +}; diff --git a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicCreatePayload.ts b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicCreatePayload.ts new file mode 100644 index 00000000..23cdc88b --- /dev/null +++ b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicCreatePayload.ts @@ -0,0 +1,4 @@ +import type { TOPICCREATEPAYLOADRESOLVERS } from './../../types.gen'; +export const TopicCreatePayload: TOPICCREATEPAYLOADRESOLVERS = { + /* Implement TopicCreatePayload union logic here */ +}; diff --git a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicEditPayload.ts b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicEditPayload.ts new file mode 100644 index 00000000..622c090a --- /dev/null +++ b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicEditPayload.ts @@ -0,0 +1,4 @@ +import type { TOPICEDITPAYLOADRESOLVERS } from './../../types.gen'; +export const TopicEditPayload: TOPICEDITPAYLOADRESOLVERS = { + /* Implement TopicEditPayload union logic here */ +}; diff --git a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicsCreatedByUserPayload.ts b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicsCreatedByUserPayload.ts new file mode 100644 index 00000000..7d1a2f87 --- /dev/null +++ b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/topic/rslvrs/TopicsCreatedByUserPayload.ts @@ -0,0 +1,4 @@ +import type { TOPICSCREATEDBYUSERPAYLOADRESOLVERS } from './../../types.gen'; +export const TopicsCreatedByUserPayload: TOPICSCREATEDBYUSERPAYLOADRESOLVERS = { + /* Implement TopicsCreatedByUserPayload union logic here */ +}; diff --git a/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/user/rslvrs/UserPayload.ts b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/user/rslvrs/UserPayload.ts new file mode 100644 index 00000000..feff6c4a --- /dev/null +++ b/packages/typescript-resolver-files-e2e/src/test-config-overrides/modules/user/rslvrs/UserPayload.ts @@ -0,0 +1,4 @@ +import type { USERPAYLOADRESOLVERS } from './../../types.gen'; +export const UserPayload: USERPAYLOADRESOLVERS = { + /* Implement UserPayload union logic here */ +}; From 3beb7ce26cd2fab828b998809949a5199e9c3197 Mon Sep 17 00:00:00 2001 From: Eddy Nguyen Date: Thu, 13 Jun 2024 21:57:47 +1000 Subject: [PATCH 6/7] Add FIXME to update meta --- .../src/generateResolverFiles/addResolverMainFiles.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/typescript-resolver-files/src/generateResolverFiles/addResolverMainFiles.ts b/packages/typescript-resolver-files/src/generateResolverFiles/addResolverMainFiles.ts index 8bd1f6dd..69a174b6 100644 --- a/packages/typescript-resolver-files/src/generateResolverFiles/addResolverMainFiles.ts +++ b/packages/typescript-resolver-files/src/generateResolverFiles/addResolverMainFiles.ts @@ -37,6 +37,7 @@ export const addResolverMainFiles = ({ resolverMainFile, resolverMainFileMode, emitLegacyCommonJSImports, + generatedTypesFileMeta, }, result, }: GenerateResolverFilesContext): void => { @@ -194,7 +195,7 @@ export const addResolverMainFiles = ({ }, resolverMainFiles); const resolversIdentifier = 'resolvers'; - const resolversTypeName = 'Resolvers'; // Generated type from typescript-resolvers plugin + const resolversTypeName = 'Resolvers'; // FIXME: use data from `typescript-resolvers`'s `meta` Object.entries(resolverMainFiles).forEach(([resolverMainFilename, file]) => { const outputDir = path.dirname(resolverMainFilename); From 7f465d571a6a3508999f3fcf1708ad9af55a2716 Mon Sep 17 00:00:00 2001 From: Eddy Nguyen Date: Thu, 13 Jun 2024 21:59:47 +1000 Subject: [PATCH 7/7] Fix lint --- .../src/generateResolverFiles/addResolverMainFiles.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/typescript-resolver-files/src/generateResolverFiles/addResolverMainFiles.ts b/packages/typescript-resolver-files/src/generateResolverFiles/addResolverMainFiles.ts index 69a174b6..826d6f9d 100644 --- a/packages/typescript-resolver-files/src/generateResolverFiles/addResolverMainFiles.ts +++ b/packages/typescript-resolver-files/src/generateResolverFiles/addResolverMainFiles.ts @@ -37,7 +37,6 @@ export const addResolverMainFiles = ({ resolverMainFile, resolverMainFileMode, emitLegacyCommonJSImports, - generatedTypesFileMeta, }, result, }: GenerateResolverFilesContext): void => {