Skip to content

Commit

Permalink
Use /ids endpoint to retrieve learningpaths
Browse files Browse the repository at this point in the history
  • Loading branch information
katrinewi committed Feb 10, 2025
1 parent f92b80d commit a5e16c7
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/api/folderResourceMetaApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import groupBy from "lodash/groupBy";
import { IArticleV2DTO } from "@ndla/types-backend/article-api";
import { IAudioMetaInformationDTO } from "@ndla/types-backend/audio-api";
import { IImageMetaInformationV2DTO } from "@ndla/types-backend/image-api";
import { ILearningPathSummaryV2DTO } from "@ndla/types-backend/learningpath-api";
import { ILearningPathV2DTO } from "@ndla/types-backend/learningpath-api";
import { ResourceType } from "@ndla/types-backend/myndla-api";
import { Node } from "@ndla/types-taxonomy";
import { fetchAudio } from "./audioApi";
Expand Down Expand Up @@ -47,7 +47,7 @@ const fetchResourceMeta = async (
if (type === "learningpath") {
const learningpaths = await context.loaders.learningpathsLoader.loadMany(ids);
return learningpaths
.filter((learningpath): learningpath is ILearningPathSummaryV2DTO => !!learningpath)
.filter((learningpath): learningpath is ILearningPathV2DTO => !!learningpath)
.map(learningpathToMeta);
} else {
const articles = await context.loaders.articlesLoader.loadMany(ids);
Expand Down
16 changes: 5 additions & 11 deletions src/api/learningpathApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@
*
*/

import {
ILearningPathV2DTO,
ILearningPathSummaryV2DTO,
ISearchResultV2DTO,
ILearningStepV2DTO,
} from "@ndla/types-backend/learningpath-api";
import { ILearningPathV2DTO, ILearningStepV2DTO } from "@ndla/types-backend/learningpath-api";
import {
GQLMutationDeleteLearningpathStepArgs,
GQLMutationNewLearningpathArgs,
Expand All @@ -25,17 +20,16 @@ import { fetch, resolveJson } from "../utils/apiHelpers";
export async function fetchLearningpaths(
learningpathIds: string[],
context: Context,
): Promise<Array<ILearningPathSummaryV2DTO | undefined>> {
): Promise<Array<ILearningPathV2DTO | undefined>> {
const response = await fetch(
`/learningpath-api/v2/learningpaths/?language=${context.language}&fallback=true&ids=${learningpathIds.join(",")}`,
`/learningpath-api/v2/learningpaths/ids?language=${context.language}&ids=${learningpathIds.join(",")}`,
context,
);
const json: ISearchResultV2DTO = await resolveJson(response);

const json: ILearningPathV2DTO[] = await resolveJson(response);
// The api does not always return the exact number of results as ids provided.
// So always map over ids so that dataLoader gets the right amount of results in correct order.
return learningpathIds.map((id) => {
const learningpath = json.results.find((item) => {
const learningpath = json.find((item) => {
return item.id.toString() === id;
});
return learningpath;
Expand Down
4 changes: 2 additions & 2 deletions src/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import DataLoader from "dataloader";
import { IArticleV2DTO } from "@ndla/types-backend/article-api";
import { IFilmFrontPageDataDTO, IFrontPageDTO, ISubjectPageDataDTO } from "@ndla/types-backend/frontpage-api";
import { ILearningPathSummaryV2DTO } from "@ndla/types-backend/learningpath-api";
import { ILearningPathV2DTO } from "@ndla/types-backend/learningpath-api";
import { Node } from "@ndla/types-taxonomy";
import {
fetchArticles,
Expand All @@ -34,7 +34,7 @@ export function articlesLoader(context: Context): DataLoader<string, IArticleV2D
);
}

export function learningpathsLoader(context: Context): DataLoader<string, ILearningPathSummaryV2DTO | undefined> {
export function learningpathsLoader(context: Context): DataLoader<string, ILearningPathV2DTO | undefined> {
return new DataLoader(async (learningpathIds) => {
return fetchLearningpaths(learningpathIds, context);
});
Expand Down
15 changes: 5 additions & 10 deletions src/utils/apiHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@
import { GraphQLError } from "graphql";
import { Response } from "node-fetch";
import { IArticleV2DTO } from "@ndla/types-backend/article-api";
import {
ILearningPathV2DTO,
ILearningPathSummaryV2DTO,
ILearningStepV2DTO,
} from "@ndla/types-backend/learningpath-api";
import { ILearningPathV2DTO, ILearningStepV2DTO } from "@ndla/types-backend/learningpath-api";
import { Node, TaxonomyContext, TaxonomyCrumb } from "@ndla/types-taxonomy";
import createFetch from "./fetch";
import { createCache } from "../cache";
Expand Down Expand Up @@ -187,18 +183,17 @@ export function articleToMeta(article: IArticleV2DTO): GQLMeta {
};
}

export function learningpathToMeta(learningpath: ILearningPathSummaryV2DTO): GQLMeta {
export function learningpathToMeta(learningpath: ILearningPathV2DTO): GQLMeta {
return {
id: learningpath.id,
title: learningpath.title.title,
htmlTitle: learningpath.title.title,
introduction: learningpath.introduction.introduction,
metaDescription: learningpath.description.description,
lastUpdated: learningpath.lastUpdated,
metaImage: learningpath.coverPhotoUrl
metaImage: learningpath.coverPhoto?.url
? {
url: learningpath.coverPhotoUrl,
alt: learningpath.introduction.introduction,
url: learningpath.coverPhoto.url,
alt: "",
}
: undefined,
};
Expand Down

0 comments on commit a5e16c7

Please sign in to comment.