1
1
import { TypeComposer } from 'graphql-compose'
2
2
import DataLoader from 'dataloader'
3
- import SingleContinous from './singleContinous'
4
-
3
+ import md5 from 'md5'
5
4
import {
6
5
dataloaderOptions
7
6
} from './definitions'
@@ -43,7 +42,9 @@ export function composeWithDataLoader(
43
42
typeComposer . setResolver ( 'findById' ,
44
43
findByIdResolver . wrapResolve ( next => rp => {
45
44
if ( options . removeProjection ) delete rp . projection
46
- SingleContinous . run ( findByIdLoader , rp , 'findById' , options )
45
+ setTimeout ( ( ) => {
46
+ let res = findByIdLoader . clear ( rp )
47
+ } , options . cacheExpiration )
47
48
return findByIdLoader . load ( rp )
48
49
} )
49
50
)
@@ -63,7 +64,9 @@ export function composeWithDataLoader(
63
64
typeComposer . setResolver (
64
65
'findByIds' ,
65
66
findByIdsResolver . wrapResolve ( fn => rp => {
66
- SingleContinous . run ( findByIdsLoader , rp , 'findByIds' , options )
67
+ setTimeout ( ( ) => {
68
+ let res = findByIdsLoader . clear ( rp )
69
+ } , options . cacheExpiration )
67
70
return findByIdsLoader . load ( rp )
68
71
} )
69
72
)
@@ -83,7 +86,9 @@ export function composeWithDataLoader(
83
86
typeComposer . setResolver (
84
87
'count' ,
85
88
countResolver . wrapResolve ( fn => rp => {
86
- SingleContinous . run ( countLoader , rp , 'count' , options )
89
+ setTimeout ( ( ) => {
90
+ let res = countLoader . clear ( rp )
91
+ } , options . cacheExpiration )
87
92
return countLoader . load ( rp )
88
93
} )
89
94
)
@@ -102,8 +107,10 @@ export function composeWithDataLoader(
102
107
103
108
typeComposer . setResolver (
104
109
'findOne' ,
105
- findByIdsResolver . wrapResolve ( fn => rp => {
106
- SingleContinous . run ( findOneLoader , rp , 'findOne' , options )
110
+ findOneResolver . wrapResolve ( fn => rp => {
111
+ setTimeout ( ( ) => {
112
+ let res = findOneLoader . clear ( rp )
113
+ } , options . cacheExpiration )
107
114
return findOneLoader . load ( rp )
108
115
} )
109
116
)
@@ -117,13 +124,15 @@ export function composeWithDataLoader(
117
124
if ( options . debug ) console . log ( 'New db request (findMany)' )
118
125
resolve ( resolveParamsArray . map ( rp => findManyResolver . resolve ( rp ) ) )
119
126
} ) ,
120
- { cacheKeyFn : key => getHashKey ( key ) } )
127
+ { cacheKeyFn : key => getHashKey ( key ) } )
121
128
122
129
typeComposer . setResolver (
123
130
'findMany' ,
124
131
findManyResolver . wrapResolve ( next => rp => {
125
132
if ( options . removeProjection ) delete rp . projection
126
- SingleContinous . run ( findManyLoader , rp , 'findMany' , options )
133
+ setTimeout ( ( ) => {
134
+ let res = findManyLoader . clear ( rp )
135
+ } , options . cacheExpiration )
127
136
return findManyLoader . load ( rp )
128
137
} )
129
138
)
@@ -148,7 +157,9 @@ export function composeWithDataLoader(
148
157
connectionFieldNames . map ( field => projection . edges . node [ field ] = true )
149
158
rp . projection = projection
150
159
}
151
- SingleContinous . run ( connectionLoader , rp , 'connection' , options )
160
+ setTimeout ( ( ) => {
161
+ let res = connectionLoader . clear ( rp )
162
+ } , options . cacheExpiration )
152
163
return connectionLoader . load ( rp )
153
164
} )
154
165
)
@@ -157,11 +168,11 @@ export function composeWithDataLoader(
157
168
const getHashKey = key => {
158
169
let object = { }
159
170
Object . assign ( object ,
160
- { args : key . args } ,
171
+ { args : key . args || { } } ,
161
172
{ projection : key . projection || { } } ,
162
173
{ rawQuery : JSON . stringify ( key . rawQuery || { } ) } ,
163
174
{ context : JSON . stringify ( key . context || { } ) } )
164
- let hash = JSON . stringify ( object ) . split ( "" ) . reduce ( ( a , b ) => { a = ( ( a << 5 ) - a ) + b . charCodeAt ( 0 ) ; return a & a } , 0 )
175
+ let hash = md5 ( JSON . stringify ( object ) )
165
176
return hash
166
177
}
167
178
0 commit comments