Skip to content
Geecon Global
Book a consultation (opens in a new tab)
Menu

Document Manager docs

For developersv0.2.3 · Updated 29 Nov 2026

Fetching documents

List libraries and fetch a whole tree through the Delivery API or GraphQL, including trees deeper than 10 levels.

This page shows how to list libraries and fetch a library or folder with everything below it, through the Delivery API or GraphQL. The snippets are trimmed from the sample project in examples/document-manager-next; see Content model for the fields they read.

Listing libraries#

Filter on kind:

const { items } = await client.getEntries({
  content_type: "documentItem",
  "fields.kind": "library",
  order: ["fields.title"],
});

Fetching a library or folder#

include: 10 resolves up to 10 levels of links in one request. This fetches one library by slug with everything below it:

const { items } = await client.getEntries({
  content_type: "documentItem",
  "fields.slug": slug,
  "fields.kind": "library",
  include: 10,
});
const root = items[0];

To fetch a folder by ID instead, use client.getEntry(id, { include: 10 }).

Trees deeper than 10 levels#

Links below the 10th level come back unresolved: they have sys but no fields. That includes the file asset of a file item at the 10th level. Fetch them by ID and swap them in. Repeat until nothing is missing. sys.id[in] returns at most limit items, so ask 100 at a time. This is the sample's loop, trimmed:

const CHUNK = 100;
async function byIds(ids: string[], get: (ids: string[]) => Promise<{ items: unknown[] }>) {
  const unique = [...new Set(ids)];
  const found = new Map<string, Entry>();
  for (let i = 0; i < unique.length; i += CHUNK) {
    const { items } = await get(unique.slice(i, i + CHUNK));
    for (const e of items as Entry[]) found.set(e.sys.id, e);
  }
  return found;
}
 
async function fetchUnresolved(root: Entry) {
  const seen = new Set<Entry>();
  for (;;) {
    const missing: { list: Entry[]; link: Entry }[] = [];
    const files: Entry[] = []; // entries whose `file` asset link is unresolved
    const walk = (entry: Entry) => {
      if (seen.has(entry)) return;
      seen.add(entry);
      if (entry.fields?.file && !entry.fields.file.fields) files.push(entry);
      const list: Entry[] | undefined = entry.fields?.items;
      for (const child of list ?? []) {
        if (child.fields) walk(child);
        else missing.push({ list: list as Entry[], link: child });
      }
    };
    walk(root);
    if (!missing.length && !files.length) return;
 
    const entries = await byIds(
      missing.map((m) => m.link.sys.id),
      (ids) => client.getEntries({ "sys.id[in]": ids, include: 10, limit: CHUNK }),
    );
    for (const { list, link } of missing) {
      const entry = entries.get(link.sys.id);
      list.splice(list.indexOf(link), 1, ...(entry ? [entry] : []));
    }
    const assets = await byIds(
      files.map((f) => f.fields?.file.sys.id),
      (ids) => client.getAssets({ "sys.id[in]": ids, limit: CHUNK }),
    );
    for (const f of files) if (f.fields) f.fields.file = assets.get(f.fields.file.sys.id);
    seen.clear();
  }
}

Entries and assets that aren't published don't come back, so they drop out. Each extra 10 levels cost a few requests. Cache the result if your tree is deep.

GraphQL#

This query reads one library with three levels below it. It uses the default content type, so the GraphQL type is DocumentItem and the collection is documentItemCollection.

This query was written by hand and has not been run against a space. Check it in Contentful's GraphQL explorer first, and adjust names if your content type ID isn't documentItem.

query Library($slug: String!) {
  documentItemCollection(where: { kind: "library", slug: $slug }, limit: 1) {
    items {
      title
      slug
      itemsCollection(limit: 20) {
        items {
          title
          kind
          slug
          itemsCollection(limit: 20) {
            items {
              title
              kind
              slug
              itemsCollection(limit: 20) {
                items {
                  title
                  kind
                  slug
                  versionLabel
                  file {
                    url
                    fileName
                    contentType
                    size
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Because items links to the same type, each level you add nests the collection again, and query complexity multiplies. Contentful rejects queries over its complexity limit. The limit on each collection keeps this one under it. Without it, each collection defaults to 100 items, and three levels would multiply to far more than the limit. If a folder holds more than the limit, the extra items are left out, so raise the limit for that level or fetch the folder on its own. Lower the limits if Contentful says the query is too complex, and request file only at the levels where files appear.