What is GraphQL ?

Basically GraphQL is a query language for APIs which is used to load data from a server to a client. Also GraphQL provides a understandable description of the data in your API. Its major aim is speeding up and boosting the APIs, their flexibility, and their effectiveness. Using GraphQL, The user can able to make a single request to retrieve required data rather than to construct several REST request to fetch the same data.

Know the difference between REST API and GraphQL ?

Most of definitely heard of GraphQL but may not be quite sure how it’s different from REST. Here, we learn about The biggest difference between GraphQL and REST.

GraphQL is an application layer server-side technology that is used for executing queries with existing dataREST is a software architectural style that defines a set of constraints for creating Web services.
GraphQL, the client requests data with queriesthe client makes an HTTP request and data is sent as an HTTP response
GraphQL can be organized in terms of a schemaREST can be arranged in terms of endpoints.
The development speed in GraphQL is fast.The development speed in REST is Slow.
The response format for GraphQL mutations should be a string.The response format for REST mutations can be anything.

How to access GraphQL

Use a GraphQL IDE such as GraphiQL or a browser extension to run the code samples and tutorials. after that If you install a browser extension, make sure it has the ability to set request headers. On Google Chrome, Altair GraphQL Client is one extension that can do the job.

Firstly exploring GraphQL, set the GraphQL endpoint by entering http://<magento2-server>/graphql in the URL bar of your IDE or extension. You can use the browser in the right column to determine how to set up a query or mutation. Examples are also available throughout the GraphQL documentation.

The following image shows a sample query, its response, and the GraphQL browser:

Main operations of GraphQL

  1. queries (for reading and receiving information),
  2. mutations (needed for taking actions, creating data, and changing information, f.e. a customer’s email),
  3. subscriptions (this operation isn’t available in Magento yet but it provides the opportunity to get data from the server in real-time automatically after a while, for instance, for notifications).

    we will explain this in detail with example in next post.