Huy Nguyen
Huy Nguyen

Reputation: 3010

Representing hierarchical types in GraphQL

This is an example of a problem I'm trying to solve. Suppose I want to create a GraphQL schema that models administrative entities (countries, states, provinces etc, represented by type AdministrativeEntity) of cities (type City) around the world. Each city belongs to a list of entities that have hierarchical relationship to each other. However, the number of "levels" of a city's administrative entities may differ. For example, "Chicago" belongs to "United States" and "Illinois" (which has two levels) but Singapore only belongs to "Singapore" (only one level). How should I represent the type AdministrativeEntity in my GraphQL schema? Should it be a scalar, an Object type or something else?

type City {
  entity: AdministrativeEntity!
}

Upvotes: 1

Views: 484

Answers (1)

xadm
xadm

Reputation: 8418

You can use recurrency - parent property in AdministrativeEntity type. This is the most universal, expandable, manageable.

... but for savings (on scale) you can use simple [ordered] array of names.

Of course you can use both solutions (2 properties) in the same time (2 resolvers operating on the same data/db).

Upvotes: 1

Related Questions