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 #136 - Mongoose virtuals' , ( ) => { const CommentSchema = new mongoose . Schema ( { author : { type : mongoose . Schema . Types . ObjectId , rel : 'Autor' , } , links : [ String ] , } ) ; const Comment = mongoose . model ( 'Comment' , CommentSchema ) ; const CommentTC = composeWithMongoose ( Comment ) ; CommentTC . wrapResolverAs ( 'createManyFiltered' , 'createMany' , ( updateManyFiltered ) => { const recordsTC = CommentTC . getResolver ( 'createMany' ) . getArgITC ( 'records' ) ; const clonedRecordTC = recordsTC . clone ( 'createManyFilteredInput' ) ; clonedRecordTC . removeField ( 'links' ) . addFields ( { hi : 'String' } ) ; updateManyFiltered . extendArg ( 'records' , { type : clonedRecordTC . List } ) ; return updateManyFiltered . wrapResolve ( ( next ) => async ( rp ) => { console . log ( rp . args ) ; return next ( rp ) ; } ) . debug ( ) ; } ) ; it ( 'check that virtual field works' , async ( ) => { schemaComposer . Query . addFields ( { noop : 'String' } ) ; schemaComposer . Mutation . addFields ( { createCommentsFiltered : CommentTC . getResolver ( 'createManyFiltered' ) , createManyComments : CommentTC . getResolver ( 'createMany' ) , } ) ; const schema = schemaComposer . buildSchema ( ) ; const res = await graphql . graphql ( { schema , source : 'mutation { createManyComments(records: [{ links: ["a"] }]) { createdCount } }' , } ) ; expect ( res ) . toEqual ( { data : { createManyComments : { createdCount : 1 } } } ) ; } ) ; } ) ;