Skip to content

GraphQL: add _children field for downward hierarchy traversal #91

Description

@mitselek

Problem

There is no way to traverse downward in the entity hierarchy via GraphQL. Given an Organization, you cannot discover its child entities without a separate query. The _parent property lets you look up, but there is no corresponding field to look down.

Suggested change

schema.js (~line 254), add to output fields:

const outputFields = [
  // ... existing system fields ...
  '  "Child entities"',
  '  _children: [ReferenceValue]'
]

resolvers.js, add field resolver:

TypeResolvers[typeName] = {
  _children: async (parent, _, { entu }) => {
    const children = await entu.db.collection('entity').find({
      'private._parent.reference': parent._id,
      access: entu.user ? { $in: [entu.user, 'domain', 'public'] } : 'public'
    }, {
      projection: { _id: 1, 'private.name': 1 }
    }).toArray()

    return children.map(c => ({
      _id: c._id,
      reference: c._id,
      string: c.private?.name?.[0]?.string || c._id.toString()
    }))
  }
}

Example query this enables

query {
  organization(filter: { _id: "org-id" }) {
    _id
    name { string }
    _children { reference string }
  }
}

Use case

Polyphony PoC: browsing an Organization's Roles and Members inline. Combined with #89 (_parent filter), this gives both directions of hierarchy traversal.

~20 lines changed, purely additive.

Split from #88

(ER:Codd + Saavedra)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions