A beginner's guide to the Graph query language (GRAPHQL)

A beginner's guide to the Graph query language (GRAPHQL)

Every web or mobile-based apps have APIs(application programming interfaces) that encapsulate the business logic that powers them. These APIs serve as layers that exist on a server which allows clients to carry out specific operations on them known as CRUD requests, based on the server's configuration and use case. In this article, I'll introduce and explain how Graphql can be used to create APIs that are fast, efficient, and flexible, unlike traditional REST APIs.

What is GraphQL?

Graphql is a query language that is used to query and mutate data that exists on a graphql server. Graphql is defined by its specification which means it is language independent. Graphql servers can be implemented in any programming language of your choice. This graphql specification defines the rules on how graphql servers can be implemented in various languages and how graphql schemas should be defined.

Why should you use GraphQL?

GraphQL was designed by Facebook in 2012. Its inception was a result of rebuilding Facebook's native applications. Many giant companies such as Netflix and Github use GraphQL in their production apps because graphql has proven to be fast, efficient, and production-ready. GraphQL also has a core community built around it such as Apollo and Prisma.

GraphQL was designed originally for data fetching in mobile applications but in 2015 Facebook open-sourced graphql and since then, its use extended into web applications. This sudden adaptation of graphql was because of the power it gives to client applications to get the exact data they need from the server unlike REST APIs where data are represented in form of resources in different endpoints/URLs, but graphql exposes a single endpoint(usually /graphql but can be changed) which can be used to get every data that exists in its server which reduces API complexity. Graphql does this by sending a graphql document as a payload to each request a client makes to the server and the server interprets this document and gets the exact data we need.

GraphQL VS REST

graphql_vs_rest.jpeg

GraphQL has a lot of reasons why it's referred to rather than REST. I'll use a real-life scenario to buttress these points. Let's assume you are designing an API for an e-commerce application and your client needs data or information on a specific product such as its category and reviews. With the REST API design approach, the data would be defined with a specific structure on the following endpoints:

From the example above, the client has to hit two endpoints to get the data that it needs. In most cases, the client doesn't need all the properties that exist in each endpoint but it has no way of specifying what data it needs at each API call because of the REST API rigid design structure. This is usually referred to as over-fetching of data. In some scenarios, the client has to make several API calls to get the data that satisfy its use case. This is referred to as under-fetching of data. Graphql solves the problem of over-fetching and under-fetching, by giving power to the client to specify the exact data it needs in a single graphql query with a single endpoint. In our e-commerce example, it would look something like this:

query {
  product {
    category {
      id
      name
      createdAt
      updatedAt
    }
    reviews {
      id
      comment
      rating
    }
  }
}

You can see how graphql makes getting data much easier for the client without the need to make several API calls to meet its need. Graphql also has other benefits such as automatic documentation of queries, mutation, and other graphql types (more on these in future articles), unlike REST where we write documentation ourselves using tools like Swagger or Postman. This graphql automatic documentation feature reduces API development time which allows developers to develop API faster. Additionally, graphql does not need API versioning as REST does.

Conclusion

Graphql provides a handful of features that makes API development much easier to implement and also allows us to implement business logic in a graphql schema rather than several API endpoints. Graphql has also proven to be production-ready and efficient as it is used by gaint companies in their backend implementation. Graphql can also be implemented in any language of your choice and I suggest you check your language implementation of graphql and you'll see how great graphql is. NOTE: This is just a beginner's guide to graphql. I'll be writing more about graphql features and advanced concepts in future articles.

GraphQL resources