A complete and simple GraphQL api implementation
Alvaro Pa?ó
Alvaro Pa?ó
Software Architect & Tech Leader | Crafting High-Performance, Cloud-Native Systems | 10+ Years in Full-Stack Development | IDCiber Consul Member
Hi
In the way of the moderns web apps, one of the most awesome new technologies is QraphQL. GraphQL is awesome because resolve the problem of the RESTFul APIs, the endpoints, that is staying in the past.
The new way to do right things in cloud services follow the QL trends. But what's the great points about QL?
- Only the content that is needed by the website is returned
- This reduces network traffic compared to a REST approach where mostly too much content is returned (overfetching).
- All content for a single webpage can be fetched in one request
- Making many API requests to fetch all content for a webpage (underfetching) can lead to performance issues. With GraphQL there is only one request.
- Less coordination needed between API and website parties
- Exposing more content is a few line of configuration code. The new content is immediately accessible by the website. There is no need for communication on which endpoints data needs to be made available.
- Total flexibility for API consumers
- Different API consumers have different needs. To make content fetching performant, one might build specialised REST endpoints that return exactly the information a API consumer needs which is costly and brittle.
- GraphQL Schema
- The schema enables tools to do auto-complete and detect on compile time that a request will work (or not). This reduced development time.
- Documentation is embedded in the schema
- This is nice because the documentation is right in the editor instead of on a separate documentation website like with REST.
- Versioning is not an issue
- Deprecating fields and providing an update path is easy. So much easier than providing proper versioning in REST.
- Smart client-side caching
- User-specific content makes it hard to achieve a good cache-hit rate using HTTP cache methods. Based on the GraphQL schema there can be intelligent client side caching which knows what data is available and what needs to be fetched.
- Efficient changes
- GraphQL also supports changes, they are called mutations. Mutations can be batched (multiple mutations) and even fetch content in the same request.
So, I've implemented a little and complete QL example and you can try it on: https://github.com/alvaro-paco/GraphQl-Pdv-Example