@@ -11,10 +11,13 @@ afterAll(() => mongoose.disconnect());
11
11
describe ( '#78 Mongoose and Discriminators' , ( ) => {
12
12
const options = { discriminatorKey : 'kind' } ;
13
13
14
- const eventSchema = new mongoose . Schema ( { refId : String } , options ) ;
14
+ const eventSchema = new mongoose . Schema (
15
+ { refId : String , name : { type : String , index : true } } ,
16
+ options
17
+ ) ;
15
18
const Event = mongoose . model ( 'Event' , eventSchema ) ;
16
19
17
- const clickedLinkSchema = new mongoose . Schema ( { url : String } ) ;
20
+ const clickedLinkSchema = new mongoose . Schema ( { url : { type : String , index : true } } , options ) ;
18
21
const ClickedLinkEvent = Event . discriminator ( 'ClickedLinkEvent' , clickedLinkSchema ) ;
19
22
20
23
const EventTC = composeWithMongooseDiscriminators ( Event ) ;
@@ -23,13 +26,48 @@ describe('#78 Mongoose and Discriminators', () => {
23
26
afterAll ( ( ) => Event . deleteMany ( { } ) ) ;
24
27
25
28
it ( 'creating Types from models' , ( ) => {
26
- expect ( EventTC . getFieldNames ( ) ) . toEqual ( [ '_id' , 'kind' , 'refId' ] ) ;
27
- expect ( ClickedLinkEventTC . getFieldNames ( ) ) . toEqual ( [ '_id' , 'kind' , 'refId' , 'url' ] ) ;
29
+ expect ( EventTC . getFieldNames ( ) ) . toEqual ( [ '_id' , 'kind' , 'refId' , 'name' ] ) ;
30
+ expect ( ClickedLinkEventTC . getFieldNames ( ) ) . toEqual ( [ '_id' , 'kind' , 'refId' , 'name' , ' url'] ) ;
28
31
} ) ;
29
32
30
- it ( 'manually override resolver output type for findMany ' , async ( ) => {
33
+ it ( 'perform filter operation on a child model ' , async ( ) => {
31
34
// let's check graphql response
35
+ await Event . deleteMany ( { } ) ;
36
+ await Event . create ( { refId : 'aaa' , name : 'aName' } ) ;
37
+ await Event . create ( { refId : 'bbb' , name : 'bName' } ) ;
38
+ await ClickedLinkEvent . create ( { refId : 'ccc' , name : 'cName' , url : 'url1' } ) ;
39
+ await ClickedLinkEvent . create ( { refId : 'ddd' , name : 'dName' , url : 'url2' } ) ;
40
+
41
+ schemaComposer . Query . addFields ( {
42
+ clickedLinkEventFindMany : ClickedLinkEventTC . getResolver ( 'findMany' ) ,
43
+ } ) ;
44
+
45
+ const schema = schemaComposer . buildSchema ( ) ;
32
46
47
+ const res = await graphql . graphql (
48
+ schema ,
49
+ `{
50
+ clickedLinkEventFindMany( filter: { AND: [ { _operators: { url: { in: [ "url1", "url2" ] } } }, { name: "dName" } ] }) {
51
+ __typename
52
+ refId
53
+ name
54
+ url
55
+ }
56
+ }`
57
+ ) ;
58
+
59
+ expect ( res ) . toEqual ( {
60
+ data : {
61
+ clickedLinkEventFindMany : [
62
+ { __typename : 'ClickedLinkEvent' , refId : 'ddd' , name : 'dName' , url : 'url2' } ,
63
+ ] ,
64
+ } ,
65
+ } ) ;
66
+ } ) ;
67
+
68
+ it ( 'manually override resolver output type for findMany' , async ( ) => {
69
+ // let's check graphql response
70
+ await Event . deleteMany ( { } ) ;
33
71
await Event . create ( { refId : 'aaa' } ) ;
34
72
await Event . create ( { refId : 'bbb' } ) ;
35
73
await ClickedLinkEvent . create ( { refId : 'ccc' , url : 'url1' } ) ;
0 commit comments