10.4 C
New York
Thursday, December 8, 2022

GraphQL: An Alternative to REST on HTTP – MUO – MakeUseOf

Find out how a GraphQL API can give you extra power and shorten your development time.
Many open-source architectural standards exist for building and distributing applications. REST (Representational State Transfer), SOAP (Simple Object Access Protocol), RPC (Remote Procedural Call), and GraphQL APIs are the most popular.
RESTful APIs are the most used API architectural standard. If you’ve written complex RESTful APIs with many endpoints, you’ve likely realized how complicated they can be. This is especially true if there are only slight differences between the endpoints.
You may also encounter problems with data fetching since RESTful APIs are not flexible enough to select specific data. GraphQL solves these problems of RESTful APIs.
GraphQL (Graph Query Language) is a query language and runtime for building APIs. Unlike REST APIs with many endpoints for consuming data, GraphQL APIs have one entry point. You can fetch specific data by describing it in queries.
The GraphQL specification defines the query language and how GraphQL servers operate. You can build and consume GraphQL APIs in server-side languages from Python to Javascript, and any language that supports HTTP.
Meta built GraphQL in 2012 as an alternative to REST for building on HTTP. They released GraphQL as an open-source standard in 2015. Today, the GraphQL foundation oversees the development of the GraphQL specification.
GraphQL is fairly new, with low adoption, and there are hidden costs to using it. Building GraphQL APIs can be unnecessarily complex, especially for small projects with a few endpoints.
Also, all GraphQL requests eventually return a status code of 200 regardless of the state of the request.
Unlike REST, which is resource-oriented, GraphQL requires that you think about data as a graph to interact with data. You can specify the structure of the data, and the specification provides a robust query interface for interacting with the API over HTTP. You’ll be able to use various features depending on the GraphQL package or library you choose to use.
GraphQL schemas include object types that define the requestable object and its available fields. On API queries and mutations, the GraphQL package validates queries and executes the queries based on the specified handler functions (resolvers).
REST is an easy-to-use standard, and most programming languages have tools for building RESTful APIs fast. However, there are many issues with building and consuming RESTful APIs.
Here are some of the issues with REST that makes developers prefer GraphQL for some use cases.
RESTful APIs relay data based on the endpoint’s specification. They aren’t flexible enough to retrieve data beyond what is hard coded in the handler function of the endpoint.
Suppose an endpoint returns a list of data on call, and you need to specify values or criteria for fields. In that case, the developer has to create an endpoint and define the business logic to return the data. You can parse the valuable resource manually, which eventually takes more time.
GraphQL solves the issue of inefficient data fetching since you can query APIs to return data based on criteria and specifications flexibly.
GraphQL APIs are interactive; you can specify the data you need to retrieve in an easy, readable syntax.
The GraphQL query above queries a user schema for entries where the age field is 89. The query has an embedded query for entries where the alive field evaluates true. It returns the name, bio, and nationality fields from the schema.
Building and consuming GraphQL APIs is easier than using REST, especially as project size increases. During the development phase, you don’t have to develop as many routes and handler functions as you will when developing RESTful APIs. Consuming GraphQL APIs is not as tedious as RESTful APIs.
In REST, different endpoints give access to different resources, unlike GraphQL, where there’s a single endpoint. This yields flexibility and performance, and queries can call different resolver functions.
The GraphQL Schema Definition Language (SDL) specifies the schemas for GraphQL services.
The GraphQL SDL syntax is easy to read and understand. You’ll specify the structure of your schema in a file with the .graphql or .graphqls extension.
The GraphQL code above is the schema for a GraphQL API defining the structure of the API for requests. The schema defines CRUD functionality for the API.
On the client side, based on the schema's structure and the client's data or operation, the client can execute a query (GET or DELETE in REST) or a mutation (PUT or POST).
Here’s an example of querying the Human schema.
The query above would return the human schema's name and age field data.
GraphQL mutations have a fairly different syntax in contrast to queries. Here’s an example mutation operation on the Human schema.
The mutation code inputs name and age fields to the client and returns the data from the fields.
You’ll need a data store for persistence when you’re building your GraphQL API. Like REST and most HTTP-based web architecture, GraphQL is stateless, and you can use any data store or database for your app.
GraphQL is a specification, and you can build GraphQL in the most popular server-side languages. You’ll have to find a library with the features you need for your project.
When choosing a GraphQL library, you want to use a feature-rich library that supports all GraphQL types and operations. Most libraries take either a schema-first or a code-first approach. In the former, you define a GraphQL schema, and the library generates resolvers and boilerplate code. For the latter, you hard-code the resolvers without defining a schema.
Since the inception of GraphQL, developers and companies have released tools to simplify its use. These can reduce the development time for smaller and medium projects.
You can check out open-source GraphQL clients, the GraphQL documentation, and its specification to learn more.
Goodness is a mechanical engineering student and software developer passionate about cloud technologies and the Go programming language.
Join our newsletter for tech tips, reviews, free ebooks, and exclusive deals!


Related Articles


Please enter your comment!
Please enter your name here

Latest Articles