cover-img

Schemas and Types

8 July, 2023

1

1

0

In the previous blog, we learned about the mutation and queries in GraphQL.
We also discussed that querying ing GraphQL is basically selecting necessary fields from an object. Today we will talk about schemas and types in GraphQL.

Consider this example below,

Input:

hero {
name
appearsIn
}
}

Output:

{
"data": {
"hero": {
"name": "R2-D2",
"appearsIn": [
"NEWHOPE",
"EMPIRE",
"JEDI"
]
}
}
}

Just by observation, we can say that the query matches the output which we receive. We can intuitively predict the output most of the time. But it is a good practice to have an exact description of the data we want and not just predict the output. This is where the role of Schema comes into the picture.

Schemas and Types

A schema is simply the representation or blueprint of a GrapQL API.
And to construct the schema, we must understand what types are.
Types can be of different categories:

  1. Scalar Types:

They are the most common types used in a schema. They are atomic values and cannot have sub-fields. Meaning to say that they cannot be further divided into another data type.
Consider the following example to know the five main scalar types,

type User {
id:ID!
name:String!
age:Int!
height:Float!
isMarried:Boolean!
}
  • ID: It is used as a key or identifier for objects.
  • String: It represents textual data. It's a UTF-8 character sequence.
  • Int: A signed 32-bit integer.
  • Float: A signed double-precision floating point value.
  • Boolean: true or false value.

This is about the different scalar types that are available.


Also, the term User indicates the name of the Object Type.

And If you notice the above example carefully, you might wonder what the exclamation mark (!) is doing there.
It is to say that the field is non-nullable, meaning that the GraphQL service promises to always give back a value when we query this field and it can never be a null value.

  1. Object Type:

They represent entities with their own fields and types.

type Post {
id: ID!
title: String!
content: String!
author: User!
}

Here you can see that the author field has the type 'User' and it is not an atomic value rather it is a type per se and has been used in the above Scalar example.

  1. Query Type:

Serves as the entry point for making queries and defines the available data that can be requested from the GraphQL API.

type Query {
getUser(id: ID!): User
getPosts: [Post!]!
}
  1. Mutation type:

Defines the operations to create or update data.

type Mutation {
createUser(name: String!, email: String!): User!
updatePost(id: ID!, title: String, content: String): Post!
}
  1. Enum type:

Define a predefined and restricted list of options for a field, providing a way to represent a finite set of values.

enum Role {
ADMIN
USER
MODERATOR
}
  1. Subscription type:

Provides real-time capabilities by allowing clients to subscribe to specific events or changes in data.

type Subscription {
newPost: Post!
}
  1. Interface type:

Define a set of shared fields that can be implemented by one or more object types, enabling polymorphism and creating common behavior across types.

interface Animal {
id: ID!
name: String!
}

type Dog implements Animal {
id: ID!
name: String!
breed: String!
}

type Cat implements Animal {
id: ID!
name: String!
color: String!
}
  1. Union type:

Allow a field to return one or more object types, providing flexibility in representing different possible return types for a field.

union SearchResult = Post | User | Comment


Hands-On:

There aren't really many publically available GraphQL APIs out there as much as REST APIs but we can make use of which are available. One such API is the Countries GraphQL API

Link: https://countries.trevorblades.com

Now you can go to this link and test the API. One example query you can try is getting the country information,

query GetCountry {
country(code: "US") {
name
native
capital
emoji
currency
languages {
code
name
}
}
}

Output:

{
"data": {
"country": {
"name": "United States",
"native": "United States",
"capital": "Washington D.C.",
"emoji": "πŸ‡ΊπŸ‡Έ",
"currency": "USD,USN,USS",
"languages": [
{
"code": "en",
"name": "English"
}
]
}
}
}

We are defining the fields that we require in the output. If you remove the 'capital' field from the query, it won't be in the output as well.

Conclusion

Thank you for reading this article. In this article, we have covered what are schemas and types in GraphQL.

I hope this article was helpful and please like and share it with your friends.
Until the next article in the series, happy reading!

1

1

0

Shashank Bhat G S
Intern @Sahaj Software | Prev CCO @ Showwcase | Web-developer | Blogger

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

Β© Copyright 2025. Showcase Creators Inc. All rights reserved.