From ae60311e856700b9cce538f4324c298ac3f3e7e1 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Sat, 22 Sep 2018 00:45:48 -0600 Subject: [PATCH] feat: added root parent edge --- .../server/graph/tenant/resolvers/comment.ts | 25 +++++++++++++++++++ .../server/graph/tenant/schema/schema.graphql | 5 ++++ 2 files changed, 30 insertions(+) diff --git a/src/core/server/graph/tenant/resolvers/comment.ts b/src/core/server/graph/tenant/resolvers/comment.ts index 8690c5cb4..9272a294c 100644 --- a/src/core/server/graph/tenant/resolvers/comment.ts +++ b/src/core/server/graph/tenant/resolvers/comment.ts @@ -24,6 +24,31 @@ const Comment: GQLCommentTypeResolver = { parentCount: comment => comment.parent_id ? comment.grandparent_ids.length + 1 : 0, replyCount: comment => comment.child_ids.length, + rootParent: (comment, input, ctx, info) => { + // If there isn't a parent, then return nothing! + if (!comment.parent_id) { + return null; + } + + // rootParentID is the root parent id for a given comment. + const rootParentID = + comment.grandparent_ids.length > 0 + ? comment.grandparent_ids[0] + : comment.parent_id; + + // Get the field names of the fields being requested, if it's only the ID, + // we have that, so no need to make a database request. + const fields = getRequestedFields(info); + if (fields.length === 1 && fields[0] === "id") { + return { + id: rootParentID, + }; + } + + // We want more than the ID! Get the comment! + // TODO: (wyattjoh) if the parent and the parents (containing the parent) are requested, the parent comment is retrieved from the database twice. Investigate ways of reducing i/o. + return ctx.loaders.Comments.comment.load(rootParentID); + }, parent: (comment, input, ctx, info) => { // If there isn't a parent, then return nothing! if (!comment.parent_id) { diff --git a/src/core/server/graph/tenant/schema/schema.graphql b/src/core/server/graph/tenant/schema/schema.graphql index 8a3a5e007..4b2d9ca23 100644 --- a/src/core/server/graph/tenant/schema/schema.graphql +++ b/src/core/server/graph/tenant/schema/schema.graphql @@ -665,6 +665,11 @@ type Comment { """ parent: Comment + """ + rootParent is the comment that is not a reply to another Comment. + """ + rootParent: Comment + """ parents returns a CommentsConnection that allows accessing direct parents of the given Comment.