Golang GraphQL

Topics Covered

Overview

Golang is a simple programming language that is fast and helps build an efficient application with ease. In this article, we will be learning about how to build an API at the backend along with using Golang GraphQL.

Introduction to GraphQL

GraphQL is developed by Facebook now known as Meta. It is mainly used for fetching APIs, data fetching, etc. if you look at the official document of GraphQL, in it, it is defined as a language mainly used for querying the APIs that provide the overall view of the data within the API. It can simply be defined as a syntax that shows how to fetch the data and load it from the server to the client. The client specifies which data needs to be fetched from the request stack which makes it easier to fetch data from multiple sources.

Prerequisites

In this article, we will be covering how to use GraphQL with Golang, and hence following are the prerequisite for understanding this better:

  • GraphQL
  • Go language

You can check out Scaler for learning the Go language.

Concepts of GraphQL

GraphQL Types

These are one of the most fundamental components of GraphQL schema definition and are represented as JSON field that contains an object which is fetched from the services. Below is an example of this concept:

In this sample example, the car has three fields namely name, id, and the model of the car.

GraphQL Queries

It is generally a way of fetching data from the client server by using GraphQL. The structure of the query would be defined by how the data is fetched, whether it's over-fetched or under-fetched. Given below is an example of the sample query which has the names of the cars following the definition type:

GraphQL Schema

Schemas are similar to that of a map. These are used for helping us understand the data by describing the relationship of the schema with the server. By doing this, the client gets to know how to structure the data and the client’s requests. These are mainly written in Schema Definition Language also knowns as SDLs.

GraphQL Mutations

These are mostly used for either creating or modifying the objects in GraphQL and are very similar to the PUT and POST methods of the REST APIs. It has syntax similar to that of queries but the only difference is that it should start with the mutation keyword.

GraphQL Subscriptions

The GraphQL subscription is mainly a feature that is used for allowing the server to send the data to the client for the event for which it was configured. To do this, the server has to maintain a smooth connection with the client to which it has subscribed.

GraphQL with Golang

Golang is a statically typed compiled programming language with in-built support for garbage collection and concurrent programming. It has features like simple, considerate, and type-safe language with automatic garbage collection. It also comes with a powerful library which is distributed as packages. The potential of using GraphQL with Golang and integrating their libraries is smooth as compared to other languages and hence the GraphQL and Golang duo is widely used. Here are a few libraries that are used for implementing golang with graphql smoother:

  • Graph-gophers/graphql-go - this library is used for parsing the schema language of GraphQL when the resolver info is to be given during the compilation time without even waiting for the runtime to detect the problem with the schema of GraphQL and the resolver.
  • GraphQL-go/graphql - this library is used for queries, mutations as well as subscriptions just like other libraries. It also has the implementation of the library GraphQL-js which makes it useful.
  • Gqlgen- it is used for prioritizing the safety type of the code and is dependent on the schema-first approach.

Each of these libraries mentioned above has a folder for examples where we can check the implementation of how these libraries are implemented.

Applications of GraphQL

Schema Homogeneity

The library `graphql-go/graphic is a featured implementation of GraphQL in the Go language that supports various other features like mutations, queries, subscriptions, etc. This library, however, has an advantage when compared with other libraries because this library helps in building production-ready servers for GraphQL. This library is also an official reference implementation of the language GraphQL.js. The use and benefit of the near-identical API with GraphQL.js make it easier to read and write GraphQL schema written for other languages like JavaScript or Node.js.

To understand this better, let’s take an example and try to understand the GraphQL schema definition for golang along with GraphQL

When we import the package graphql-go and define the type of object GraphQL named BaseQuery with a hello field for creating the schema.

By creating the sever of GraphQL after the creation of schema, the following steps that are used are mentioned in the documentation properly.

Write a code where GraphQL schema definition is used with GraphQL.js along with JavaScript.

In the code given below, we will first require the package GraphQL and will define its object type which is BaseQuery with a hello field.

When we take a look at the above twp representation of the code, we can conclude that implementing GraphQL in the Go language by using the package graphql-go is easy.

Solving the n+1 Problem

The REST API uses one resolver that too per API endpoint while the GraphQL does the execution of one resolver per field. From this, we can understand that there are many resolvers for a single request, especially for data that is nested. This problem is known as the n+1 problem.

Look at the query given below to understand this concept much better

Explanation: In this example, however, it is not shown how quickly this problem can arise but if we assume that the data is to be fetched for 200 authors then the resolver executed for the 200+1 trip for fetching the response. Although this could have been done in just two trips that is request and response.

To fix this issue, the concept of batching is used. Batching is the primary feature of the DataLoader. It was developed by Facebook in the year 2010 for making use of GraphQL for fetching data in applications. It is used for providing simple efficient API as compared to remote data sources like caching or batching.

Look at the example given below to understand how to use DataLoader in the code

Irrespective of the library that you use, the DataLoader is used for solving the n+1 problem by allowing the resolver in GraphQL to request the data and in return giving the promise for the data instead of returning the exact request of the field.

Client Server Data Scheme Mismatch

Normally when coding the backend of the application with GraphQL, the codes get duplicated since a similar kind of schema is used for the database and API endpoint.

For instance, look at the example given below:

In this, a query from the database of the server would return the property Id of the car but when the same query is used for the car directly from the server it would return the property of the car. To simplify this, the server-server query is used.

Examples for Understanding

Consider an example given below to understand this concept better

Define the Schema

Implementing the Resolver

Run the code or server

Open http://localhost:8080 in a browser to try the output.

Conclusion

  • Golang is a simple programming language that is fast and helps build an efficient application with ease. In this article, we will be learning about how to build an API at the backend along with using Golang GraphQL.
  • GraphQL is developed by FaceBook also known as Meta. It is mainly used for fetching APIs, data fetching, etc. if you look at the official document of GraphQL, in it, it is defined as a language mainly used for querying the APIs that provide the overall view of the data within the API. it can simply be defined as a syntax that shows how to fetch the data and load it from the server to the client.