import { schemaComposer , graphql } from 'graphql-compose' ; import { composeWithMongoose } from '../../index' ; import { UserModel } from '../../__mocks__/userModel' ; beforeAll ( async ( ) => { await UserModel . base . createConnection ( ) ; await UserModel . create ( { name : 'AAAAA' , age : 10 , contacts : { email : '1@1.com' } , } ) ; await UserModel . create ( { name : 'BBBBB' , age : 20 , contacts : { email : '2@2.com' } , } ) ; } ) ; afterAll ( ( ) => UserModel . base . disconnect ( ) ) ; const UserTC = composeWithMongoose ( UserModel ) ; describe ( 'issue #219 - Authorization using wrapResolve' , ( ) => { it ( 'correct request' , async ( ) => { UserTC . wrapResolverResolve ( 'findOne' , ( next ) => ( rp ) => { rp . beforeQuery = async ( query ) => { query = query . where ( { age : { $gt : 19 } } ) ; if ( rp ?. args ?. filter ?. name === 'CACHED' ) { return { name : 'CACHED' , age : 99 } ; } if ( rp ?. args ?. filter ?. name === 'ERROR' ) { throw new Error ( 'Wrong arg!' ) ; } return query . exec ( ) ; } ; return next ( rp ) ; } ) ; schemaComposer . Query . addFields ( { findUser : UserTC . getResolver ( 'findOne' ) , } ) ; const schema = schemaComposer . buildSchema ( ) ; expect ( await graphql . graphql ( { schema , source : `query {
          findUser(filter: { name: "AAAAA" }) {
            name
            age
          }
        }` , } ) ) . toEqual ( { data : { findUser : null } } ) ; expect ( await graphql . graphql ( { schema , source : `query {
          findUser(filter: { name: "BBBBB" }) {
            name
            age
          }
        }` , } ) ) . toEqual ( { data : { findUser : { age : 20 , name : 'BBBBB' } } } ) ; expect ( await graphql . graphql ( { schema , source : `query {
          findUser(filter: { name: "CACHED" }) {
            name
            age
          }
        }` , } ) ) . toEqual ( { data : { findUser : { age : 99 , name : 'CACHED' } } } ) ; expect ( await graphql . graphql ( { schema , source : `query {
          findUser(filter: { name: "ERROR" }) {
            name
            age
          }
        }` , } ) ) . toEqual ( { data : { findUser : null } , errors : [ new Error ( 'Wrong arg!' ) ] } ) ; } ) ; } ) ;