Skip to content

Commit 5500456

Browse files
committed
refactor: _disTypes replaced via new schemaComposer.addSchemaMustHaveType() method
1 parent 1634944 commit 5500456

File tree

4 files changed

+7
-145
lines changed

4 files changed

+7
-145
lines changed

src/__tests__/_disTypes-test.js

Lines changed: 4 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('_disTypes Test With and Without test_disTypes Options set', () => {
1616

1717
afterEach(() => Event.remove({}));
1818

19-
describe('No test_disTypes, Include types Manually in Query', () => {
19+
describe('opts test_disTypes FALSE', () => {
2020
let EventTC;
2121
let ClickedLinkEventTC;
2222

@@ -31,67 +31,6 @@ describe('_disTypes Test With and Without test_disTypes Options set', () => {
3131
expect(ClickedLinkEventTC.getFieldNames()).toEqual(['_id', 'kind', 'refId', 'url']);
3232
});
3333

34-
it('manually override resolver output type for findMany', async () => {
35-
// let's check graphql response
36-
37-
await Event.create({ refId: 'aaa' });
38-
await Event.create({ refId: 'bbb' });
39-
await ClickedLinkEvent.create({ refId: 'ccc', url: 'url1' });
40-
await ClickedLinkEvent.create({ refId: 'ddd', url: 'url2' });
41-
42-
schemaComposer.rootQuery().addFields({
43-
eventFindMany: EventTC.getResolver('findMany'),
44-
ClickedLinkEvent: ClickedLinkEventTC.getResolver('findMany'),
45-
GenericEvent: EventTC.getResolver('findOne').setType(EventTC),
46-
});
47-
48-
const schema = schemaComposer.buildSchema();
49-
50-
const res = await graphql.graphql(
51-
schema,
52-
`{
53-
eventFindMany {
54-
__typename
55-
... on GenericEvent {
56-
refId
57-
}
58-
... on ClickedLinkEvent {
59-
kind
60-
refId
61-
url
62-
}
63-
}
64-
}`
65-
);
66-
67-
expect(res).toEqual({
68-
data: {
69-
eventFindMany: [
70-
{ __typename: 'GenericEvent', refId: 'aaa' },
71-
{ __typename: 'GenericEvent', refId: 'bbb' },
72-
{ __typename: 'ClickedLinkEvent', kind: 'ClickedLinkEvent', refId: 'ccc', url: 'url1' },
73-
{ __typename: 'ClickedLinkEvent', kind: 'ClickedLinkEvent', refId: 'ddd', url: 'url2' },
74-
],
75-
},
76-
});
77-
});
78-
});
79-
80-
describe('opts test_disTypes TRUE', () => {
81-
let EventTC;
82-
let ClickedLinkEventTC;
83-
84-
beforeAll(() => {
85-
schemaComposer.clear();
86-
EventTC = composeWithMongooseDiscriminators(Event, { test_disTypes: true });
87-
ClickedLinkEventTC = EventTC.discriminator(ClickedLinkEvent);
88-
});
89-
90-
it('creating Types from models', () => {
91-
expect(EventTC.getFieldNames()).toEqual(['_id', 'kind', 'refId']);
92-
expect(ClickedLinkEventTC.getFieldNames()).toEqual(['_id', 'kind', 'refId', 'url']);
93-
});
94-
9534
it('manually override resolver output type for findMany', async () => {
9635
// let's check graphql response
9736

@@ -111,7 +50,7 @@ describe('_disTypes Test With and Without test_disTypes Options set', () => {
11150
`{
11251
eventFindMany {
11352
__typename
114-
... on GenericEvent {
53+
... on Event {
11554
refId
11655
}
11756
... on ClickedLinkEvent {
@@ -126,64 +65,13 @@ describe('_disTypes Test With and Without test_disTypes Options set', () => {
12665
expect(res).toEqual({
12766
data: {
12867
eventFindMany: [
129-
{ __typename: 'GenericEvent', refId: 'aaa' },
130-
{ __typename: 'GenericEvent', refId: 'bbb' },
68+
{ __typename: 'Event', refId: 'aaa' },
69+
{ __typename: 'Event', refId: 'bbb' },
13170
{ __typename: 'ClickedLinkEvent', kind: 'ClickedLinkEvent', refId: 'ccc', url: 'url1' },
13271
{ __typename: 'ClickedLinkEvent', kind: 'ClickedLinkEvent', refId: 'ddd', url: 'url2' },
13372
],
13473
},
13574
});
13675
});
13776
});
138-
139-
describe('opts test_disTypes FALSE', () => {
140-
let EventTC;
141-
let ClickedLinkEventTC;
142-
143-
beforeAll(() => {
144-
schemaComposer.clear();
145-
EventTC = composeWithMongooseDiscriminators(Event, { test_disTypes: false });
146-
ClickedLinkEventTC = EventTC.discriminator(ClickedLinkEvent);
147-
});
148-
149-
it('creating Types from models', () => {
150-
expect(EventTC.getFieldNames()).toEqual(['_id', 'kind', 'refId']);
151-
expect(ClickedLinkEventTC.getFieldNames()).toEqual(['_id', 'kind', 'refId', 'url']);
152-
});
153-
154-
it('manually override resolver output type for findMany', async () => {
155-
// let's check graphql response
156-
157-
await Event.create({ refId: 'aaa' });
158-
await Event.create({ refId: 'bbb' });
159-
await ClickedLinkEvent.create({ refId: 'ccc', url: 'url1' });
160-
await ClickedLinkEvent.create({ refId: 'ddd', url: 'url2' });
161-
162-
schemaComposer.rootQuery().addFields({
163-
eventFindMany: EventTC.getResolver('findMany'),
164-
});
165-
166-
const schema = schemaComposer.buildSchema();
167-
168-
const res = await graphql.graphql(
169-
schema,
170-
`{
171-
eventFindMany {
172-
__typename
173-
... on GenericEvent {
174-
refId
175-
}
176-
... on ClickedLinkEvent {
177-
kind
178-
refId
179-
url
180-
}
181-
}
182-
}`
183-
);
184-
185-
expect(res.errors[0].message).toEqual('Unknown type "GenericEvent".');
186-
expect(res.errors[1].message).toEqual('Unknown type "ClickedLinkEvent".');
187-
});
188-
});
18977
});

src/__tests__/integration-discriminators-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('#78 Mongoose and Discriminators', () => {
1717
const clickedLinkSchema = new mongoose.Schema({ url: String });
1818
const ClickedLinkEvent = Event.discriminator('ClickedLinkEvent', clickedLinkSchema);
1919

20-
const EventTC = composeWithMongooseDiscriminators(Event, { test_disTypes: true });
20+
const EventTC = composeWithMongooseDiscriminators(Event);
2121
const ClickedLinkEventTC = EventTC.discriminator(ClickedLinkEvent);
2222

2323
// Todo: Remove

src/composeWithMongooseDiscriminators.js

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -154,23 +154,7 @@ export class DiscriminatorTypeComposer extends TypeComposer {
154154
this.DInterface = createDInterface(this);
155155
this.setInterfaces([this.DInterface]);
156156

157-
// Hoist on _disTypes
158-
if (opts.test_disTypes) {
159-
if (!this.GQC.rootQuery().hasField('_disTypes')) {
160-
this.GQC.rootQuery().addFields({
161-
_disTypes: TypeComposer.create({
162-
name: '_disTypes',
163-
description: 'Hoisting for Discriminator Types',
164-
}),
165-
});
166-
}
167-
168-
this.GQC.rootQuery()
169-
.getFieldTC('_disTypes')
170-
.addFields({
171-
[this.getTypeName()]: this,
172-
});
173-
}
157+
this.schemaComposer.addSchemaMustHaveType(this);
174158

175159
// prepare Base Resolvers
176160
prepareBaseResolvers(this);
@@ -246,6 +230,7 @@ export class DiscriminatorTypeComposer extends TypeComposer {
246230

247231
childTC = composeChildTC(this, childTC, this.opts);
248232

233+
this.schemaComposer.addSchemaMustHaveType(childTC);
249234
this.childTCs.push(childTC);
250235

251236
return childTC;

src/discriminators/composeChildTC.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,6 @@ export function composeChildTC(
3535

3636
composedChildTC.setInterfaces([baseDTC.getDInterface()]);
3737

38-
// hoist this type
39-
if (opts.test_disTypes) {
40-
baseDTC
41-
.getGQC()
42-
.rootQuery()
43-
.getFieldTC('_disTypes')
44-
.addFields({
45-
[composedChildTC.getTypeName()]: composedChildTC,
46-
});
47-
}
48-
4938
prepareChildResolvers(baseDTC, composedChildTC, opts);
5039

5140
reorderFields(

0 commit comments

Comments
 (0)