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.
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:
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:
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:
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.
The inaugural edition of Talks At Andela happened this October, and we had none other than the excel...
The concept behind Lean UX is that the first design iteration isn’t conclusive, so don’t spend t...
We recently held our inaugural launch ceremony for Fellows completing the Andela Fellowship. Rukayat...
Tap into a global talent pool and hire the “right” developers in days, not months.
Accelerate your career by working with high-performing engineering teams around the world.BECOME A DEVELOPER
We take great pride in matching our developers with the best partners. Tell us about your team below!