GraphQL Types and Relationships

Kingdom Orjiewuru
Software Insight
Published in
3 min readJul 24, 2017
Origin: graphql.org

By the time you finish reading this article, you should have a clear understanding of GraphQL types and schema, also you should be able to create those yourself. If you’re unfamiliar with the concept of a GraphQL schema, don’t worry; you can think of it as a GraphQL server’s capabilities.

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

The Schema Definition Language (SDL)

Before we do a deep dive into GraphQL types and relationships, let us first discuss about the language used to write those.

A schema definition is the most concise way to define a GraphQL query. To define the schema of a GraphQL API, you have to use the type system provided by GraphQL. The syntax for writing schemas 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 can be written in any programming language that implements the type system.

Types

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!}

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).

Relations

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!}

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!]!}

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.

Conclusion

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; and after that, we’ll combine the knowledge to create a simple API for a document management system. Please stay tuned.

[1] A field is a key that holds a value(s) or other fields. There are two types of fields in GraphQL — scalar fields and complex fields. Scalar fields are the basic types in a GraphQL schema. They represent primitive values like String, Integer, Float, Boolean. Complex fields are fields that contain other data in them.

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

If you have any questions or observations, use the comment session to share them.

Connect with me Twitter | Github

--

--