Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
  • Loading branch information
mehulmathur16 committed Sep 11, 2024
1 parent e41e715 commit e845252
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
4 changes: 3 additions & 1 deletion projects/mehulmathur16/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down Expand Up @@ -47,7 +48,8 @@ server.register(mercurius, {
return {
userDataLoader,
postDataLoader,
cache
cache,
postsCache
}
},
graphiql: true
Expand Down
15 changes: 11 additions & 4 deletions projects/mehulmathur16/src/resolvers.ts
Original file line number Diff line number Diff line change
@@ -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']) {
Expand Down

0 comments on commit e845252

Please sign in to comment.