11/* @flow */
22
33import mongoose from 'mongoose' ;
4+ import MongodbMemoryServer from 'mongodb-memory-server' ;
5+ import { graphql , GQC } from 'graphql-compose' ;
46import { composeWithMongoose } from '../index' ;
57
8+ // May require additional time for downloading MongoDB binaries
9+ jasmine . DEFAULT_TIMEOUT_INTERVAL = 60000 ;
10+
11+ let mongoServer ;
12+ const opts = { useMongoClient : true } ;
13+
14+ beforeAll ( async ( ) => {
15+ mongoServer = new MongodbMemoryServer ( ) ;
16+ const mongoUri = await mongoServer . getConnectionString ( ) ;
17+ mongoose . connect ( mongoUri , opts , err => {
18+ if ( err ) console . error ( err ) ;
19+ } ) ;
20+ } ) ;
21+
22+ afterAll ( ( ) => {
23+ mongoose . disconnect ( ) ;
24+ mongoServer . stop ( ) ;
25+ } ) ;
26+
627describe ( 'github issues checks' , ( ) => {
7- it ( '#78 Mongoose and Discriminators' , ( ) => {
28+ describe ( '#78 Mongoose and Discriminators' , ( ) => {
829 const options = { discriminatorKey : 'kind' } ;
930
10- const eventSchema = new mongoose . Schema ( { time : Date } , options ) ;
31+ const eventSchema = new mongoose . Schema ( { refId : String } , options ) ;
1132 const Event = mongoose . model ( 'GenericEvent' , eventSchema ) ;
1233
1334 const clickedLinkSchema = new mongoose . Schema ( { url : String } , options ) ;
@@ -16,7 +37,63 @@ describe('github issues checks', () => {
1637 const EventTC = composeWithMongoose ( Event ) ;
1738 const ClickedLinkEventTC = composeWithMongoose ( ClickedLinkEvent ) ;
1839
19- expect ( EventTC . getFieldNames ( ) ) . toEqual ( [ 'time' , '_id' , 'kind' ] ) ;
20- expect ( ClickedLinkEventTC . getFieldNames ( ) ) . toEqual ( [ 'url' , '_id' , 'time' , 'kind' ] ) ;
40+ it ( 'creating Types from models' , ( ) => {
41+ expect ( EventTC . getFieldNames ( ) ) . toEqual ( [ 'refId' , '_id' , 'kind' ] ) ;
42+ expect ( ClickedLinkEventTC . getFieldNames ( ) ) . toEqual ( [ 'url' , '_id' , 'refId' , 'kind' ] ) ;
43+ } ) ;
44+
45+ it ( 'manually override resolver output type for findMany' , async ( ) => {
46+ const EventDescriminatorType = new graphql . GraphQLUnionType ( {
47+ name : 'EventDescriminator' ,
48+ types : [ EventTC . getType ( ) , ClickedLinkEventTC . getType ( ) ] ,
49+ resolveType : value => {
50+ if ( value . kind === 'ClickedLinkEvent' ) {
51+ return ClickedLinkEventTC . getType ( ) ;
52+ }
53+ return EventTC . getType ( ) ;
54+ } ,
55+ } ) ;
56+
57+ EventTC . getResolver ( 'findMany' ) . setType ( new graphql . GraphQLList ( EventDescriminatorType ) ) ;
58+
59+ // let's check graphql response
60+
61+ await Event . create ( { refId : 'aaa' } ) ;
62+ await Event . create ( { refId : 'bbb' } ) ;
63+ await ClickedLinkEvent . create ( { refId : 'ccc' , url : 'url1' } ) ;
64+ await ClickedLinkEvent . create ( { refId : 'ddd' , url : 'url2' } ) ;
65+
66+ GQC . rootQuery ( ) . addFields ( {
67+ eventFindMany : EventTC . getResolver ( 'findMany' ) ,
68+ } ) ;
69+ const schema = GQC . buildSchema ( ) ;
70+
71+ const res = await graphql . graphql (
72+ schema ,
73+ `{
74+ eventFindMany {
75+ __typename
76+ ... on GenericEvent {
77+ refId
78+ }
79+ ... on ClickedLinkEvent {
80+ refId
81+ url
82+ }
83+ }
84+ }`
85+ ) ;
86+
87+ expect ( res ) . toEqual ( {
88+ data : {
89+ eventFindMany : [
90+ { __typename : 'GenericEvent' , refId : 'aaa' } ,
91+ { __typename : 'GenericEvent' , refId : 'bbb' } ,
92+ { __typename : 'ClickedLinkEvent' , refId : 'ccc' , url : 'url1' } ,
93+ { __typename : 'ClickedLinkEvent' , refId : 'ddd' , url : 'url2' } ,
94+ ] ,
95+ } ,
96+ } ) ;
97+ } ) ;
2198 } ) ;
2299} ) ;
0 commit comments