Want to be More Productive? Checkout my Open Source Productivity Application

GraphQL For Beginners: Part #1 The Terminology

In this tutorial series, We will dig deeper into GraphQL and develop an Awesome application using GraphQL, Node, Express and MongoDB.
Let's understand the terminology used alongside GraphQL.

Here's the links for GraphQL Website and its Github Repositories

Before we get started, This is not an official representation of GraphQL, This is how I interpreted things, How I understood these different terms and how I think this works.

When building an application using GraphQL, There are few new terminologies we will come across, Some of them are

  • Type
  • Query
  • Mutation
  • Resolve
  • Subscription
  • Schema

Let's dig deeper into each of these terms and understand what it means.

Type

As the name says Type is the type of Object/Field we're creating or accessing.

If you have used databases you must already be familiar with this as we have different types for fields in Databases like String, Integer, Float, Number, Varchar, Text, etc.

It's just like that, Here we have different types which we can associate with fields.

In GraphQL We have to associate every field with a Type.

The most common ones are:

GraphQLObjectType
This is used for defining objects, Each field in the object will have its own type
GraphQLID
For fields that represent ID of a particular record., Ex: MongoDB _id field
GraphQLInt
This type is used when we expect the field value to be a number.
GraphQLString
This is used when the field value is expected to be String/Text.
GraphQLFloat
This is used for floats
GraphQLList
This is used when we expect the field value to be an Array.
GraphQLNonNull
This can be used with any Field Type, We use this when we want to mark a field as Required.

There are many more field Types, You can find the full list here http://graphql.org/learn/schema/.

Along with the existing Types, We can very easily create our own Field types, This can be useful for Email, URL, Date, Phone, Address, etc fields.

Query

Its like a GET request, Everytime we want to access the data for reading, We make use of Queries.

We can specify as many queries as we want in our GraphQL Application and we can call those queries as needed.,

We can pass optional or required parameters to the query as well, And we can use those parameters internally to filter and return the correct data.

Anyways, Query doesn't fetch the data, It acts as a proxy, It processes the incoming GraphQL request and calls the resolve method and passes along all the parameters, Its the job of resolve method to fetch the data from any source and return it back to the query.

The query then checks the data for correct field types and returns it back to the client.

Mutation

Just like queries, Mutation is used whenever we want to manipulate the data, This is used for Create, Update and Delete operations.

Everytime we want to add/manipulate a record, We call GraphQL Mutation and pass all the arguements/parameters., The mutation then calls the resolve method and passes along all the data it receives.

Here we do wherever we want with the data and however we want to do it, once done we return the response back to the mutation, which in turn will return the response back to the client. (after validating its field types)

Resolve

This is familiar territory., We use resolve for fetching and manipulating the data. How we want to do it is left entirely on us.

We can fetch the data from Database, API, File, or any datasource we can imagine/have access to, GraphQL doesn't restrict us.

In resolve method we can easily return a Promise which makes our code non-blocking, is good practice and gives us more freedom on data fetching and manipulation.

Once we have the data, We return it back to the handler., Which then validates the data, its type and structure.

The field type must match the type we specify while creating a Record Type otherwise error will be thrown. (lot of types here).

After successful validation, The data is returned back to the client.

Subscription

In its simplest terms, Subscriptions are like queries, but everytime the data changes the query is run and new response is sent back to all* connected clients.

We have to specify on our own when the data changes, Which query its associated with and which and all clients to update.

This is relatively new, But a step forward in the direction of real time updates.

Schema

Its here where we connect everything together.

Here we create a RootQuery(name can be anything) and here we specify all the queries we have written.

Similarly, We create a RootMutation and specify all the mutations we have written.

Finally we return a GraphQLSchema with this query and mutation object.

The server we create will make use of this GraphQLSchema object and handle things from there.

Ending Notes

Give GraphQL a try, You will fall in love with it.

I myself was sceptical of GraphQL, But this Udemy course by Stephen Grider piqued my interest.

I developed an Open Source application using GraphQL and the experienc was great.

You can checkout the Application here https://github.com/dhruv-kumar-jha/productivity-frontend.

In upcomig tutorials we will setup our Express server, MongoDB and Write our first Type, Query and Mutation. And then the app (which i still haven't decided what it will be.)

If you have any questiosn/queries/feedback, Feel free share them here, on twitter or by contacting me.