GraphQL: Types and Relationships

This is a multi-part tutorial by Kingdom Orjiewuru, an Andela Developer based in Lagos.

By the time you finish reading this article, you should have a clear understanding of GraphQL types and schema to the extent that you can create them yourself. And if you’re an absolute beginner, don’t fret—think of GraphQL schema as just a way to describe relationships in your API.

Note: We’ll use a simple document management system to illustrate the concepts we’ll be discussing here.  

The schema definition languages (SDL)

Before we do a deep dive into GraphQL types and relationships, let’s discuss the language used to write them.

So first, a schema definition is the most concise way to define a GraphQL query. To define the schema of a GraphQL API, you’ll have to use the type system provided by GraphQL. The syntax for writing it is called Schema Definition Language.

The main components of a schema definition are the types and their fields.

A GraphQL schema describes the types, shapes, and relationships in your data, and it can be written in any programming language that implements the type system.


Types are used to describe the set of possible data you can query from a service. Here’s an example of how to define a type in GraphQL:

type User {
  name: String!
  username: String!
GraphQL Type
The User type we just described is a GraphQL Object Type which means that it is a type that contains some fields.

Our User type has two fields: name and username, and they’re both of type String¹.

The ! following the types indicates that the field is required(non-nullable).


In GraphQL, you can express relationships between types. In the example document management system mentioned at the beginning of this article, a User could be associated with a Document as described below:

type Document {
  title: String!
  content: String!
  author: User!
GraphQL Relation

Looking at the author field, you’ll notice it’s associated to the User type we previously created.

Conversely, we’ll go ahead and associate our User type to the Document type we just created and add some new fields. Here’s how our User type will currently look:

type User {
  name: String!
  username: String!
  email: String!
  noOfDocuments: Int!
  documents: [Document!]!
GraphQL Relationships

We have just created a one-to-many relationship between User and Document. Notice that the documents field will contain an actual array of documents.


We’ve briefly discussed how to create GraphQL types and also create relationships between those types. We’ve also tried out some examples of those.

I hope you enjoyed this article! We’ve just scratched the surface of GraphQL features. Next up will be queries, mutations, and subscriptions. After that, we’ll combine the knowledge to create a simple API for a document management system. Please stay tuned.

Special thanks to Peggy Rayzis and Eric Baer for reviewing this article.

Related Posts