File tree 3 files changed +23
-10
lines changed
3 files changed +23
-10
lines changed Original file line number Diff line number Diff line change @@ -75,17 +75,19 @@ describe('Mongoose', () => {
75
75
expect ( getCollection ( UserModel ) . collectionName ) . toBe ( 'users' )
76
76
} )
77
77
78
- test ( 'data source ' , async ( ) => {
78
+ test ( 'Data Source with Model ' , async ( ) => {
79
79
const users = new Users ( UserModel )
80
80
users . initialize ( )
81
81
const user = await users . findOneById ( alice . _id )
82
82
expect ( user . name ) . toBe ( 'Alice' )
83
+ expect ( user . id ) . toBe ( alice . _id . toString ( ) )
83
84
} )
84
85
85
- test ( 'collection ' , async ( ) => {
86
+ test ( 'Data Source with Collection ' , async ( ) => {
86
87
const users = new Users ( userCollection )
87
88
users . initialize ( )
88
89
const user = await users . findOneById ( alice . _id )
89
90
expect ( user . name ) . toBe ( 'Alice' )
91
+ expect ( user . id ) . toBeUndefined ( )
90
92
} )
91
93
} )
Original file line number Diff line number Diff line change 1
1
import DataLoader from 'dataloader'
2
2
3
- import { getCollection } from './helpers'
3
+ import { getCollection , isModel } from './helpers'
4
4
5
5
// https://github.com/graphql/dataloader#batch-function
6
6
const orderDocs = ids => docs => {
@@ -12,12 +12,23 @@ const orderDocs = ids => docs => {
12
12
}
13
13
14
14
export const createCachingMethods = ( { collection, cache } ) => {
15
- const loader = new DataLoader ( ids =>
16
- collection
17
- . find ( { _id : { $in : ids } } )
18
- . toArray ( )
19
- . then ( orderDocs ( ids ) )
20
- )
15
+ const loader = new DataLoader ( ( ids ) => {
16
+ const res = collection . find ( {
17
+ _id : {
18
+ $in : ids ,
19
+ } ,
20
+ } ) ;
21
+
22
+ let promise ;
23
+
24
+ if ( isModel ( collection ) ) {
25
+ promise = res . exec ( ) ;
26
+ } else {
27
+ promise = res . toArray ( ) ;
28
+ }
29
+
30
+ return promise . then ( orderDocs ( ids ) ) ;
31
+ } ) ;
21
32
22
33
const cachePrefix = `mongo-${ getCollection ( collection ) . collectionName } -`
23
34
Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ class MongoDataSource extends DataSource {
28
28
this . context = context
29
29
30
30
const methods = createCachingMethods ( {
31
- collection : this . collection ,
31
+ collection : this . model || this . collection ,
32
32
cache : cache || new InMemoryLRUCache ( )
33
33
} )
34
34
You can’t perform that action at this time.
0 commit comments