×
How to use GraphQL in Spring: An easy step-by-step tutorial

How to use GraphQL in Spring: An easy step-by-step tutorial

How to use GraphQL in Spring: An easy step-by-step tutorial

Why GraphQL?
 

Given that there are already standards like SOAP and REST for exposing APIs, you might wonder about the motivations for creating GraphQL. 

To address this, let’s look at REST for a moment. REST is a way to model resources over HTTP; generally speaking, each request operates on a single resource, operates on the entire resource, and indicates no schema or type system. While REST is very popular due to its simplicity, these architectural assumptions tend to create very chatty APIs that rely on the client to interpret the types. Thinking about a single resource at a time can also make things like domain-driven design less of a natural fit for REST.

GraphQL approaches the problem of fetching data differently; it’s a query language (like SQL is a query language) that allows clients to fetch data how they need it returned. Behind the query language is a query engine that invokes the correct data fetchers based on the parsing of the query.

Where REST is a set of principles that describe how a server can expose data to a client, GraphQL is a client-side query language that clients use to describe to the server the data that it wants. This means a client can more easily query across resources (think SQL table joins) and retrieve only the data it needs (think SQL SELECT and WHERE clauses) without the server needing to anticipate that specific query in advance.

Read More