-
-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy path92-test.js
64 lines (56 loc) · 1.75 KB
/
92-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
/* @flow */
import { schemaComposer, graphql } from 'graphql-compose';
import { composeWithMongoose } from '../../index';
import { UserModel } from '../../__mocks__/userModel';
// May require additional time for downloading MongoDB binaries
jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000;
beforeAll(() => UserModel.base.connect());
afterAll(() => UserModel.base.disconnect());
const UserTC = composeWithMongoose(UserModel);
schemaComposer.Query.addFields({
users: UserTC.getResolver('findMany'),
});
describe('issue #92 - How to verify the fields?', () => {
UserTC.wrapResolverResolve('createOne', next => rp => {
if (rp.args.record.age < 21) throw new Error('You are too young');
if (rp.args.record.age > 60) throw new Error('You are too old');
return next(rp);
});
schemaComposer.Mutation.addFields({
addUser: UserTC.getResolver('createOne'),
});
const schema = schemaComposer.buildSchema();
it('correct request', async () => {
const result: any = await graphql.graphql(
schema,
`
mutation {
addUser(record: { name: "User1", age: 30 }) {
record {
name
age
}
}
}
`
);
expect(result).toEqual({ data: { addUser: { record: { age: 30, name: 'User1' } } } });
});
it('wrong request', async () => {
const result: any = await graphql.graphql(
schema,
`
mutation {
addUser(record: { name: "User1", age: 10 }) {
record {
name
age
}
}
}
`
);
expect(result).toEqual({ data: { addUser: null }, errors: expect.anything() });
expect(result.errors[0].message).toBe('You are too young');
});
});