22
33import mongoose from 'mongoose' ;
44import MongodbMemoryServer from 'mongodb-memory-server' ;
5- import { graphql , GQC } from 'graphql-compose' ;
5+ import { schemaComposer , graphql } from 'graphql-compose' ;
66import { composeWithMongoose } from '../index' ;
7+ import { UserModel } from '../__mocks__/userModel' ;
78
89// May require additional time for downloading MongoDB binaries
910jasmine . DEFAULT_TIMEOUT_INTERVAL = 60000 ;
@@ -26,6 +27,11 @@ afterAll(() => {
2627 mongoServer . stop ( ) ;
2728} ) ;
2829
30+ const UserTC = composeWithMongoose ( UserModel ) ;
31+ schemaComposer . rootQuery ( ) . addFields ( {
32+ users : UserTC . getResolver ( 'findMany' ) ,
33+ } ) ;
34+
2935describe ( 'github issues checks' , ( ) => {
3036 describe ( '#78 Mongoose and Discriminators' , ( ) => {
3137 const options = { discriminatorKey : 'kind' } ;
@@ -65,10 +71,10 @@ describe('github issues checks', () => {
6571 await ClickedLinkEvent . create ( { refId : 'ccc' , url : 'url1' } ) ;
6672 await ClickedLinkEvent . create ( { refId : 'ddd' , url : 'url2' } ) ;
6773
68- GQC . rootQuery ( ) . addFields ( {
74+ schemaComposer . rootQuery ( ) . addFields ( {
6975 eventFindMany : EventTC . getResolver ( 'findMany' ) ,
7076 } ) ;
71- const schema = GQC . buildSchema ( ) ;
77+ const schema = schemaComposer . buildSchema ( ) ;
7278
7379 const res = await graphql . graphql (
7480 schema ,
@@ -98,4 +104,52 @@ describe('github issues checks', () => {
98104 } ) ;
99105 } ) ;
100106 } ) ;
107+
108+ describe ( '#92 How to verify the fields?' , async ( ) => {
109+ UserTC . wrapResolverResolve ( 'createOne' , next => rp => {
110+ if ( rp . args . record . age < 21 ) throw new Error ( 'You are too young' ) ;
111+ if ( rp . args . record . age > 60 ) throw new Error ( 'You are too old' ) ;
112+ return next ( rp ) ;
113+ } ) ;
114+
115+ schemaComposer . rootMutation ( ) . addFields ( {
116+ addUser : UserTC . getResolver ( 'createOne' ) ,
117+ } ) ;
118+ const schema = schemaComposer . buildSchema ( ) ;
119+
120+ it ( 'correct request' , async ( ) => {
121+ const result : any = await graphql . graphql (
122+ schema ,
123+ `
124+ mutation {
125+ addUser(record: { name: "User1", age: 30 }) {
126+ record {
127+ name
128+ age
129+ }
130+ }
131+ }
132+ `
133+ ) ;
134+ expect ( result ) . toEqual ( { data : { addUser : { record : { age : 30 , name : 'User1' } } } } ) ;
135+ } ) ;
136+
137+ it ( 'wrong request' , async ( ) => {
138+ const result : any = await graphql . graphql (
139+ schema ,
140+ `
141+ mutation {
142+ addUser(record: { name: "User1", age: 10 }) {
143+ record {
144+ name
145+ age
146+ }
147+ }
148+ }
149+ `
150+ ) ;
151+ expect ( result ) . toEqual ( { data : { addUser : null } , errors : expect . anything ( ) } ) ;
152+ expect ( result . errors [ 0 ] . message ) . toBe ( 'You are too young' ) ;
153+ } ) ;
154+ } ) ;
101155} ) ;
0 commit comments