Querying Program Contents
Read the parts, modules, lessons, and projects that make up each program in your company catalog, for example to show a syllabus or count a program’s projects.
Contents are only loaded when your query selects contents. Leave the field out of queries that don’t need it, such as a catalog sync that only reads titles, to keep those requests fast.
How Contents Are Shaped
CatalogProgram.contents is a ProgramContents union. Its member depends on the program’s type, and each member holds the top level of that program’s hierarchy:
parts → modules → lessons
modules → lessons
lessons
A lesson that introduces a project exposes it as project; otherwise project is null. Durations use ISO 8601 format, e.g. PT45M.
Writing the Query
Because contents is a union, select fields with an inline fragment for each member you want, such as ... on CourseContents. Add __typename so your code can tell which member it received. Named fragments like lessonFields keep the lesson fields in one place, since lessons appear at a different depth in each member.
contents is null when a program’s hierarchy is unavailable, for example when it has no published release yet. Only content from the published release is included.
Try it in the Catalog API Playground.
query GetProgramContents {
catalog {
programs {
key
title
type
contents {
__typename
... on DegreeContents {
parts {
title
modules { ...moduleFields }
}
}
... on PartContents {
modules { ...moduleFields }
}
... on CourseContents {
lessons { ...lessonFields }
}
}
}
}
}
fragment moduleFields on CatalogModule {
title
lessons { ...lessonFields }
}
fragment lessonFields on CatalogLesson {
key
title
duration
project {
title
duration
}
}{
"data": {
"catalog": {
"programs": [
{
"key": "cd0001",
"title": "Intro to SQL",
"type": "COURSE",
"contents": {
"__typename": "CourseContents",
"lessons": [
{
"key": "lesson-select",
"title": "Selecting Data",
"duration": "PT45M",
"project": null
},
{
"key": "lesson-sales-report",
"title": "Build a Sales Report",
"duration": "PT30M",
"project": {
"title": "Build a Sales Report",
"duration": "PT2H"
}
}
]
}
},
{
"key": "nd0002",
"title": "Cloud Architect",
"type": "DEGREE",
"contents": {
"__typename": "DegreeContents",
"parts": [
{
"title": "Cloud Foundations",
"modules": [
{
"title": "Networking Basics",
"lessons": [
{
"key": "lesson-vpc-design",
"title": "Design a Virtual Private Cloud",
"duration": "PT45M",
"project": null
}
]
}
]
}
]
}
},
{
"key": "cd0003",
"title": "Upcoming Course",
"type": "COURSE",
"contents": null
}
]
}
}
}Walking the Hierarchy
Branch on __typename to reach the lessons at the right depth. Handle a null contents separately, and add a default case so your code keeps working if a new member is added to the union.
function lessonsOf(contents) {
if (!contents) return [];
switch (contents.__typename) {
case "DegreeContents":
return contents.parts.flatMap((part) =>
part.modules.flatMap((module) => module.lessons),
);
case "PartContents":
return contents.modules.flatMap((module) => module.lessons);
case "CourseContents":
return contents.lessons;
default:
return [];
}
}
for (const program of data.catalog.programs) {
if (!program.contents) {
console.log(`${program.title}: contents unavailable`);
continue;
}
const lessons = lessonsOf(program.contents);
const projects = lessons.filter((lesson) => lesson.project);
console.log(
`${program.title}: ${lessons.length} lessons, ${projects.length} projects`,
);
}