From e8452529d1a9ca1955903dd13405243f483fc959 Mon Sep 17 00:00:00 2001 From: Mehul Mathur Date: Wed, 11 Sep 2024 19:00:44 +0000 Subject: [PATCH] testing --- projects/mehulmathur16/src/index.ts | 4 +++- projects/mehulmathur16/src/resolvers.ts | 15 +++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/projects/mehulmathur16/src/index.ts b/projects/mehulmathur16/src/index.ts index 49f339a..29db42d 100644 --- a/projects/mehulmathur16/src/index.ts +++ b/projects/mehulmathur16/src/index.ts @@ -14,6 +14,7 @@ server.register(mercurius, { resolvers, context: () => { let cache = {}; + let postsCache = {}; const userDataLoader = new DataLoader(async function (ids: any) { const response = await axiosInstance.get('/users', { @@ -47,7 +48,8 @@ server.register(mercurius, { return { userDataLoader, postDataLoader, - cache + cache, + postsCache } }, graphiql: true diff --git a/projects/mehulmathur16/src/resolvers.ts b/projects/mehulmathur16/src/resolvers.ts index 4c7d369..4b7dee0 100644 --- a/projects/mehulmathur16/src/resolvers.ts +++ b/projects/mehulmathur16/src/resolvers.ts @@ -1,18 +1,25 @@ import axiosInstance from './axios'; -import http from 'http'; const resolvers = { Query: { - posts: async (_: any, __: any, { cache }: any) => { + posts: async (_: any, __: any, { cache, postsCache }: any) => { if (cache['posts']) { return cache['posts']; } const response = await axiosInstance.get('/posts'); cache['posts'] = response.data; + + response.data.map((currPost: any) => { + postsCache[currPost.id] = currPost; + }) + return response.data; }, - post: async (_: any, { id }: { id: number }, context: any) => { - return context?.postDataLoader.load(id); + post: async (_: any, { id }: { id: number }, { postsCache, postDataLoader }: any) => { + if (postsCache[id]) { + return postsCache[id]; + } + return postDataLoader.load(id); }, users: async (_: any, __: any, { cache }: any) => { if (cache['users']) {