Skip to content

Commit

Permalink
Merge pull request #380 from NDLANO/resolve-article-resource-from-con…
Browse files Browse the repository at this point in the history
…tent-uri

Add query for resolving resource from article contenturi
  • Loading branch information
Jonas-C authored Nov 8, 2023
2 parents fd2017b + 1d613bf commit 810fd73
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
17 changes: 17 additions & 0 deletions src/api/taxonomyApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,23 @@ export async function fetchTopics(
return resolveJson(response);
}

export async function fetchNodeByContentUri(
contentUri: string,
context: Context,
): Promise<Node | undefined> {
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,
Expand Down
32 changes: 32 additions & 0 deletions src/resolvers/resourceResolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,47 @@ import {
GQLArticle,
GQLLearningpath,
GQLMeta,
GQLQueryArticleResourceArgs,
GQLQueryResourceArgs,
GQLResource,
GQLResourceType,
GQLResourceTypeDefinition,
GQLTaxonomyContext,
GQLVisualElementOembed,
} from '../types/schema';
import { fetchNodeByContentUri } from '../api/taxonomyApi';

export const Query = {
async articleResource(
_: any,
{ articleId, taxonomyId }: GQLQueryArticleResourceArgs,
context: ContextWithLoaders,
): Promise<GQLResource | null> {
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,
Expand Down
1 change: 1 addition & 0 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 18 additions & 5 deletions src/types/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ export type GQLArenaTopic = {
export type GQLArenaUser = {
__typename?: 'ArenaUser';
displayName: Scalars['String'];
username: Scalars['String'];
id: Scalars['Int'];
profilePicture?: Maybe<Scalars['String']>;
slug: Scalars['String'];
username: Scalars['String'];
};

export type GQLArticle = {
Expand Down Expand Up @@ -1066,7 +1066,9 @@ export type GQLQuery = {
arenaRecentTopics: Array<GQLArenaTopic>;
arenaTopic?: Maybe<GQLArenaTopic>;
arenaTopicsByUser: Array<GQLArenaTopic>;
arenaUser?: Maybe<GQLArenaUser>;
article?: Maybe<GQLArticle>;
articleResource?: Maybe<GQLResource>;
audio?: Maybe<GQLAudio>;
competenceGoal?: Maybe<GQLCompetenceGoal>;
competenceGoals?: Maybe<Array<GQLCompetenceGoal>>;
Expand Down Expand Up @@ -1111,14 +1113,12 @@ export type GQLQueryAllFolderResourcesArgs = {
size?: InputMaybe<Scalars['Int']>;
};


export type GQLQueryArenaCategoryArgs = {
categoryId: Scalars['Int'];
page: Scalars['Int'];
};

export type GQLQueryArenaUserArgs = {
username: Scalars['String'];
};

export type GQLQueryArenaTopicArgs = {
page: Scalars['Int'];
Expand All @@ -1131,6 +1131,11 @@ export type GQLQueryArenaTopicsByUserArgs = {
};


export type GQLQueryArenaUserArgs = {
username: Scalars['String'];
};


export type GQLQueryArticleArgs = {
absoluteUrl?: InputMaybe<Scalars['Boolean']>;
convertEmbeds?: InputMaybe<Scalars['Boolean']>;
Expand All @@ -1143,6 +1148,12 @@ export type GQLQueryArticleArgs = {
};


export type GQLQueryArticleResourceArgs = {
articleId?: InputMaybe<Scalars['String']>;
taxonomyId?: InputMaybe<Scalars['String']>;
};


export type GQLQueryAudioArgs = {
id: Scalars['Int'];
};
Expand Down Expand Up @@ -2185,6 +2196,7 @@ export type GQLArenaUserResolvers<ContextType = any, ParentType extends GQLResol
id?: Resolver<GQLResolversTypes['Int'], ParentType, ContextType>;
profilePicture?: Resolver<Maybe<GQLResolversTypes['String']>, ParentType, ContextType>;
slug?: Resolver<GQLResolversTypes['String'], ParentType, ContextType>;
username?: Resolver<GQLResolversTypes['String'], ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

Expand Down Expand Up @@ -3085,10 +3097,11 @@ export type GQLQueryResolvers<ContextType = any, ParentType extends GQLResolvers
arenaCategories?: Resolver<Array<GQLResolversTypes['ArenaCategory']>, ParentType, ContextType>;
arenaCategory?: Resolver<Maybe<GQLResolversTypes['ArenaCategory']>, ParentType, ContextType, RequireFields<GQLQueryArenaCategoryArgs, 'categoryId' | 'page'>>;
arenaRecentTopics?: Resolver<Array<GQLResolversTypes['ArenaTopic']>, ParentType, ContextType>;
arenaUser?: Resolver<Maybe<GQLResolversTypes['ArenaUser']>, ParentType, ContextType, RequireFields<GQLQueryArenaUserArgs, 'username'>>;
arenaTopic?: Resolver<Maybe<GQLResolversTypes['ArenaTopic']>, ParentType, ContextType, RequireFields<GQLQueryArenaTopicArgs, 'page' | 'topicId'>>;
arenaTopicsByUser?: Resolver<Array<GQLResolversTypes['ArenaTopic']>, ParentType, ContextType, RequireFields<GQLQueryArenaTopicsByUserArgs, 'userSlug'>>;
arenaUser?: Resolver<Maybe<GQLResolversTypes['ArenaUser']>, ParentType, ContextType, RequireFields<GQLQueryArenaUserArgs, 'username'>>;
article?: Resolver<Maybe<GQLResolversTypes['Article']>, ParentType, ContextType, RequireFields<GQLQueryArticleArgs, 'id'>>;
articleResource?: Resolver<Maybe<GQLResolversTypes['Resource']>, ParentType, ContextType, Partial<GQLQueryArticleResourceArgs>>;
audio?: Resolver<Maybe<GQLResolversTypes['Audio']>, ParentType, ContextType, RequireFields<GQLQueryAudioArgs, 'id'>>;
competenceGoal?: Resolver<Maybe<GQLResolversTypes['CompetenceGoal']>, ParentType, ContextType, RequireFields<GQLQueryCompetenceGoalArgs, 'code'>>;
competenceGoals?: Resolver<Maybe<Array<GQLResolversTypes['CompetenceGoal']>>, ParentType, ContextType, Partial<GQLQueryCompetenceGoalsArgs>>;
Expand Down

0 comments on commit 810fd73

Please sign in to comment.