Skip to content

Commit

Permalink
Small cache change
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish committed Dec 13, 2024
1 parent 9ad792d commit 573d859
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
8 changes: 4 additions & 4 deletions src/lib/gql/gql-queries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const getEntityFromPath = async <T extends NodeUnion>(
}

export const getConfigPage = nextCache(
cache(async <T extends ConfigPagesUnion>(configPageType: ConfigPagesUnion["__typename"]): Promise<T | undefined> => {
async <T extends ConfigPagesUnion>(configPageType: ConfigPagesUnion["__typename"]): Promise<T | undefined> => {
let query: ConfigPagesQuery
try {
query = await graphqlClient({next: {tags: ["config-pages"]}}).ConfigPages()
Expand All @@ -79,16 +79,16 @@ export const getConfigPage = nextCache(
return query[queryKey].nodes[0] as T
}
}
}),
},
[],
{tags: ["config-pages"]}
)

export const getConfigPageField = nextCache(
cache(async <T extends ConfigPagesUnion, F>(configPageType: ConfigPagesUnion["__typename"], fieldName: keyof T) => {
async <T extends ConfigPagesUnion, F>(configPageType: ConfigPagesUnion["__typename"], fieldName: keyof T) => {
const configPage = await getConfigPage<T>(configPageType)
return configPage?.[fieldName] as F
}),
},
[],
{tags: ["config-pages"]}
)
Expand Down
49 changes: 26 additions & 23 deletions src/lib/utils/getLegacyBookPaths.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
import {BooksWorkIdQuery, BooksWorkIdQueryVariables} from "@lib/gql/__generated__/drupal.d"
import {graphqlClient} from "@lib/gql/gql-client"
import {unstable_cache as nextCache} from "next/cache"
import {cache} from "react"

export const getLegacyBookPaths = nextCache(
async () => {
const nodes: Array<{id: number; path: string}> = []
let fetchMore = true
let nodeQuery: BooksWorkIdQuery
const cursors: Omit<BooksWorkIdQueryVariables, "first"> = {}
export const getLegacyBookPaths = cache(
nextCache(
async () => {
const nodes: Array<{id: number; path: string}> = []
let fetchMore = true
let nodeQuery: BooksWorkIdQuery
const cursors: Omit<BooksWorkIdQueryVariables, "first"> = {}

while (fetchMore) {
nodeQuery = await graphqlClient({cache: "no-cache"}).BooksWorkId({first: 1000, ...cursors})
nodeQuery.nodeSupBooks.nodes
.filter(node => !!node.supBookWorkIdNumber)
.map(node =>
nodes.push({
id: node.supBookWorkIdNumber as number,
path: node.path,
})
)
cursors.after = nodeQuery.nodeSupBooks.pageInfo.endCursor
fetchMore = nodeQuery.nodeSupBooks.pageInfo.hasNextPage
}
while (fetchMore) {
nodeQuery = await graphqlClient({cache: "no-cache"}).BooksWorkId({first: 1000, ...cursors})
nodeQuery.nodeSupBooks.nodes
.filter(node => !!node.supBookWorkIdNumber)
.map(node =>
nodes.push({
id: node.supBookWorkIdNumber as number,
path: node.path,
})
)
cursors.after = nodeQuery.nodeSupBooks.pageInfo.endCursor
fetchMore = nodeQuery.nodeSupBooks.pageInfo.hasNextPage
}

return nodes
},
[],
{tags: ["legacy-books"]}
return nodes
},
[],
{tags: ["legacy-books"]}
)
)

0 comments on commit 573d859

Please sign in to comment.