REST
SOAP
OData
CORBA
JSONAPI
SWAGGER
RAML
RDF
OWL
With REST, the server determines what data will be sent down to the client.
If you need two different views of the same object (for example summary and detail views), you have to make two separate endpoints.
REST has no versioning mechanism, so every time you refactor your server code and your data schema, you have to be mindful that there will be old clients that will still require the data in the old format
{
design(uuid: "z-6VpPknv") {
hash
furniture_type
}
}
{
"data": {
"design": {
"hash": "4c8763222e16f53fc4259da1182e11bdeda661f6",
"furniture_type": "shelf"
}
}
}
FE
UserAPI
DesignAPI
ElementsAPI
GET user designs
List the price of each furniture part of a user's saved designs
GET structure of each design
GET price of each furniture part
GraphQL’s power comes from a simple idea — instead of defining the structure of responses on the server, the flexibility is given to the client.
...But you can't go and refactor all your APIs to use GraphQL...
In an endpoint-based API, clients can use HTTP caching to easily avoid refetching resources, and for identifying when two resources are the same. The URL in these APIs is a globally unique identifier that the client can leverage to build a cache.
var _cache = new Map();
rest.get = uri => {
if (!_cache.has(uri)) {
_cache.set(uri, fetch(uri));
}
return _cache.get(uri);
};
It is very common for the results of multiple queries to overlap. However, our response cache from the previous section doesn't account for this overlap
In GraphQL we can normalize the hierarchical response into a flat collection of records
query {
story(id: "1") {
text,
author {
name
}
}
}
query: {
story: {
text: "MYCS",
author: {
name: "Nikolas"
}
}
}
Map {
// `story(id: "1")`
1: Map {
text: 'MYCS',
author: Link(2),
},
// `story.author`
2: Map {
name: 'Nikolas',
},
};
Query
Response
Caching layer
Although the response is hierarchical, we'll cache it by flattening all the records.
class Design extends Component {
render{
return(<div>
<div>{ this.props.furnitureType }</div>
<image url={ this.props.imageUrl}/>
</div>)
}
}
const DesignWithData = graphql(gql`{
design (uuid: 1234) {
furnitureType,
imageUrl
}
}`)(Design);