feat(server): multi region support in dataloaders

This commit is contained in:
Kristaps Fabians Geikins
2024-11-07 09:57:43 +02:00
parent a48ec073a1
commit 69ff3864af
26 changed files with 827 additions and 742 deletions
@@ -1,4 +1,3 @@
import { db } from '@/db/knex'
import { getFeatureFlags } from '@/modules/shared/helpers/envHelper'
import { defineRequestDataloaders } from '@/modules/shared/helpers/graphqlHelper'
import {
@@ -14,42 +13,46 @@ import { keyBy } from 'lodash'
const { FF_WORKSPACES_MODULE_ENABLED } = getFeatureFlags()
declare module '@/modules/core/loaders' {
interface AllRequestDataLoaders
interface ModularizedDataLoaders
extends Partial<ReturnType<typeof dataLoadersDefinition>> {}
}
const dataLoadersDefinition = defineRequestDataloaders(({ ctx, createLoader }) => {
const getWorkspaces = getWorkspacesFactory({ db })
const getWorkspaceDomains = getWorkspaceDomainsFactory({ db })
const dataLoadersDefinition = defineRequestDataloaders(
({ ctx, createLoader, deps: { db } }) => {
const getWorkspaces = getWorkspacesFactory({ db })
const getWorkspaceDomains = getWorkspaceDomainsFactory({ db })
return {
workspaces: {
/**
* Get workspace, with the active user's role attached
*/
getWorkspace: createLoader<string, WorkspaceWithOptionalRole | null>(
async (ids) => {
const results = keyBy(
await getWorkspaces({ workspaceIds: ids.slice(), userId: ctx.userId }),
(w) => w.id
)
return ids.map((id) => results[id] || null)
}
)
},
workspaceDomains: {
/**
* Get workspace, with the active user's role attached
*/
getWorkspaceDomains: createLoader<string, WorkspaceDomain | null>(async (ids) => {
const results = keyBy(
await getWorkspaceDomains({ workspaceIds: ids.slice() }),
(w) => w.id
return {
workspaces: {
/**
* Get workspace, with the active user's role attached
*/
getWorkspace: createLoader<string, WorkspaceWithOptionalRole | null>(
async (ids) => {
const results = keyBy(
await getWorkspaces({ workspaceIds: ids.slice(), userId: ctx.userId }),
(w) => w.id
)
return ids.map((id) => results[id] || null)
}
)
return ids.map((id) => results[id] || null)
})
},
workspaceDomains: {
/**
* Get workspace, with the active user's role attached
*/
getWorkspaceDomains: createLoader<string, WorkspaceDomain | null>(
async (ids) => {
const results = keyBy(
await getWorkspaceDomains({ workspaceIds: ids.slice() }),
(w) => w.id
)
return ids.map((id) => results[id] || null)
}
)
}
}
}
})
)
export default FF_WORKSPACES_MODULE_ENABLED ? dataLoadersDefinition : undefined