From 2a4b89859c7b8fde9c9536a707bd9f3eeae00056 Mon Sep 17 00:00:00 2001 From: NGPixel Date: Mon, 7 Oct 2019 23:38:06 -0400 Subject: [PATCH] feat: fetch page tree resolver --- server/graph/resolvers/page.js | 28 ++++++++++++++++++++++++++++ server/graph/schemas/page.graphql | 25 +++++++++++++++++++++++++ server/jobs/rebuild-tree.js | 3 +++ 3 files changed, 56 insertions(+) diff --git a/server/graph/resolvers/page.js b/server/graph/resolvers/page.js index f017a68d..84f34c27 100644 --- a/server/graph/resolvers/page.js +++ b/server/graph/resolvers/page.js @@ -115,6 +115,34 @@ module.exports = { }, async tags (obj, args, context, info) { return WIKI.models.tags.query().orderBy('tag', 'asc') + }, + async tree (obj, args, context, info) { + let results = [] + let conds = { + localeCode: args.locale, + parent: (args.parent < 1) ? null : args.parent + } + switch (args.mode) { + case 'FOLDERS': + conds.isFolder = true + results = await WIKI.models.knex('pageTree').where(conds) + break + case 'PAGES': + await WIKI.models.knex('pageTree').where(conds).andWhereNotNull('pageId') + break + default: + results = await WIKI.models.knex('pageTree').where(conds) + break + } + return results.filter(r => { + return WIKI.auth.checkAccess(context.req.user, ['read:pages'], { + path: r.path, + locale: r.localeCode + }) + }).map(r => ({ + ...r, + locale: r.localeCode + })) } }, PageMutation: { diff --git a/server/graph/schemas/page.graphql b/server/graph/schemas/page.graphql index 078209d8..7982dd72 100644 --- a/server/graph/schemas/page.graphql +++ b/server/graph/schemas/page.graphql @@ -40,6 +40,12 @@ type PageQuery { ): Page @auth(requires: ["manage:pages", "delete:pages", "manage:system"]) tags: [PageTag]! @auth(requires: ["manage:system", "read:pages"]) + + tree( + parent: Int! + mode: PageTreeMode! + locale: String! + ): [PageTreeItem] @auth(requires: ["manage:system", "read:pages"]) } # ----------------------------------------------- @@ -182,6 +188,19 @@ type PageListItem { tags: [String] } +type PageTreeItem { + id: Int! + path: String! + depth: Int! + title: String! + isPrivate: Boolean! + isFolder: Boolean! + privateNS: String + parent: Int + pageId: Int + locale: String! +} + enum PageOrderBy { CREATED ID @@ -194,3 +213,9 @@ enum PageOrderByDirection { ASC DESC } + +enum PageTreeMode { + FOLDERS + PAGES + ALL +} diff --git a/server/jobs/rebuild-tree.js b/server/jobs/rebuild-tree.js index 7d731f0f..a2805bc7 100644 --- a/server/jobs/rebuild-tree.js +++ b/server/jobs/rebuild-tree.js @@ -43,6 +43,9 @@ module.exports = async (pageId) => { pageId: isFolder ? null : page.id }) parentId = pik + } else if (isFolder && !found.isFolder) { + found.isFolder = true + parentId = found.id } else { parentId = found.id }