Know your Graphql

Rashad Muntar
3 min readSep 13, 2022

Presumably, if you are reading this, you are a human being. As a human, you have a number of interests and passions. You also have family members, friends, acquaintances, classmates, and colleagues. Those people also have their own social relationships, interests, and passions. Some of these relationships and interests overlap, while others do not. Altogether, we each have a connected graph of the people in our lives. These types of interconnected data are exactly the challenge that GraphQL initially set out to solve in API development. By writing a GraphQL API we are able to efficiently connect data, which reduces the complexity and number of requests while allowing us to serve a client precisely the data they need.

Graphql Basics

Let’s take a few moments to step back and look at the different pieces of a GraphQL API. The two primary building blocks of a GraphQL API are schemas and resolvers. By understanding these two components, you can apply them more effectively to your API design and development.

Schemas

A schema is a written representation of our data and interactions. By requiring a schema, GraphQL enforces a strict plan for our API. This is because your API can only return data and perform interactions that are defined within the schema. The fundamental component of GraphQL schemas are object types which returns scalar types. GraphQL contains five built-in scalar types:

String: A string with UTF-8 character encoding
Boolean: A true or false value
Int: A 32-bit integer
Float: A floating-point value
ID: A unique identifier

example of Graphql Schema

Graphql schema type
Schema Query to retrieve data in the form of the type declared above

Resolvers

The second piece of our GraphQL API will be resolvers. Resolvers perform exactly the action their name implies; they resolve the data that the API user has requested. We will write these resolvers by first defining them in our schema and then implementing the logic within our JavaScript code. Every Graphql resolver contains two basic building blocks: queries and mutations.

Queries

A query requests specific data from an API, in its desired format. In our hypothetical note API, we may write a query that will return a full list of notes and another that will return detailed information about a single note. The query will then return an object, containing the data that the API user has requested. A query never modifies the data, only accesses it.

Query inside a resolver

Mutations

We use a mutation when we want to modify the data in our API. In our notes example, we may write a mutation that changes the content for a given notes and another that allows us to change the author of the note. Similar to a query, a mutation is also expected to return a result in the form of an object, typically the end result of the performed action.

Conclusion

If you are able to understand these basic building blocks of Graphql, you will be able to build any Graphql API using not just Javascript but different languages.

--

--

Rashad Muntar
0 Followers

Full-Stack Engineer JavaScript, Ruby, C++, Node.js, Rails and React. In love with pair-programming and remote work