From 1d613bfdc562fc0f43fc84bd582cec21752b05b9 Mon Sep 17 00:00:00 2001 From: Jonas Ege Carlsen Date: Wed, 8 Nov 2023 12:47:10 +0100 Subject: [PATCH] Add query for resolving resource from article contenturi --- src/api/taxonomyApi.ts | 17 ++++++++++++++++ src/resolvers/resourceResolvers.ts | 32 ++++++++++++++++++++++++++++++ src/schema.ts | 1 + src/types/schema.d.ts | 23 ++++++++++++++++----- 4 files changed, 68 insertions(+), 5 deletions(-) diff --git a/src/api/taxonomyApi.ts b/src/api/taxonomyApi.ts index 98b1349f..7c38027c 100644 --- a/src/api/taxonomyApi.ts +++ b/src/api/taxonomyApi.ts @@ -113,6 +113,23 @@ export async function fetchTopics( return resolveJson(response); } +export async function fetchNodeByContentUri( + contentUri: string, + context: Context, +): Promise { + const query = qs.stringify({ + contentURI: contentUri, + language: context.language, + includeContexts: true, + }); + const response = await taxonomyFetch( + `/${context.taxonomyUrl}/v1/nodes?${query}`, + context, + ); + const resolved: Node[] = await resolveJson(response); + return resolved[0]; +} + export async function fetchNode( params: { id: string }, context: Context, diff --git a/src/resolvers/resourceResolvers.ts b/src/resolvers/resourceResolvers.ts index e91daccf..c73ed5a2 100644 --- a/src/resolvers/resourceResolvers.ts +++ b/src/resolvers/resourceResolvers.ts @@ -24,6 +24,7 @@ import { GQLArticle, GQLLearningpath, GQLMeta, + GQLQueryArticleResourceArgs, GQLQueryResourceArgs, GQLResource, GQLResourceType, @@ -31,8 +32,39 @@ import { GQLTaxonomyContext, GQLVisualElementOembed, } from '../types/schema'; +import { fetchNodeByContentUri } from '../api/taxonomyApi'; export const Query = { + async articleResource( + _: any, + { articleId, taxonomyId }: GQLQueryArticleResourceArgs, + context: ContextWithLoaders, + ): Promise { + const resource = articleId + ? await fetchNodeByContentUri(`urn:article:${articleId}`, context) + : taxonomyId + ? await fetchNode({ id: taxonomyId }, context) + : null; + if (!resource) return null; + + const visibleCtx = resource.contexts.filter(c => c.isVisible); + + return { + ...resource, + path: resource.path, + rank: visibleCtx?.[0]?.rank, + relevanceId: visibleCtx?.[0]?.relevanceId || 'urn:relevance:core', + contexts: visibleCtx.map(ctx => { + const breadcrumbs = + ctx.breadcrumbs[context.language] || ctx.breadcrumbs['nb'] || []; + return { + path: ctx.path, + parentIds: ctx.parentIds, + breadcrumbs, + }; + }), + }; + }, async resource( _: any, { id, subjectId, topicId }: GQLQueryResourceArgs, diff --git a/src/schema.ts b/src/schema.ts index 73461f92..e8824b9d 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -1202,6 +1202,7 @@ export const typeDefs = gql` type Query { resource(id: String!, subjectId: String, topicId: String): Resource + articleResource(articleId: String, taxonomyId: String): Resource article( id: String! subjectId: String diff --git a/src/types/schema.d.ts b/src/types/schema.d.ts index 90bcb9c2..2ef0e598 100644 --- a/src/types/schema.d.ts +++ b/src/types/schema.d.ts @@ -69,10 +69,10 @@ export type GQLArenaTopic = { export type GQLArenaUser = { __typename?: 'ArenaUser'; displayName: Scalars['String']; - username: Scalars['String']; id: Scalars['Int']; profilePicture?: Maybe; slug: Scalars['String']; + username: Scalars['String']; }; export type GQLArticle = { @@ -1066,7 +1066,9 @@ export type GQLQuery = { arenaRecentTopics: Array; arenaTopic?: Maybe; arenaTopicsByUser: Array; + arenaUser?: Maybe; article?: Maybe; + articleResource?: Maybe; audio?: Maybe; competenceGoal?: Maybe; competenceGoals?: Maybe>; @@ -1111,14 +1113,12 @@ export type GQLQueryAllFolderResourcesArgs = { size?: InputMaybe; }; + export type GQLQueryArenaCategoryArgs = { categoryId: Scalars['Int']; page: Scalars['Int']; }; -export type GQLQueryArenaUserArgs = { - username: Scalars['String']; -}; export type GQLQueryArenaTopicArgs = { page: Scalars['Int']; @@ -1131,6 +1131,11 @@ export type GQLQueryArenaTopicsByUserArgs = { }; +export type GQLQueryArenaUserArgs = { + username: Scalars['String']; +}; + + export type GQLQueryArticleArgs = { absoluteUrl?: InputMaybe; convertEmbeds?: InputMaybe; @@ -1143,6 +1148,12 @@ export type GQLQueryArticleArgs = { }; +export type GQLQueryArticleResourceArgs = { + articleId?: InputMaybe; + taxonomyId?: InputMaybe; +}; + + export type GQLQueryAudioArgs = { id: Scalars['Int']; }; @@ -2185,6 +2196,7 @@ export type GQLArenaUserResolvers; profilePicture?: Resolver, ParentType, ContextType>; slug?: Resolver; + username?: Resolver; __isTypeOf?: IsTypeOfResolverFn; }; @@ -3085,10 +3097,11 @@ export type GQLQueryResolvers, ParentType, ContextType>; arenaCategory?: Resolver, ParentType, ContextType, RequireFields>; arenaRecentTopics?: Resolver, ParentType, ContextType>; - arenaUser?: Resolver, ParentType, ContextType, RequireFields>; arenaTopic?: Resolver, ParentType, ContextType, RequireFields>; arenaTopicsByUser?: Resolver, ParentType, ContextType, RequireFields>; + arenaUser?: Resolver, ParentType, ContextType, RequireFields>; article?: Resolver, ParentType, ContextType, RequireFields>; + articleResource?: Resolver, ParentType, ContextType, Partial>; audio?: Resolver, ParentType, ContextType, RequireFields>; competenceGoal?: Resolver, ParentType, ContextType, RequireFields>; competenceGoals?: Resolver>, ParentType, ContextType, Partial>;