11import { TypeComposer } from 'graphql-compose'
22import DataLoader from 'dataloader'
3- import SingleContinous from './singleContinous'
4-
3+ import md5 from 'md5'
54import {
65 dataloaderOptions
76} from './definitions'
@@ -43,7 +42,9 @@ export function composeWithDataLoader(
4342 typeComposer . setResolver ( 'findById' ,
4443 findByIdResolver . wrapResolve ( next => rp => {
4544 if ( options . removeProjection ) delete rp . projection
46- SingleContinous . run ( findByIdLoader , rp , 'findById' , options )
45+ setTimeout ( ( ) => {
46+ let res = findByIdLoader . clear ( rp )
47+ } , options . cacheExpiration )
4748 return findByIdLoader . load ( rp )
4849 } )
4950 )
@@ -63,7 +64,9 @@ export function composeWithDataLoader(
6364 typeComposer . setResolver (
6465 'findByIds' ,
6566 findByIdsResolver . wrapResolve ( fn => rp => {
66- SingleContinous . run ( findByIdsLoader , rp , 'findByIds' , options )
67+ setTimeout ( ( ) => {
68+ let res = findByIdsLoader . clear ( rp )
69+ } , options . cacheExpiration )
6770 return findByIdsLoader . load ( rp )
6871 } )
6972 )
@@ -83,7 +86,9 @@ export function composeWithDataLoader(
8386 typeComposer . setResolver (
8487 'count' ,
8588 countResolver . wrapResolve ( fn => rp => {
86- SingleContinous . run ( countLoader , rp , 'count' , options )
89+ setTimeout ( ( ) => {
90+ let res = countLoader . clear ( rp )
91+ } , options . cacheExpiration )
8792 return countLoader . load ( rp )
8893 } )
8994 )
@@ -102,8 +107,10 @@ export function composeWithDataLoader(
102107
103108 typeComposer . setResolver (
104109 '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 )
107114 return findOneLoader . load ( rp )
108115 } )
109116 )
@@ -117,13 +124,15 @@ export function composeWithDataLoader(
117124 if ( options . debug ) console . log ( 'New db request (findMany)' )
118125 resolve ( resolveParamsArray . map ( rp => findManyResolver . resolve ( rp ) ) )
119126 } ) ,
120- { cacheKeyFn : key => getHashKey ( key ) } )
127+ { cacheKeyFn : key => getHashKey ( key ) } )
121128
122129 typeComposer . setResolver (
123130 'findMany' ,
124131 findManyResolver . wrapResolve ( next => rp => {
125132 if ( options . removeProjection ) delete rp . projection
126- SingleContinous . run ( findManyLoader , rp , 'findMany' , options )
133+ setTimeout ( ( ) => {
134+ let res = findManyLoader . clear ( rp )
135+ } , options . cacheExpiration )
127136 return findManyLoader . load ( rp )
128137 } )
129138 )
@@ -148,7 +157,9 @@ export function composeWithDataLoader(
148157 connectionFieldNames . map ( field => projection . edges . node [ field ] = true )
149158 rp . projection = projection
150159 }
151- SingleContinous . run ( connectionLoader , rp , 'connection' , options )
160+ setTimeout ( ( ) => {
161+ let res = connectionLoader . clear ( rp )
162+ } , options . cacheExpiration )
152163 return connectionLoader . load ( rp )
153164 } )
154165 )
@@ -157,11 +168,11 @@ export function composeWithDataLoader(
157168 const getHashKey = key => {
158169 let object = { }
159170 Object . assign ( object ,
160- { args : key . args } ,
171+ { args : key . args || { } } ,
161172 { projection : key . projection || { } } ,
162173 { rawQuery : JSON . stringify ( key . rawQuery || { } ) } ,
163174 { 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 ) )
165176 return hash
166177 }
167178
0 commit comments