Skip to content

Commit 51bdecf

Browse files
daneznodkz
authored andcommitted
fix(discriminator): Copy relations to child type composers
1 parent 4f08750 commit 51bdecf

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/discriminators/__tests__/composeChildTC-test.ts

+19
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ beforeAll(() => schemaComposer.clear());
88

99
describe('composeChildTC ->', () => {
1010
const CharacterDTC = composeWithMongooseDiscriminators(CharacterModel);
11+
CharacterDTC.addRelation('friends', {
12+
resolver: () => CharacterDTC.getResolver('findById'),
13+
prepareArgs: {
14+
_id: (source) => source.friends,
15+
},
16+
projection: { friends: 1 },
17+
});
18+
CharacterDTC.extendField('friends', { type: '[String!]' });
19+
1120
const PersonTC = CharacterDTC.discriminator(PersonModel);
1221
const DroidTC = CharacterDTC.discriminator(DroidModel);
1322

@@ -21,6 +30,16 @@ describe('composeChildTC ->', () => {
2130
expect(PersonTC.getFieldNames()).toEqual(expect.arrayContaining(CharacterDTC.getFieldNames()));
2231
});
2332

33+
it('should copy all relations from BaseDTC to ChildTCs', () => {
34+
expect(DroidTC.getRelations()).toEqual(CharacterDTC.getRelations());
35+
expect(PersonTC.getRelations()).toEqual(CharacterDTC.getRelations());
36+
});
37+
38+
it('should copy the extended Field from BaseDTC to ChildTCs', () => {
39+
expect(DroidTC.getField('friends').type).toEqual(CharacterDTC.getField('friends').type);
40+
expect(PersonTC.getField('friends').type).toEqual(CharacterDTC.getField('friends').type);
41+
});
42+
2443
it('should make childTC have same fieldTypes as baseTC', () => {
2544
const characterFields = CharacterDTC.getFieldNames();
2645

src/discriminators/composeChildTC.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@ import type {
66
import { prepareChildResolvers } from './prepareChildResolvers';
77
import { reorderFields } from './utils/reorderFields';
88

9+
function copyBaseTcRelationsToChildTc(
10+
baseDTC: ObjectTypeComposer<any, any>,
11+
childTC: ObjectTypeComposer<any, any>
12+
) {
13+
const relations = baseDTC.getRelations();
14+
const childRelations = childTC.getRelations();
15+
Object.keys(relations).forEach((name) => {
16+
if (childRelations[name]) {
17+
return;
18+
}
19+
childTC.addRelation(name, relations[name] as any);
20+
});
21+
22+
return childTC;
23+
}
24+
925
// copy all baseTypeComposer fields to childTC
1026
// these are the fields before calling discriminator
1127
function copyBaseTCFieldsToChildTC(
@@ -35,7 +51,8 @@ export function composeChildTC<TSource, TContext>(
3551
childTC: ObjectTypeComposer<TSource, TContext>,
3652
opts: ComposeWithMongooseDiscriminatorsOpts<TContext>
3753
): ObjectTypeComposer<TSource, TContext> {
38-
const composedChildTC = copyBaseTCFieldsToChildTC(baseDTC, childTC);
54+
let composedChildTC = copyBaseTcRelationsToChildTc(baseDTC, childTC);
55+
composedChildTC = copyBaseTCFieldsToChildTC(baseDTC, composedChildTC);
3956

4057
composedChildTC.addInterface(baseDTC.getDInterface());
4158

0 commit comments

Comments
 (0)