|
| 1 | +/* @flow */ |
| 2 | +/* eslint-disable no-await-in-loop */ |
| 3 | + |
| 4 | +import mongoose from 'mongoose'; |
| 5 | +import MongodbMemoryServer from 'mongodb-memory-server'; |
| 6 | +import { schemaComposer, graphql } from 'graphql-compose'; |
| 7 | +import { composeWithMongoose } from '../../index'; |
| 8 | + |
| 9 | +let mongoServer; |
| 10 | +beforeAll(async () => { |
| 11 | + mongoServer = new MongodbMemoryServer(); |
| 12 | + const mongoUri = await mongoServer.getConnectionString(); |
| 13 | + await mongoose.connect( |
| 14 | + mongoUri, |
| 15 | + { useNewUrlParser: true } |
| 16 | + ); |
| 17 | + // mongoose.set('debug', true); |
| 18 | +}); |
| 19 | + |
| 20 | +afterAll(() => { |
| 21 | + mongoose.disconnect(); |
| 22 | + mongoServer.stop(); |
| 23 | +}); |
| 24 | + |
| 25 | +// May require additional time for downloading MongoDB binaries |
| 26 | +jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000; |
| 27 | + |
| 28 | +describe('issue #136 - Mongoose virtuals', () => { |
| 29 | + const CommentSchema = new mongoose.Schema({ |
| 30 | + author: { |
| 31 | + type: mongoose.Schema.Types.ObjectId, |
| 32 | + rel: 'Autor', |
| 33 | + }, |
| 34 | + links: [String], |
| 35 | + }); |
| 36 | + |
| 37 | + const Comment = mongoose.model('Comment', CommentSchema); |
| 38 | + const CommentTC = composeWithMongoose(Comment); |
| 39 | + |
| 40 | + CommentTC.wrapResolverAs('createManyFiltered', 'createMany', updateManyFiltered => { |
| 41 | + const recordsTC = CommentTC.getResolver('createMany').getArgTC('records'); |
| 42 | + const clonedRecordTC = recordsTC.clone('createManyFilteredInput'); |
| 43 | + clonedRecordTC.removeField('links').addFields({ hi: 'String' }); |
| 44 | + updateManyFiltered.extendArg('records', { type: clonedRecordTC.getTypePlural() }); |
| 45 | + |
| 46 | + return updateManyFiltered |
| 47 | + .wrapResolve(next => async rp => { |
| 48 | + console.log(rp.args); |
| 49 | + return next(rp); |
| 50 | + }) |
| 51 | + .debug(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('check that virtual field works', async () => { |
| 55 | + // INIT GRAPHQL SCHEMA |
| 56 | + schemaComposer.Query.addFields({ noop: 'String' }); |
| 57 | + schemaComposer.Mutation.addFields({ |
| 58 | + createCommentsFiltered: CommentTC.getResolver('createManyFiltered'), |
| 59 | + createManyComments: CommentTC.getResolver('createMany'), |
| 60 | + }); |
| 61 | + const schema = schemaComposer.buildSchema(); |
| 62 | + |
| 63 | + const res = await graphql.graphql({ |
| 64 | + schema, |
| 65 | + source: 'mutation { createManyComments(records: [{ links: ["a"] }]) { createCount } }', |
| 66 | + }); |
| 67 | + |
| 68 | + expect(res).toEqual({ data: { createManyComments: { createCount: 1 } } }); |
| 69 | + }); |
| 70 | +}); |
0 commit comments