Apr 04, 2020
Guide to GraphQL
What is GraphQL
GraphQL is a Query Language for APIs that describes how to ask for data and is generally used to load data from a server to a client. Firstly, it doesn’t have anything to do with databases. It is not an alternative to SQL. It is basically used to send/receive data between client and server using a minimum number of HTTP requests. In order to achieve this, you have to create relations between the entities programmatically. That’s how it is called as a query language.
GraphQL and Graph architecture
In computer science, the Graph represents structure used to model pairwise relations between various objects(or nodes). Understanding, using and thinking in graphs makes us better programmers. At least that’s how we’re supposed to think. Graph-based APIs model the data in terms of nodes and edges (objects and relationships) and allows the client to interact with multiple nodes in a single request. With GraphQL, you model your business domain as a graph by defining a schema. within your schema, you define different types of nodes(data sources) and how they connect/relate to one another. That’s how it is called GraphQL.
Query Language for APIs
GraphQL is a Query Language for APIs that describes how to ask for data and is used to load data from a server to a client.
GraphQL vs REST API Using GraphQL we can solve over-fetching and under-fetching issues. because, with the GraphQL, the client gets only the data that it asked for and with a minimum number of requests. Unlike REST showing HTTP error codes for an error, GraphQL will provide a detailed error message including all the resolvers and referring to the exact query/mutation part at fault.
GraphQL uses a single(or minimum number of) endpoints to send/receive data between client and server. it can handle all your application operations with a single endpoint. By using a single, evolving version, GraphQL APIs give apps continuous access to new features and encourage cleaner, more maintainable server code.
GraphQL comes with self-documenting capability. i.e. autogeneration of API documentation based on GraphQL schema. GraphQL keeps documentation in sync with API changes. So no need to re-document everything for API changes. Using the in-built GraphQL browser, you can experiment with the API and the data it exposes.
Schema, Mutations and Resolvers
The GraphQL schema is at the center of every GraphQL server. It defines the server’s API, allowing clients to know which operations can be performed by the server and what fields and types that every query returns. A GraphQL operation can either be a read or a write operation. GraphQL query is used to read or fetch the data while a mutation is used to write or alter the data. subscriptions are a GraphQL feature that allows a server to send data to its clients when a specific event happens. A resolver is a function that is responsible for fetching data from the backend(Read Operations) and sending data to the backend(CUD Operations).