2. Agenda [ What ]
1. REST Refresher
2. Introduction of GraphQL
3. Queries
4. SDL, Schema Design (Graph Thinking)
5. Query Resolvers
6. In Action with Spring Boot
7. Performance
8. Resources
3. REST Refresher [ API First ]
1. http://<host>:<port>/<context>/blogs GET
2. http://<host>:<port>/<context>/blogs POST
3. http://<host>:<port>/<context>/blogs /{id} DELETE
4. http://<host>:<port>/<context>/blogs PUT
5. http://<host>:<port>/<context>/blogs/{id} GET
We generally design rest endpoints be letting the action defined by the
http methods. These are very simple use cases.
4. Introduction of GraphQL [ But Why ? ]
We have seen a very simple use case of blog service REST APIs in the previous slide. Which
does not require very complex API call sequencing, but REST style suffers majorly from the
issue like
1. Overfetching and Underfetching
2. Network Requests
3. Caching
4. Error Handling
5. Versioning
6. Require discipline on both sides
5. Again Introduction of GraphQL [ Client First ]
GraphQL is the specifications developed by Facebook and later open sourced. Now maintained by the
community. It’s again a change in mindset from REST in a similar way the shift happen from SOAP to
REST
From Official Site
“GraphQL is a query language for your API, and a server-side runtime for executing queries by using
a type system you define for your data. GraphQL isn't tied to any specific database or storage
engine and is instead backed by your existing code and data”
7. Queries [ How ]
GraphQL queries are divided in three types
1. Query : For querying the data from APIs
2. Mutation : To update or make any changes in stored data
3. Subscription : To get notifications if any state changes in data
10. SDL continues ...
1. Objects as type
2. Enum
3. Interface
So whatever can be thought as an object can be
represented in GraphQL schema.
GraphQL allows you to think in terms of a graph
and its node as entities involved and then design
the communication. But the result of GraphQL
queries is always a tree.
11. Query Resolvers
These actually map the schema to the actual
execution points. GraphQl provides query
resolvers based on the type of queries mentioned
in schemas. But it has three main resolvers
1. Query Resolver
2. Mutation Query Resolver
3. Subscription Query Resolver
12. Enough With The Talk, Show me the example !
[ In Action with Spring Boot ]
Covering this GraphQL in action example using spring boot
and libraries of GraphQL in java. A blog service is created
with architecture mentioned in the diagram
https://github.com/vipin-pratap18/java-graphql-springboot
13. Performance and other issues
GraphQL solves RESTs problem but introduces its own like a contract between a client and server has to be
established beforehand. This is GraphQLs biggest feature and challenge sometimes as developer and designers
have to think and document all the possible use cases required to cover in output.
Performance is slow than REST as GraphQL has to identify the relationship and fetch all the dependent objects
separately if required in response.
It does not have any good documentation and monitoring tools available as of now.
Queries will be very rigid, though it is called query language query structure and parameters are fixed like REST.
GraphQL exposes the risk of the arbitrary request as in REST endpoint structure and parameters are fixed so without
match it will not proceed. This might not be the case with GraphQL sometimes. Which might leads to DDoS.
No proper response codes against any request, server, validation failure at server end.
14. So where it is useful !
GraphQL is proving its worth on both Web and
Mobile apps. But real difference can be seen on
the mobile app, as these devices are low on
memory and cannot load a large amount of data.
Which can be controlled by the client itself in
GraphQL.
In a web application it has the capability to
segregate the multiple API calls in one and also
can control the data going out of the service.