There are a few schools of thought on this
graphQL as an api gateway, meaning you can leaverage the restful http cache… but you have to build a restful api too
cache at the resolver level, which could be done with some sort of wrapping function.
// in the resolvers comments: async (post: any) => cacheWrap(postCommentsCache, getPostComments, post.id) // generic wrapping cache function function cacheWrap(store: any, resolver: any, key: any) { return new Promise((resolve) => { if (store[key]) { resolve(store[key]); } else { return setTimeout(() => { const result = resolver(key); store[key] = result; resolve(result); }, 5000); } }); } // function to get values if not in cache function getPostComments(id: Int) { return new Promise((resolve) => { return setTimeout(() => { resolve(comments.filter((comment) => id === comment.postId)); }, 5000); }); }
cache on server start and keep it updated as mutations happen.
Leave a Reply