-
-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy path136-test.js
67 lines (56 loc) · 2.14 KB
/
136-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* @flow */
/* eslint-disable no-await-in-loop */
import mongoose from 'mongoose';
import MongodbMemoryServer from 'mongodb-memory-server';
import { schemaComposer, graphql } from 'graphql-compose';
import { composeWithMongoose } from '../../index';
let mongoServer;
beforeAll(async () => {
mongoServer = new MongodbMemoryServer();
const mongoUri = await mongoServer.getConnectionString();
await mongoose.connect(mongoUri, { useNewUrlParser: true });
// mongoose.set('debug', true);
});
afterAll(() => {
mongoose.disconnect();
mongoServer.stop();
});
// May require additional time for downloading MongoDB binaries
jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000;
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.getTypePlural() });
return updateManyFiltered
.wrapResolve(next => async rp => {
console.log(rp.args); // eslint-disable-line
return next(rp);
})
.debug();
});
it('check that virtual field works', async () => {
// INIT GRAPHQL SCHEMA
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"] }]) { createCount } }',
});
expect(res).toEqual({ data: { createManyComments: { createCount: 1 } } });
});
});