GraphQL is a data fetching language that allows clients to declaratively describe their data requirements with a JSON-like format. In this respect, it is comparable to SQL. Furthermore, GraphQL is database agnostic. Its design allows you to deal with any kind of database.
What about REST? For a long time, REST was thought to be the only appropriate tool for building modern APIs. But not any longer. You might want to follow the discussion ? on API Design in the Book on REST & GraphQL.
GraphQL aims to invert the control over the data, giving control to the API consumer. It gives them the possibility to describe their data needs, while also optimizing the entire experience, solving many of the problems of the custom endpoint approach. Instead of working with rigid server-defined endpoints, you can send queries to get exactly the data you’re looking for in one request. You could thus say, it is an API-consumer-centric approach.
You might ? wonder: Why are there two languages in GraphQL? GraphQL helps to keep the two aspects of data retrieval and data type definition separate:
(1) GraphQL provides the capabilities to describe the data served by the API with a rigorous type system. But this description doesn’t tell you anything about how those objects might be fetched from a client. The space of accessible data is described as a type system in a schema.
(2) GraphQL provides the capabilities to form complex queries over the data provided by the API, including joins over different data types. The selection of fields that are part of the response is determined by the client. The query language allows for packaging complex queries (involving links between different objects) into a single request.
GraphQL offers clients both reading and writing access to the data, i.e. clients can request data from the server or send data to the server. In addition, GraphQL provides a subscription mechanism for communication in the other direction, so servers can notify selected clients.
In GraphQL, the shape of the response is built up by the GraphQL execution library to match the shape of the query. Essentially, you can think of GraphQL as a system for calling many nested endpoints in one request. Almost like a multiplexed REST.
Want to ? dive deep into GraphQL? Check out the Book on GraphQL API Design.
Want to get your hands dirty and try ?? some GraphQL, without all the hassle of having to set up a server and do all the maintenance later on? Get up and running with serverless GraphQL. Learn how in the Book on Serverless GraphQL with Amazon’s AWS AppSync.