batching recommendations
This commit is contained in:
@ -33,6 +33,7 @@ import ScalarResolvers from './scalars'
|
||||
import typeDefs from './schema'
|
||||
import { batchGetHighlightsFromLibraryItemIds } from './services/highlights'
|
||||
import { batchGetLabelsFromLibraryItemIds } from './services/labels'
|
||||
import { batchGetRecommendationsFromLibraryItemIds } from './services/recommendation'
|
||||
import {
|
||||
countDailyServiceUsage,
|
||||
createServiceUsage,
|
||||
@ -106,6 +107,9 @@ const contextFunc: ContextFunction<ExpressContext, ResolverContext> = async ({
|
||||
dataLoaders: {
|
||||
labels: new DataLoader(batchGetLabelsFromLibraryItemIds),
|
||||
highlights: new DataLoader(batchGetHighlightsFromLibraryItemIds),
|
||||
recommendations: new DataLoader(
|
||||
batchGetRecommendationsFromLibraryItemIds
|
||||
),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@ -25,7 +25,6 @@ import {
|
||||
} from '../generated/graphql'
|
||||
import { getAISummary } from '../services/ai-summaries'
|
||||
import { findUserFeatures } from '../services/features'
|
||||
import { findRecommendationsByLibraryItemId } from '../services/recommendation'
|
||||
import { findUploadFileById } from '../services/upload_file'
|
||||
import {
|
||||
highlightDataToHighlight,
|
||||
@ -536,10 +535,9 @@ export const functionResolvers = {
|
||||
) {
|
||||
if (item.recommendations) return item.recommendations
|
||||
|
||||
if (item.recommenderNames && item.recommenderNames.length > 0) {
|
||||
const recommendations = await findRecommendationsByLibraryItemId(
|
||||
item.id,
|
||||
ctx.uid
|
||||
if (item.recommenderNames) {
|
||||
const recommendations = await ctx.dataLoaders.recommendations.load(
|
||||
item.id
|
||||
)
|
||||
return recommendations.map(recommandationDataToRecommendation)
|
||||
}
|
||||
|
||||
@ -8,6 +8,7 @@ import winston from 'winston'
|
||||
import { ReadingProgressDataSource } from '../datasources/reading_progress_data_source'
|
||||
import { Highlight } from '../entity/highlight'
|
||||
import { Label } from '../entity/label'
|
||||
import { Recommendation } from '../entity/recommendation'
|
||||
import { PubsubClient } from '../pubsub'
|
||||
|
||||
export interface Claims {
|
||||
@ -47,6 +48,7 @@ export interface RequestContext {
|
||||
dataLoaders: {
|
||||
labels: DataLoader<string, Label[]>
|
||||
highlights: DataLoader<string, Highlight[]>
|
||||
recommendations: DataLoader<string, Recommendation[]>
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import { nanoid } from 'nanoid'
|
||||
import { DeepPartial } from 'typeorm'
|
||||
import { DeepPartial, In } from 'typeorm'
|
||||
import { LibraryItem } from '../entity/library_item'
|
||||
import { Recommendation } from '../entity/recommendation'
|
||||
import { authTrx } from '../repository'
|
||||
@ -12,6 +12,23 @@ import {
|
||||
updateLibraryItem,
|
||||
} from './library_item'
|
||||
|
||||
export const batchGetRecommendationsFromLibraryItemIds = async (
|
||||
libraryItemIds: readonly string[]
|
||||
): Promise<Recommendation[][]> => {
|
||||
const libraryItems = await authTrx(async (tx) =>
|
||||
tx.getRepository(LibraryItem).find({
|
||||
where: { id: In(libraryItemIds as string[]) },
|
||||
relations: ['recommendations'],
|
||||
})
|
||||
)
|
||||
|
||||
return libraryItemIds.map(
|
||||
(libraryItemId) =>
|
||||
libraryItems.find((libraryItem) => libraryItem.id === libraryItemId)
|
||||
?.recommendations || []
|
||||
)
|
||||
}
|
||||
|
||||
export const addRecommendation = async (
|
||||
item: LibraryItem,
|
||||
recommendation: Recommendation,
|
||||
|
||||
Reference in New Issue
Block a user