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:

[pastacode lang="javascript" manual="type%20User%20%7B%0A%20%20name%3A%20String!%0A%20%20username%3A%20String!%0A%7D%0A" message="GraphQL Type" highlight="" provider="manual"/]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:

[pastacode lang="javascript" manual="type%20Document%20%7B%0A%20%20title%3A%20String!%0A%20%20content%3A%20String!%0A%20%20author%3A%20User!%0A%7D" message="GraphQL Relation" highlight="" provider="manual"/]

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:

[pastacode lang="javascript" manual="type%20User%20%7B%0A%20%20name%3A%20String!%0A%20%20username%3A%20String!%0A%20%20email%3A%20String!%0A%20%20noOfDocuments%3A%20Int!%0A%20%20documents%3A%20%5BDocument!%5D!%0A%7D%0A" message="GraphQL Relationships" highlight="" provider="manual"/]

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

The latest articles from Andela.

Visit our blog

Top takeaways from Gartner IT Symposium

As the symposium concluded, it became evident that the journey into the AI-driven future is both challenging and exhilarating for IT leaders.

Android ML face detection with Camera X

In this Writer's Room tutorial, Andela Community member Stephen Henry explains how to integrate ML face detection into an Android app using CameraX.

Advance your career in: Application Engineering

Serving as a bridge between the engineering and business sides of an organization, application engineers are highly sought after - and by upskilling in this field, you can set yourself up for an incredibly impactful and lucrative career.

We have a 96%+
talent match success rate.

The Andela Talent Operating Platform provides transparency to talent profiles and assessment before hiring. AI-driven algorithms match the right talent for the job.