batch load sources
This commit is contained in:
@ -40,6 +40,7 @@ import {
|
||||
countDailyServiceUsage,
|
||||
createServiceUsage,
|
||||
} from './services/service_usage'
|
||||
import { findSubscriptionsByNames } from './services/subscriptions'
|
||||
import { batchGetUploadFilesByIds } from './services/upload_file'
|
||||
import { tracer } from './tracing'
|
||||
import { getClaimsByToken, setAuthInCookie } from './utils/auth'
|
||||
@ -116,6 +117,13 @@ const contextFunc: ContextFunction<ExpressContext, ResolverContext> = async ({
|
||||
uploadFiles: new DataLoader(batchGetUploadFilesByIds),
|
||||
libraryItems: new DataLoader(batchGetLibraryItems),
|
||||
publicItems: new DataLoader(batchGetPublicItems),
|
||||
subscriptions: new DataLoader(async (names: readonly string[]) => {
|
||||
if (!claims?.uid) {
|
||||
throw new Error('No user id found in claims')
|
||||
}
|
||||
|
||||
return findSubscriptionsByNames(claims?.uid || '', names as string[])
|
||||
}),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@ -1303,7 +1303,7 @@ export type HomeItem = {
|
||||
previewContent?: Maybe<Scalars['String']>;
|
||||
saveCount?: Maybe<Scalars['Int']>;
|
||||
seen_at?: Maybe<Scalars['Date']>;
|
||||
subscription?: Maybe<HomeItemSource>;
|
||||
source?: Maybe<HomeItemSource>;
|
||||
thumbnail?: Maybe<Scalars['String']>;
|
||||
title: Scalars['String'];
|
||||
url: Scalars['String'];
|
||||
@ -6043,7 +6043,7 @@ export type HomeItemResolvers<ContextType = ResolverContext, ParentType extends
|
||||
previewContent?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
|
||||
saveCount?: Resolver<Maybe<ResolversTypes['Int']>, ParentType, ContextType>;
|
||||
seen_at?: Resolver<Maybe<ResolversTypes['Date']>, ParentType, ContextType>;
|
||||
subscription?: Resolver<Maybe<ResolversTypes['HomeItemSource']>, ParentType, ContextType>;
|
||||
source?: Resolver<Maybe<ResolversTypes['HomeItemSource']>, ParentType, ContextType>;
|
||||
thumbnail?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
|
||||
title?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
|
||||
url?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
|
||||
|
||||
@ -1172,7 +1172,7 @@ type HomeItem {
|
||||
previewContent: String
|
||||
saveCount: Int
|
||||
seen_at: Date
|
||||
subscription: HomeItemSource
|
||||
source: HomeItemSource
|
||||
thumbnail: String
|
||||
title: String!
|
||||
url: String!
|
||||
|
||||
@ -21,6 +21,8 @@ import {
|
||||
Article,
|
||||
Highlight,
|
||||
HomeItem,
|
||||
HomeItemSource,
|
||||
HomeItemSourceType,
|
||||
Label,
|
||||
PageType,
|
||||
Recommendation,
|
||||
@ -29,6 +31,7 @@ import {
|
||||
} from '../generated/graphql'
|
||||
import { getAISummary } from '../services/ai-summaries'
|
||||
import { findUserFeatures } from '../services/features'
|
||||
import { Merge } from '../util'
|
||||
import {
|
||||
highlightDataToHighlight,
|
||||
isBase64Image,
|
||||
@ -665,8 +668,10 @@ export const functionResolvers = {
|
||||
canDelete: !libraryItem.deletedAt,
|
||||
canSave: false,
|
||||
dir: libraryItem.directionality,
|
||||
subscription: null,
|
||||
previewContent: libraryItem.description,
|
||||
subscription: libraryItem.subscription,
|
||||
siteName: libraryItem.siteName,
|
||||
siteIcon: libraryItem.siteIcon,
|
||||
} as HomeItem)
|
||||
)
|
||||
.concat(
|
||||
@ -688,29 +693,44 @@ export const functionResolvers = {
|
||||
broadcastCount: publicItem.stats.broadcastCount,
|
||||
likeCount: publicItem.stats.likeCount,
|
||||
saveCount: publicItem.stats.saveCount,
|
||||
subscription: publicItem.source,
|
||||
source: publicItem.source,
|
||||
} as HomeItem)
|
||||
)
|
||||
)
|
||||
},
|
||||
},
|
||||
HomeItem: {
|
||||
async subscription(
|
||||
item: HomeItem,
|
||||
async source(
|
||||
item: Merge<
|
||||
HomeItem,
|
||||
{ subscription?: string; siteName: string; siteIcon?: string }
|
||||
>,
|
||||
_: unknown,
|
||||
ctx: WithDataSourcesContext
|
||||
) {
|
||||
// if (item.subscription) {
|
||||
// return item.subscription
|
||||
// }
|
||||
// const subscription = await ctx.dataLoaders.subscriptions.load(item.id)
|
||||
// if (!subscription) {
|
||||
// return {
|
||||
// name: item.siteName,
|
||||
// icon: item.siteIcon,
|
||||
// type: 'rss',
|
||||
// }
|
||||
// }
|
||||
): Promise<HomeItemSource> {
|
||||
if (item.source) {
|
||||
return item.source
|
||||
}
|
||||
|
||||
if (!item.subscription) {
|
||||
return {
|
||||
name: item.siteName,
|
||||
icon: item.siteIcon,
|
||||
type: HomeItemSourceType.Library,
|
||||
}
|
||||
}
|
||||
|
||||
const subscription = await ctx.dataLoaders.subscriptions.load(
|
||||
item.subscription
|
||||
)
|
||||
|
||||
return {
|
||||
id: subscription.id,
|
||||
url: subscription.url,
|
||||
name: subscription.name,
|
||||
icon: subscription.icon,
|
||||
type: subscription.type as unknown as HomeItemSourceType,
|
||||
}
|
||||
},
|
||||
},
|
||||
...resultResolveTypeResolver('Login'),
|
||||
|
||||
@ -11,6 +11,7 @@ import { Label } from '../entity/label'
|
||||
import { LibraryItem } from '../entity/library_item'
|
||||
import { PublicItem } from '../entity/public_item'
|
||||
import { Recommendation } from '../entity/recommendation'
|
||||
import { Subscription } from '../entity/subscription'
|
||||
import { UploadFile } from '../entity/upload_file'
|
||||
import { HomeItem } from '../generated/graphql'
|
||||
import { PubsubClient } from '../pubsub'
|
||||
@ -56,6 +57,7 @@ export interface RequestContext {
|
||||
uploadFiles: DataLoader<string, UploadFile | undefined>
|
||||
libraryItems: DataLoader<string, LibraryItem>
|
||||
publicItems: DataLoader<string, PublicItem>
|
||||
subscriptions: DataLoader<string, Subscription>
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -3130,7 +3130,7 @@ const schema = gql`
|
||||
dir: String
|
||||
seen_at: Date
|
||||
wordCount: Int
|
||||
subscription: HomeItemSource
|
||||
source: HomeItemSource
|
||||
canSave: Boolean
|
||||
canComment: Boolean
|
||||
canShare: Boolean
|
||||
|
||||
Reference in New Issue
Block a user