File tree Expand file tree Collapse file tree 1 file changed +8
-3
lines changed Expand file tree Collapse file tree 1 file changed +8
-3
lines changed Original file line number Diff line number Diff line change 1
1
const hash = require ( 'object-hash' ) ;
2
2
3
- function cache ( func ) {
3
+ function cache ( func , options ) {
4
4
return ( root , args , context ) => {
5
5
if ( ! context . resolverCache ) {
6
6
throw new Error ( 'Missing resolverCache property on the Graphql context.' ) ;
7
7
}
8
8
9
9
const key = `${ hash ( root ) } :${ hash ( args ) } ` ;
10
+ const executeAndCache = ( ) =>
11
+ Promise . resolve ( func ( root , args , context ) ) . then ( value =>
12
+ context . resolverCache . set ( key , value , options )
13
+ ) ;
14
+
10
15
return (
11
16
context . resolverCache
12
17
. get ( key )
13
- . then ( result => ( result ? result : func ( root , args , context ) ) )
18
+ . then ( value => ( value ? value : executeAndCache ( ) ) )
14
19
// Error
15
- . catch ( ( ) => func ( root , args , context ) )
20
+ . catch ( ( ) => executeAndCache ( ) )
16
21
) ;
17
22
} ;
18
23
}
You can’t perform that action at this time.
0 commit comments