-
-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy path78-test.js
93 lines (77 loc) · 2.8 KB
/
78-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* @flow */
import mongoose from 'mongoose';
import MongodbMemoryServer from 'mongodb-memory-server';
import { schemaComposer, graphql } from 'graphql-compose';
import { composeWithMongoose } from '../../index';
let mongoServer;
beforeAll(async () => {
mongoServer = new MongodbMemoryServer();
const mongoUri = await mongoServer.getConnectionString();
await mongoose.connect(mongoUri, { useNewUrlParser: true });
});
afterAll(() => {
mongoose.disconnect();
mongoServer.stop();
});
// May require additional time for downloading MongoDB binaries
jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000;
describe('issue #78 - Mongoose and Discriminators', () => {
const options = { discriminatorKey: 'kind' };
const eventSchema = new mongoose.Schema({ refId: String }, options);
const Event = mongoose.model('GenericEvent', eventSchema);
const clickedLinkSchema = new mongoose.Schema({ url: String }, options);
const ClickedLinkEvent = Event.discriminator('ClickedLinkEvent', clickedLinkSchema);
const EventTC = composeWithMongoose(Event);
const ClickedLinkEventTC = composeWithMongoose(ClickedLinkEvent);
it('creating Types from models', () => {
expect(EventTC.getFieldNames()).toEqual(['refId', '_id', 'kind']);
expect(ClickedLinkEventTC.getFieldNames()).toEqual(['url', '_id', 'refId', 'kind']);
});
it('manually override resolver output type for findMany', async () => {
const EventDescriminatorType = new graphql.GraphQLUnionType({
name: 'EventDescriminator',
types: [EventTC.getType(), ClickedLinkEventTC.getType()],
resolveType: value => {
if (value.kind === 'ClickedLinkEvent') {
return ClickedLinkEventTC.getType();
}
return EventTC.getType();
},
});
EventTC.getResolver('findMany').setType(new graphql.GraphQLList(EventDescriminatorType));
// let's check graphql response
await Event.create({ refId: 'aaa' });
await Event.create({ refId: 'bbb' });
await ClickedLinkEvent.create({ refId: 'ccc', url: 'url1' });
await ClickedLinkEvent.create({ refId: 'ddd', url: 'url2' });
schemaComposer.Query.addFields({
eventFindMany: EventTC.getResolver('findMany'),
});
const schema = schemaComposer.buildSchema();
const res = await graphql.graphql(
schema,
`{
eventFindMany {
__typename
... on GenericEvent {
refId
}
... on ClickedLinkEvent {
refId
url
}
}
}`
);
expect(res).toEqual({
data: {
eventFindMany: [
{ __typename: 'GenericEvent', refId: 'aaa' },
{ __typename: 'GenericEvent', refId: 'bbb' },
{ __typename: 'ClickedLinkEvent', refId: 'ccc', url: 'url1' },
{ __typename: 'ClickedLinkEvent', refId: 'ddd', url: 'url2' },
],
},
});
});
});