Skip to content

Commit e8a6b1c

Browse files
committed
feat(discriminator): override more TypeComposer field change methods
Remove D from addDField, setDField, addDRelation methods.
1 parent 2ae085b commit e8a6b1c

File tree

2 files changed

+139
-16
lines changed

2 files changed

+139
-16
lines changed

src/__tests__/composeWithMongooseDiscriminators-test.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ describe('composeWithMongooseDiscriminators ->', () => {
116116
});
117117
});
118118

119-
describe('addDFields(newDFields)', () => {
119+
describe('addFields(newFields)', () => {
120120
const characterDTC = composeWithMongooseDiscriminators(CharacterModel);
121121
const personTC = characterDTC.discriminator(PersonModel);
122122
const droidTC = characterDTC.discriminator(DroidModel);
@@ -126,7 +126,7 @@ describe('composeWithMongooseDiscriminators ->', () => {
126126
};
127127

128128
beforeAll(() => {
129-
characterDTC.addDFields(newFields);
129+
characterDTC.addFields(newFields);
130130
});
131131

132132
it('should add fields to baseTC', () => {
@@ -147,7 +147,31 @@ describe('composeWithMongooseDiscriminators ->', () => {
147147
});
148148
});
149149

150-
describe('extendDFields(fieldName, extensionDField)', () => {
150+
describe('removeField()', () => {
151+
const characterDTC = composeWithMongooseDiscriminators(CharacterModel);
152+
const personTC = characterDTC.discriminator(PersonModel);
153+
const droidTC = characterDTC.discriminator(DroidModel);
154+
const field = 'friends';
155+
156+
beforeAll(() => {
157+
characterDTC.removeField(field);
158+
});
159+
160+
it('should remove fields from baseTC', () => {
161+
expect(characterDTC.hasField(field)).toBeFalsy();
162+
});
163+
164+
it('should remove fields from DInterface', () => {
165+
expect(characterDTC.getDInterface().getFields()[field]).toBeFalsy();
166+
});
167+
168+
it('should remove fields from childTC', () => {
169+
expect(personTC.hasField(field)).toBeFalsy();
170+
expect(droidTC.hasField(field)).toBeFalsy();
171+
});
172+
});
173+
174+
describe('extendFields(fieldName, extensionField)', () => {
151175
const characterDTC = composeWithMongooseDiscriminators(CharacterModel);
152176
const personTC = characterDTC.discriminator(PersonModel);
153177
const droidTC = characterDTC.discriminator(DroidModel);
@@ -158,7 +182,7 @@ describe('composeWithMongooseDiscriminators ->', () => {
158182
};
159183

160184
beforeAll(() => {
161-
characterDTC.extendDField(fieldName, fieldExtension);
185+
characterDTC.extendField(fieldName, fieldExtension);
162186
});
163187

164188
it('should extend field on baseTC', () => {
@@ -226,14 +250,12 @@ describe('composeWithMongooseDiscriminators ->', () => {
226250
});
227251
});
228252

229-
describe('ChildDiscriminatorTypeComposer', () => {
253+
describe('DiscriminatorTypes', () => {
230254
it('should have as an interface DInterface', () => {
231255
const baseDTC = composeWithMongooseDiscriminators(CharacterModel);
232256
expect(baseDTC.discriminator(DroidModel).getInterfaces()).toEqual(
233257
expect.arrayContaining(Array.of(baseDTC.getDInterface()))
234258
);
235259
});
236-
237-
it('should have all resolvers', () => {});
238260
});
239261
});

src/discriminators/DiscriminatorTypeComposer.js

Lines changed: 110 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ import type { GraphQLFieldConfigMap } from 'graphql-compose/lib/graphql';
1212
import type {
1313
ComposePartialFieldConfigAsObject,
1414
RelationOpts,
15+
ComposeFieldConfig,
16+
GetRecordIdFn,
1517
} from 'graphql-compose/lib/TypeComposer';
1618
import { Model } from 'mongoose';
17-
import { type TypeConverterOpts, composeWithMongoose } from '../composeWithMongoose';
19+
import { composeWithMongoose, type TypeConverterOpts } from '../composeWithMongoose';
1820
import { composeChildTC } from './composeChildTC';
1921
import { mergeCustomizationOptions } from './merge-customization-options';
2022
import { prepareBaseResolvers } from './prepare-resolvers/prepareBaseResolvers';
@@ -164,18 +166,68 @@ export class DiscriminatorTypeComposer extends TypeComposer {
164166
return !!this.childTCs.find(ch => ch.getTypeName() === DName);
165167
}
166168

167-
// add fields only to DInterface, baseTC, childTC
168-
addDFields(newDFields: ComposeFieldConfigMap<any, any>): this {
169-
super.addFields(newDFields);
169+
setFields(fields: ComposeFieldConfigMap<any, any>): this {
170+
super.setFields(fields);
170171

171172
for (const childTC of this.childTCs) {
172-
childTC.addFields(newDFields);
173+
childTC.setFields(fields);
173174
}
174175

175176
return this;
176177
}
177178

178-
extendDField(
179+
setField(fieldName: string, fieldConfig: ComposeFieldConfig<any, any>): this {
180+
super.setField(fieldName, fieldConfig);
181+
182+
for (const childTC of this.childTCs) {
183+
childTC.setField(fieldName, fieldConfig);
184+
}
185+
186+
return this;
187+
}
188+
189+
// discriminators must have all interface fields
190+
addFields(newFields: ComposeFieldConfigMap<any, any>): this {
191+
super.addFields(newFields);
192+
193+
for (const childTC of this.childTCs) {
194+
childTC.addFields(newFields);
195+
}
196+
197+
return this;
198+
}
199+
200+
addNestedFields(newFields: ComposeFieldConfigMap<any, any>): this {
201+
super.addNestedFields(newFields);
202+
203+
for (const childTC of this.childTCs) {
204+
childTC.addNestedFields(newFields);
205+
}
206+
207+
return this;
208+
}
209+
210+
removeField(fieldNameOrArray: string | Array<string>): this {
211+
super.removeField(fieldNameOrArray);
212+
213+
for (const childTC of this.childTCs) {
214+
childTC.removeField(fieldNameOrArray);
215+
}
216+
217+
return this;
218+
}
219+
220+
removeOtherFields(fieldNameOrArray: string | Array<string>): this {
221+
super.removeOtherFields(fieldNameOrArray);
222+
223+
for (const childTC of this.childTCs) {
224+
childTC.removeOtherFields(fieldNameOrArray);
225+
}
226+
227+
return this;
228+
}
229+
230+
extendField(
179231
fieldName: string,
180232
partialFieldConfig: ComposePartialFieldConfigAsObject<any, any>
181233
): this {
@@ -188,13 +240,52 @@ export class DiscriminatorTypeComposer extends TypeComposer {
188240
return this;
189241
}
190242

243+
reorderFields(names: string[]): this {
244+
super.reorderFields(names);
245+
246+
for (const childTC of this.childTCs) {
247+
childTC.reorderFields(names);
248+
}
249+
250+
return this;
251+
}
252+
253+
makeFieldNonNull(fieldNameOrArray: string | Array<string>): this {
254+
super.makeFieldNonNull(fieldNameOrArray);
255+
256+
for (const childTC of this.childTCs) {
257+
childTC.makeFieldNonNull(fieldNameOrArray);
258+
}
259+
260+
return this;
261+
}
262+
263+
makeFieldNullable(fieldNameOrArray: string | Array<string>): this {
264+
super.makeFieldNullable(fieldNameOrArray);
265+
266+
for (const childTC of this.childTCs) {
267+
childTC.makeFieldNullable(fieldNameOrArray);
268+
}
269+
270+
return this;
271+
}
272+
273+
deprecateFields(fields: { [fieldName: string]: string } | string[] | string): this {
274+
super.deprecateFields(fields);
275+
276+
for (const childTC of this.childTCs) {
277+
childTC.deprecateFields(fields);
278+
}
279+
280+
return this;
281+
}
282+
191283
// relations with args are a bit hard to manage as interfaces i believe as of now do not
192284
// support field args. Well if one wants to have use args, you setType for resolver as this
193285
// this = this DiscriminantTypeComposer
194286
// NOTE, those relations will be propagated to the childTypeComposers and you can use normally.
195-
// FixMe: Note, You must use this function after creating all discriminators
196-
addDRelation(fieldName: string, relationOpts: RelationOpts<any, any>): this {
197-
this.addRelation(fieldName, relationOpts);
287+
addRelation(fieldName: string, relationOpts: RelationOpts<any, any>): this {
288+
super.addRelation(fieldName, relationOpts);
198289

199290
for (const childTC of this.childTCs) {
200291
childTC.addRelation(fieldName, relationOpts);
@@ -203,6 +294,16 @@ export class DiscriminatorTypeComposer extends TypeComposer {
203294
return this;
204295
}
205296

297+
setRecordIdFn(fn: GetRecordIdFn<any, any>): this {
298+
super.setRecordIdFn(fn);
299+
300+
for (const childTC of this.childTCs) {
301+
childTC.setRecordIdFn(fn);
302+
}
303+
304+
return this;
305+
}
306+
206307
/* eslint no-use-before-define: 0 */
207308
discriminator(childModel: Model, opts?: TypeConverterOpts): TypeComposer {
208309
const customizationOpts = mergeCustomizationOptions(

0 commit comments

Comments
 (0)