diff --git a/.changeset/seven-camels-compete.md b/.changeset/seven-camels-compete.md new file mode 100644 index 00000000..903237f3 --- /dev/null +++ b/.changeset/seven-camels-compete.md @@ -0,0 +1,5 @@ +--- +'@eddeee888/gcg-typescript-resolver-files': patch +--- + +Ensure \_\_isTypeOf is in the picked properties so users can choose this way to handle abstract type should they choose diff --git a/packages/typescript-resolver-files-e2e/src/test-extended-object-types/schema-base/book-store-v2_new_again-final_v3/resolvers/Topic.ts b/packages/typescript-resolver-files-e2e/src/test-extended-object-types/schema-base/book-store-v2_new_again-final_v3/resolvers/Topic.ts index 45011951..f647ee6b 100644 --- a/packages/typescript-resolver-files-e2e/src/test-extended-object-types/schema-base/book-store-v2_new_again-final_v3/resolvers/Topic.ts +++ b/packages/typescript-resolver-files-e2e/src/test-extended-object-types/schema-base/book-store-v2_new_again-final_v3/resolvers/Topic.ts @@ -1,7 +1,8 @@ import type { TopicResolvers } from './../../types.generated'; -export const Topic: Pick = { - /* Implement Topic resolver logic here */ - bookStore_for_topic: async (_parent, _arg, _ctx) => { - /* Topic.bookStore_for_topic resolver is required because Topic.bookStore_for_topic exists but TopicMapper.bookStore_for_topic does not */ - }, -}; +export const Topic: Pick = + { + /* Implement Topic resolver logic here */ + bookStore_for_topic: async (_parent, _arg, _ctx) => { + /* Topic.bookStore_for_topic resolver is required because Topic.bookStore_for_topic exists but TopicMapper.bookStore_for_topic does not */ + }, + }; diff --git a/packages/typescript-resolver-files-e2e/src/test-extended-object-types/schema-base/book-store-v2_new_again-final_v3/resolvers/User.ts b/packages/typescript-resolver-files-e2e/src/test-extended-object-types/schema-base/book-store-v2_new_again-final_v3/resolvers/User.ts index 8c3b8e8c..9ca0a2ab 100644 --- a/packages/typescript-resolver-files-e2e/src/test-extended-object-types/schema-base/book-store-v2_new_again-final_v3/resolvers/User.ts +++ b/packages/typescript-resolver-files-e2e/src/test-extended-object-types/schema-base/book-store-v2_new_again-final_v3/resolvers/User.ts @@ -1,4 +1,4 @@ import type { UserResolvers } from './../../types.generated'; -export const User: Pick = { +export const User: Pick = { /* Implement User resolver logic here */ }; diff --git a/packages/typescript-resolver-files-e2e/src/test-extended-object-types/schema-base/topic/resolvers/Topic.ts b/packages/typescript-resolver-files-e2e/src/test-extended-object-types/schema-base/topic/resolvers/Topic.ts index 40849c4f..efda7338 100644 --- a/packages/typescript-resolver-files-e2e/src/test-extended-object-types/schema-base/topic/resolvers/Topic.ts +++ b/packages/typescript-resolver-files-e2e/src/test-extended-object-types/schema-base/topic/resolvers/Topic.ts @@ -11,6 +11,7 @@ export const Topic: Pick< | 'id' | 'name' | 'url' + | '__isTypeOf' > = { /* Implement Topic resolver logic here */ extendedTopicFieldInDifferentFileAndSameModule1: async ( diff --git a/packages/typescript-resolver-files-e2e/src/test-extended-object-types/schema-base/user/resolvers/Topic.ts b/packages/typescript-resolver-files-e2e/src/test-extended-object-types/schema-base/user/resolvers/Topic.ts index ae668760..dff73301 100644 --- a/packages/typescript-resolver-files-e2e/src/test-extended-object-types/schema-base/user/resolvers/Topic.ts +++ b/packages/typescript-resolver-files-e2e/src/test-extended-object-types/schema-base/user/resolvers/Topic.ts @@ -5,6 +5,7 @@ export const Topic: Pick< | 'extendedTopicFieldInDifferentFileAndDifferentModule1' | 'extendedTopicFieldInDifferentFileAndDifferentModule2' | 'extendedTopicFieldInDifferentFileAndDifferentModule3' + | '__isTypeOf' > = { /* Implement Topic resolver logic here */ creator: ({ creator }, _arg, _ctx) => { diff --git a/packages/typescript-resolver-files-e2e/src/test-extended-object-types/schema-base/user/resolvers/User.ts b/packages/typescript-resolver-files-e2e/src/test-extended-object-types/schema-base/user/resolvers/User.ts index 2ef09e94..36c7066f 100644 --- a/packages/typescript-resolver-files-e2e/src/test-extended-object-types/schema-base/user/resolvers/User.ts +++ b/packages/typescript-resolver-files-e2e/src/test-extended-object-types/schema-base/user/resolvers/User.ts @@ -9,6 +9,7 @@ export const User: Pick< | 'avatar' | 'id' | 'name' + | '__isTypeOf' > = { /* Implement User resolver logic here */ }; diff --git a/packages/typescript-resolver-files-e2e/src/test-extended-object-types/schema-federated-extended-object-type/topic/resolvers/ForeignType.ts b/packages/typescript-resolver-files-e2e/src/test-extended-object-types/schema-federated-extended-object-type/topic/resolvers/ForeignType.ts index 05d5faef..3ebf15f2 100644 --- a/packages/typescript-resolver-files-e2e/src/test-extended-object-types/schema-federated-extended-object-type/topic/resolvers/ForeignType.ts +++ b/packages/typescript-resolver-files-e2e/src/test-extended-object-types/schema-federated-extended-object-type/topic/resolvers/ForeignType.ts @@ -1,7 +1,7 @@ import type { ForeignTypeResolvers } from './../../types.generated'; export const ForeignType: Pick< ForeignTypeResolvers, - 'id' | 'topics' | '__resolveReference' + 'id' | 'topics' | '__isTypeOf' | '__resolveReference' > = { /* Implement ForeignType resolver logic here */ }; diff --git a/packages/typescript-resolver-files-e2e/src/test-extended-object-types/schema-federated-extended-object-type/user/resolvers/ForeignType.ts b/packages/typescript-resolver-files-e2e/src/test-extended-object-types/schema-federated-extended-object-type/user/resolvers/ForeignType.ts index dcf46a0f..00157f00 100644 --- a/packages/typescript-resolver-files-e2e/src/test-extended-object-types/schema-federated-extended-object-type/user/resolvers/ForeignType.ts +++ b/packages/typescript-resolver-files-e2e/src/test-extended-object-types/schema-federated-extended-object-type/user/resolvers/ForeignType.ts @@ -1,7 +1,7 @@ import type { ForeignTypeResolvers } from './../../types.generated'; export const ForeignType: Pick< ForeignTypeResolvers, - 'users' | '__resolveReference' + 'users' | '__isTypeOf' | '__resolveReference' > = { /* Implement ForeignType resolver logic here */ }; diff --git a/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/schools-domain/course/resolvers/School.ts b/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/schools-domain/course/resolvers/School.ts index 7135d601..38e4e889 100644 --- a/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/schools-domain/course/resolvers/School.ts +++ b/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/schools-domain/course/resolvers/School.ts @@ -1,4 +1,4 @@ import type { SchoolResolvers } from './../../../types.generated'; -export const School: Pick = { +export const School: Pick = { /* Implement School resolver logic here */ }; diff --git a/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/schools-domain/demographics/resolvers/School.ts b/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/schools-domain/demographics/resolvers/School.ts index 7d898fc0..545c329b 100644 --- a/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/schools-domain/demographics/resolvers/School.ts +++ b/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/schools-domain/demographics/resolvers/School.ts @@ -1,4 +1,4 @@ import type { SchoolResolvers } from './../../../types.generated'; -export const School: Pick = { +export const School: Pick = { /* Implement School resolver logic here */ }; diff --git a/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/schools-domain/school/resolvers/School.ts b/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/schools-domain/school/resolvers/School.ts index 6def4ca4..7ce44e14 100644 --- a/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/schools-domain/school/resolvers/School.ts +++ b/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/schools-domain/school/resolvers/School.ts @@ -1,4 +1,4 @@ import type { SchoolResolvers } from './../../../types.generated'; -export const School: Pick = { +export const School: Pick = { /* Implement School resolver logic here */ }; diff --git a/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/students-domain/avatar/resolvers/StudentProfile.ts b/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/students-domain/avatar/resolvers/StudentProfile.ts index 2cb2cbf8..5a15ae8d 100644 --- a/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/students-domain/avatar/resolvers/StudentProfile.ts +++ b/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/students-domain/avatar/resolvers/StudentProfile.ts @@ -1,4 +1,7 @@ import type { StudentProfileResolvers } from './../../../types.generated'; -export const StudentProfile: Pick = { +export const StudentProfile: Pick< + StudentProfileResolvers, + 'avatar' | '__isTypeOf' +> = { /* Implement StudentProfile resolver logic here */ }; diff --git a/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/students-domain/guardians/resolvers/Student.ts b/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/students-domain/guardians/resolvers/Student.ts index 5854e932..3adaad5e 100644 --- a/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/students-domain/guardians/resolvers/Student.ts +++ b/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/students-domain/guardians/resolvers/Student.ts @@ -1,4 +1,4 @@ import type { StudentResolvers } from './../../../types.generated'; -export const Student: Pick = { +export const Student: Pick = { /* Implement Student resolver logic here */ }; diff --git a/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/students-domain/profile/resolvers/Student.ts b/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/students-domain/profile/resolvers/Student.ts index fde81c90..f9633f53 100644 --- a/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/students-domain/profile/resolvers/Student.ts +++ b/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/students-domain/profile/resolvers/Student.ts @@ -1,4 +1,4 @@ import type { StudentResolvers } from './../../../types.generated'; -export const Student: Pick = { +export const Student: Pick = { /* Implement Student resolver logic here */ }; diff --git a/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/students-domain/profile/resolvers/StudentProfile.ts b/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/students-domain/profile/resolvers/StudentProfile.ts index daa7a27d..73b72928 100644 --- a/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/students-domain/profile/resolvers/StudentProfile.ts +++ b/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/students-domain/profile/resolvers/StudentProfile.ts @@ -1,7 +1,7 @@ import type { StudentProfileResolvers } from './../../../types.generated'; export const StudentProfile: Pick< StudentProfileResolvers, - 'email' | 'firstName' | 'id' | 'lastName' | 'phoneNumber' + 'email' | 'firstName' | 'id' | 'lastName' | 'phoneNumber' | '__isTypeOf' > = { /* Implement StudentProfile resolver logic here */ }; diff --git a/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/students-domain/student/resolvers/Student.ts b/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/students-domain/student/resolvers/Student.ts index 5c8e3a39..667fd1b0 100644 --- a/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/students-domain/student/resolvers/Student.ts +++ b/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/students-domain/student/resolvers/Student.ts @@ -1,4 +1,4 @@ import type { StudentResolvers } from './../../../types.generated'; -export const Student: Pick = { +export const Student: Pick = { /* Implement Student resolver logic here */ }; diff --git a/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/teachers-domain/avatar/resolvers/TeacherProfile.ts b/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/teachers-domain/avatar/resolvers/TeacherProfile.ts index 81957571..7c0f7fb4 100644 --- a/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/teachers-domain/avatar/resolvers/TeacherProfile.ts +++ b/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/teachers-domain/avatar/resolvers/TeacherProfile.ts @@ -1,4 +1,7 @@ import type { TeacherProfileResolvers } from './../../../types.generated'; -export const TeacherProfile: Pick = { +export const TeacherProfile: Pick< + TeacherProfileResolvers, + 'avatar' | '__isTypeOf' +> = { /* Implement TeacherProfile resolver logic here */ }; diff --git a/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/teachers-domain/profile/resolvers/Teacher.ts b/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/teachers-domain/profile/resolvers/Teacher.ts index 3decbb02..9015e062 100644 --- a/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/teachers-domain/profile/resolvers/Teacher.ts +++ b/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/teachers-domain/profile/resolvers/Teacher.ts @@ -1,4 +1,4 @@ import type { TeacherResolvers } from './../../../types.generated'; -export const Teacher: Pick = { +export const Teacher: Pick = { /* Implement Teacher resolver logic here */ }; diff --git a/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/teachers-domain/profile/resolvers/TeacherProfile.ts b/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/teachers-domain/profile/resolvers/TeacherProfile.ts index efd92550..2ad6e4bb 100644 --- a/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/teachers-domain/profile/resolvers/TeacherProfile.ts +++ b/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/teachers-domain/profile/resolvers/TeacherProfile.ts @@ -1,7 +1,7 @@ import type { TeacherProfileResolvers } from './../../../types.generated'; export const TeacherProfile: Pick< TeacherProfileResolvers, - 'email' | 'firstName' | 'id' | 'lastName' | 'phoneNumber' + 'email' | 'firstName' | 'id' | 'lastName' | 'phoneNumber' | '__isTypeOf' > = { /* Implement TeacherProfile resolver logic here */ }; diff --git a/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/teachers-domain/teacher/resolvers/Teacher.ts b/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/teachers-domain/teacher/resolvers/Teacher.ts index 4f4bdb81..51615790 100644 --- a/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/teachers-domain/teacher/resolvers/Teacher.ts +++ b/packages/typescript-resolver-files-e2e/src/test-nested-domain-modules/teachers-domain/teacher/resolvers/Teacher.ts @@ -1,4 +1,4 @@ import type { TeacherResolvers } from './../../../types.generated'; -export const Teacher: Pick = { +export const Teacher: Pick = { /* Implement Teacher resolver logic here */ }; diff --git a/packages/typescript-resolver-files-e2e/src/test-resolver-generation/schema-disabled/topic/resolvers/User.ts b/packages/typescript-resolver-files-e2e/src/test-resolver-generation/schema-disabled/topic/resolvers/User.ts index 6c6f6f66..ba93010b 100644 --- a/packages/typescript-resolver-files-e2e/src/test-resolver-generation/schema-disabled/topic/resolvers/User.ts +++ b/packages/typescript-resolver-files-e2e/src/test-resolver-generation/schema-disabled/topic/resolvers/User.ts @@ -8,7 +8,7 @@ import type { UserResolvers } from './../../types.generated'; * * If you want to skip this file generation, remove the mapper or update the pattern in the `resolverGeneration.object` config. */ -export const User: Pick = { +export const User: Pick = { /* Implement User resolver logic here */ topics: async (_parent, _arg, _ctx) => { /* User.topics resolver is required because User.topics exists but UserMapper.topics does not */ diff --git a/packages/typescript-resolver-files-e2e/src/test-resolver-generation/schema-disabled/user/resolvers/User.ts b/packages/typescript-resolver-files-e2e/src/test-resolver-generation/schema-disabled/user/resolvers/User.ts index fc8d7ea0..d316fa2f 100644 --- a/packages/typescript-resolver-files-e2e/src/test-resolver-generation/schema-disabled/user/resolvers/User.ts +++ b/packages/typescript-resolver-files-e2e/src/test-resolver-generation/schema-disabled/user/resolvers/User.ts @@ -18,6 +18,7 @@ export const User: Pick< | 'avatar' | 'id' | 'name' + | '__isTypeOf' > = { /* Implement User resolver logic here */ accountGitHub: async (_parent, _arg, _ctx) => { diff --git a/packages/typescript-resolver-files-e2e/src/test-resolvers-auto-wireup/schema/pet/resolvers/User.ts b/packages/typescript-resolver-files-e2e/src/test-resolvers-auto-wireup/schema/pet/resolvers/User.ts index 08c2cc9b..9debde02 100644 --- a/packages/typescript-resolver-files-e2e/src/test-resolvers-auto-wireup/schema/pet/resolvers/User.ts +++ b/packages/typescript-resolver-files-e2e/src/test-resolvers-auto-wireup/schema/pet/resolvers/User.ts @@ -1,4 +1,4 @@ import type { UserResolvers } from './../../types.generated'; -export const User: Pick = { +export const User: Pick = { /* Implement User resolver logic here */ }; diff --git a/packages/typescript-resolver-files-e2e/src/test-resolvers-auto-wireup/schema/zoo/resolvers/Profile.ts b/packages/typescript-resolver-files-e2e/src/test-resolvers-auto-wireup/schema/zoo/resolvers/Profile.ts index d11ae245..b1230601 100644 --- a/packages/typescript-resolver-files-e2e/src/test-resolvers-auto-wireup/schema/zoo/resolvers/Profile.ts +++ b/packages/typescript-resolver-files-e2e/src/test-resolvers-auto-wireup/schema/zoo/resolvers/Profile.ts @@ -1,4 +1,4 @@ import type { ProfileResolvers } from './../../types.generated'; -export const Profile: Pick = { +export const Profile: Pick = { /* Implement Profile resolver logic here */ }; diff --git a/packages/typescript-resolver-files/src/generateResolverFiles/handleGraphQLObjectType.ts b/packages/typescript-resolver-files/src/generateResolverFiles/handleGraphQLObjectType.ts index 0cf1e6c9..058e52ad 100644 --- a/packages/typescript-resolver-files/src/generateResolverFiles/handleGraphQLObjectType.ts +++ b/packages/typescript-resolver-files/src/generateResolverFiles/handleGraphQLObjectType.ts @@ -64,35 +64,38 @@ export const handleGraphQLObjectType: GraphQLTypeHandler< return ''; })(); - if (fieldsToPick.length > 0 && pickReferenceResolver) { + const hasFieldsToPick = fieldsToPick.length > 0; + + // __isTypeOf is required to resolve abstract types, should user chooses this approach. + // TODO: Run static analysis to enforce only one __isTypeOf per Object type, so users cannot accidentally implement/override it across modules + if (hasFieldsToPick) { + fieldsToPick.push('__isTypeOf'); + } + + if (hasFieldsToPick && pickReferenceResolver) { fieldsToPick.push('__resolveReference'); } // `typeString` contains the resolver type // If there's fieldsToPick, we must only pick said fields from the original resolver type - const typeString = - fieldsToPick.length > 0 - ? `Pick<${resolversTypeMeta.typeString}, ${fieldsToPick - .map((fieldName) => `'${fieldName}'`) - .join('|')}>` - : resolversTypeMeta.typeString; + const typeString = hasFieldsToPick + ? `Pick<${resolversTypeMeta.typeString}, ${fieldsToPick + .map((fieldName) => `'${fieldName}'`) + .join('|')}>` + : resolversTypeMeta.typeString; // Array of all resolvers that may need type checking // If there's fieldsToPick, we must only generate said fields const allResolversToGenerate = graphQLObjectTypeResolversToGenerate[resolverName]; - const resolversToGenerate = - fieldsToPick.length > 0 - ? fieldsToPick.reduce( - (res, fieldToPick) => { - if (allResolversToGenerate) { - res[fieldToPick] = allResolversToGenerate[fieldToPick]; - } - return res; - }, - {} - ) - : allResolversToGenerate; + const resolversToGenerate = hasFieldsToPick + ? fieldsToPick.reduce((res, fieldToPick) => { + if (allResolversToGenerate && allResolversToGenerate[fieldToPick]) { + res[fieldToPick] = allResolversToGenerate[fieldToPick]; + } + return res; + }, {}) + : allResolversToGenerate; const variableStatement = `${forcedGenerationWarning}export const ${resolverName}: ${typeString} = { /* Implement ${resolverName} resolver logic here */