import mongoose from 'mongoose' ; import { MongoMemoryServer } from 'mongodb-memory-server' ; import { schemaComposer , graphql } from 'graphql-compose' ; import { composeWithMongoose } from '../../index' ; let mongoServer ; beforeAll ( async ( ) => { mongoServer = await MongoMemoryServer . create ( ) ; const mongoUri = mongoServer . getUri ( ) ; await mongoose . connect ( mongoUri , { useNewUrlParser : true , useUnifiedTopology : true , } as any ) ; } ) ; afterAll ( ( ) => { mongoose . disconnect ( ) ; mongoServer . stop ( ) ; } ) ; describe ( 'issue #135 - Mongoose virtuals' , ( ) => { const RecordSchema = new mongoose . Schema ( { id : String , title : String } ) ; class RecordDoc { get virtualField123 ( ) { return `Improved ${ ( this as any ) . title }` ; } } RecordSchema . loadClass ( RecordDoc ) ; const Record = mongoose . model ( 'Record' , RecordSchema ) ; beforeAll ( async ( ) => { for ( let i = 1 ; i <= 3 ; i ++ ) { await Record . create ( { _id : `10000000000000000000000${ i }` , title : `Title ${ i }` } ) ; } } ) ; const RecordTC = composeWithMongoose ( Record ) ; RecordTC . addFields ( { virtualField123 : { type : 'String' , } , } ) ; schemaComposer . Query . addFields ( { findMany : RecordTC . getResolver ( 'findMany' ) , } ) ; const schema = schemaComposer . buildSchema ( ) ; it ( 'check that virtual field works' , async ( ) => { const res = await graphql . graphql ( { schema , source : 'query { findMany { id title virtualField123 } }' , } ) ; expect ( res ) . toEqual ( { data : { findMany : [ { id : null , title : 'Title 1' , virtualField123 : 'Improved Title 1' } , { id : null , title : 'Title 2' , virtualField123 : 'Improved Title 2' } , { id : null , title : 'Title 3' , virtualField123 : 'Improved Title 3' } , ] , } , } ) ; } ) ; } ) ;