Homunculus Reticulli
Homunculus Reticulli

Reputation: 68476

GraphQL Schema - how to represent (and query) a hierarchical structure

I am using GraphQL to build a "proof of concept" service.

One of the objects I am modelling in the Schema, is an Organisation's structure. Typically, an organisational structure is hierarchical, and different nodes could have different number of child nodes etc.

Typically, an org chart looks a bit like this:

                  CEO
                   |
   ----------------------------------
   |                 |              |
  Tech Team    Middle Management   ...

I a sure this is a problem that has been solved several times, in GraphQL applications - however, I can't seem to find any documentation that shows how to represent a hierarchical structure (such as an org. chart), using GraphQL

type Organisation {
   # Root Node here ...
}

How can a hierarchical structure be represented in a GraphQL Schema?

Upvotes: 3

Views: 371

Answers (1)

Michel Floyd
Michel Floyd

Reputation: 20256

A type can refer to itself. In your case an employee can have a supervisor (except for the CEO who has none) and can also optionally have staff.

type Employee {
  id: ID!
  firstName: String!
  lastName: String!
  supervisor: Employee
  staff: [Employee]
}

Upvotes: 3

Related Questions