@@ -5,10 +5,12 @@ import { Document } from 'mongoose';
55import { convertSchemaToGraphQL } from '../../../src/fieldsConverter' ;
66const schemaComposer = new SchemaComposer < { req : any } > ( ) ;
77
8+ // mongoose.set('debug', true);
9+
810const AuthorSchema = new mongoose . Schema (
911 {
1012 name : { type : String } ,
11- age : { type : Number } ,
13+ ag : { type : Number , alias : 'age' } ,
1214 isAlive : { type : Boolean } ,
1315 } ,
1416 { _id : false }
@@ -17,8 +19,8 @@ const AuthorSchema = new mongoose.Schema(
1719const BookSchema = new mongoose . Schema ( {
1820 _id : { type : Number } ,
1921 title : { type : String , required : true } ,
20- pageCount : { type : Number } ,
21- author : { type : AuthorSchema } ,
22+ pc : { type : Number , alias : 'pageCount' } ,
23+ a : { type : AuthorSchema , alias : 'author' } ,
2224} ) ;
2325
2426interface IAuthor {
@@ -36,25 +38,45 @@ interface IBook extends Document {
3638
3739const BookModel = mongoose . model < IBook > ( 'Book' , BookSchema ) ;
3840
39- convertSchemaToGraphQL ( AuthorSchema , 'Author' , schemaComposer ) ;
41+ const AuthorTC = convertSchemaToGraphQL ( AuthorSchema , 'Author' , schemaComposer ) ;
42+ AuthorTC . setField ( 'isAbove100' , {
43+ type : 'String' ,
44+ resolve : ( s : IAuthor ) => {
45+ return ! s ?. age ? 'unknown' : s . age > 100 ? 'yes' : 'no' ;
46+ } ,
47+ projection : { age : true } ,
48+ } ) ;
4049
4150const BookTC = composeMongoose ( BookModel , { schemaComposer } ) ;
51+ BookTC . setField ( 'bookSize' , {
52+ type : 'String' ,
53+ resolve : ( s : IBook ) => {
54+ return ! s ?. pageCount ? 'unknown' : s . pageCount > 500 ? 'big' : 'small' ;
55+ } ,
56+ projection : { pageCount : true } ,
57+ } ) ;
4258
59+ let lastExecutedProjection : Record < string , true > ;
4360schemaComposer . Query . addFields ( {
44- bookById : BookTC . mongooseResolvers . findById ( ) ,
45- bookFindOne : BookTC . mongooseResolvers . findOne ( ) ,
46- booksMany : BookTC . mongooseResolvers . findMany ( ) ,
61+ booksMany : BookTC . mongooseResolvers . findMany ( ) . wrapResolve ( ( next ) => async ( rp ) => {
62+ const res = await next ( rp ) ;
63+ lastExecutedProjection = rp . query . _fields ;
64+ return res ;
65+ } ) ,
4766} ) ;
4867
4968const schema = schemaComposer . buildSchema ( ) ;
5069
5170beforeAll ( async ( ) => {
52- mongoose . set ( 'debug' , true ) ;
5371 await BookModel . base . createConnection ( ) ;
5472 await BookModel . create ( {
5573 _id : 1 ,
5674 title : 'Atlas Shrugged' ,
57- author : { age : new Date ( ) . getFullYear ( ) - 1905 , isAlive : false , name : 'Ayn Rand' } ,
75+ author : {
76+ age : 115 ,
77+ isAlive : false ,
78+ name : 'Ayn Rand' ,
79+ } ,
5880 pageCount : 1168 ,
5981 } ) ;
6082} ) ;
@@ -63,7 +85,28 @@ afterAll(() => {
6385 BookModel . base . disconnect ( ) ;
6486} ) ;
6587
66- describe ( 'nested projection - issue #271' , ( ) => {
88+ describe ( 'nested projections with aliases - issue #271' , ( ) => {
89+ it ( 'check mongoose itself' , async ( ) => {
90+ const book = (
91+ await BookModel . find ( { } ) . select ( {
92+ bookSize : true ,
93+ 'a.isAbove100' : true ,
94+ 'a.ag' : true ,
95+ pc : true ,
96+ } )
97+ ) [ 0 ] ;
98+ expect ( book ?. pageCount ) . toEqual ( 1168 ) ;
99+ expect ( book ?. author ?. age ) . toEqual ( 115 ) ;
100+ expect ( book ?. toObject ( { virtuals : true } ) ) . toEqual ( {
101+ _id : 1 ,
102+ a : { ag : 115 , age : 115 , id : null } ,
103+ author : { ag : 115 , age : 115 , id : null } ,
104+ id : '1' ,
105+ pageCount : 1168 ,
106+ pc : 1168 ,
107+ } ) ;
108+ } ) ;
109+
67110 it ( 'Happy Path' , async ( ) => {
68111 const result = await graphql . graphql ( {
69112 schema,
@@ -76,7 +119,12 @@ describe('nested projection - issue #271', () => {
76119 }` ,
77120 contextValue : { } ,
78121 } ) ;
79- console . log ( JSON . stringify ( result ) ) ;
122+ expect ( lastExecutedProjection ) . toEqual ( { 'a.name' : true , pc : true , title : true } ) ;
123+ expect ( result ) . toEqual ( {
124+ data : {
125+ booksMany : [ { author : { name : 'Ayn Rand' } , pageCount : 1168 , title : 'Atlas Shrugged' } ] ,
126+ } ,
127+ } ) ;
80128 } ) ;
81129
82130 it ( 'Handles a Fragment' , async ( ) => {
@@ -90,12 +138,41 @@ describe('nested projection - issue #271', () => {
90138 query {
91139 booksMany {
92140 title
93- pageCount
94141 author { ...Auth }
95142 }
96143 }` ,
97144 contextValue : { } ,
98145 } ) ;
99- console . log ( JSON . stringify ( result ) ) ;
146+ expect ( lastExecutedProjection ) . toEqual ( { 'a.name' : true , title : true } ) ;
147+ expect ( result ) . toEqual ( {
148+ data : {
149+ booksMany : [ { author : { name : 'Ayn Rand' } , title : 'Atlas Shrugged' } ] ,
150+ } ,
151+ } ) ;
152+ } ) ;
153+
154+ it ( 'Handles computable fields which uses `projection` property' , async ( ) => {
155+ const result = await graphql . graphql ( {
156+ schema,
157+ source : `
158+ query {
159+ booksMany {
160+ bookSize
161+ author {
162+ isAbove100
163+ }
164+ }
165+ }` ,
166+ contextValue : { } ,
167+ } ) ;
168+ expect ( lastExecutedProjection ) . toEqual ( {
169+ bookSize : true ,
170+ 'a.isAbove100' : true ,
171+ 'a.ag' : true ,
172+ pc : true ,
173+ } ) ;
174+ expect ( result ) . toEqual ( {
175+ data : { booksMany : [ { author : { isAbove100 : 'yes' } , bookSize : 'big' } ] } ,
176+ } ) ;
100177 } ) ;
101178} ) ;
0 commit comments