GraphQL: Introduction to Query Language for APIs

DZone 's Guide to

GraphQL: Introduction to Query Language for APIs

In this brief article, take a look at GraphQL and see an introduction to the query language for APIs.

· Integration Zone ·
Free Resource


The emergence of single-page applications and diversified platforms, such as mobile, web, and tablets require catered endpoints for varying layouts. In the case of traditional service-oriented architecture, the API layer decides the structure of the response, and this results in a rigid design of endpoints, and any change in response requires change on the server-side.

Common problems that arise out of REST endpoint are over fetching and under fetching, where the client would be dumped either with a huge set of responses, which is not required or needs to make multiple calls to the server to render a single page.

Current State

Assume that we are developing a web application for an e-library, where the list of top trending books needs to be displayed. For rendering book details, basic details of the book, and its corresponding author, comments needs to be fetched. For fetching information for 5 books, 15 requests need to be made to the server. In addition, for each of the platforms, there requires a different dimension of the same data, where mobile would require minimal data, and web would require a rich set of information.


GraphQL is a query language for APIs where we would be able to define the specification of the structure of data, which gives the clients the flexibility to request what is needed and nothing more, which makes the API evolve over time.

Schema Definition Language

In GraphQL, the contract is established through server and client through schema definition language, which will ease the flow of communication of client when requesting the data, unlike in REST, information is encoded over REST endpoints with resources.

Sample Schema

Plain Text

Components of GraphQL

There are 4 major components available in GraphQL. Once the contract is established through schema definition language, communication between the API and Client will happen through Query, Mutation, and Subscription. Resolver helps the server to fulfill the action requested by client request in POST request object for query, mutation, and subscription.

  • Query.
  • Mutation.
  • Resolver.
  • Subscription.


Query facilitates an interactive form of communication between clients and services. Based on the fields requested by the client, the structure of the response would vary. In addition, it also has a provision to pass arguments and directives to control the structure of the response based on condition.


Mutation helps to change the state of the object in the server and returns back the fields requested.


Subscription enables the server to send real-time information to the client and acts similarly to WebSockets.


Resolvers act as an intermediary between the client and server, which interprets each client request and performs certain actions and returns response.

Sample Source Code Using GraphQL



GraphQL offers a simple specification to empower clients to make interactive and complex queries, further schema-based development helps API's to evolve over time. It offers a flexible response structure to change them dynamically based on the platform to bring more agility to development.

api ,graphql ,graphql components ,integration ,schema definition language

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}