2. Topics
- What is GraphQL
- Why GraphQL
- What is Apollo
- SDL (Schema Definition Language)
- Scalar Types
- Resolvers
- Operations in GraphQL
3. What is GraphQL?
- GraphQL is a query language for API which queries on
the data provided to it.
- It is not to be confused with Database query language
as it is not use to query on database.
- The data source for graphQL can be like API,
microservices, database.
4. Why GraphQL?
● Ask for what you need and get that data only. Same can be achieved by rest api by it is the core feature
of graphQL.
● Returns the data in the same format in which format we request the data.
● Comparatively less request - response cycle compared to rest api, it means you can fetch many
resources in single request.
● As GraphQL is strictly typed so, we can catch error during development time and apps is clear about the
request and response data.
● GraphQL has a powerful developer tools to test api and documentation.
5. Why GraphQL?
Typedef in data
type User {
name: String!
id: Int!
friends: [User!]!
}
Ask what you want
{
getUser{
name
friends {
name
}
}
}
Format of Response
{
“getUser”: {
name: “John”
friends: [{
name: “Alex”
},{
name: “Bob”
}]
}
}
6. The team structure of
each DAN POD is
optimised per set
hierarchy
What is Apollo?
● Apollo Server is a library use to create the graphQL server in node.js with an ease.
● Apollo Server is a wrapper over the graphQL and provide many inbuilt feature which we have to implement
ourselves if we use graphQL with node.js.
● It gives us a feature to combine multiple graphQL server into one by just combining the schema of different
server which hides all the complexity in itself and gives us a combined schema. It means that we can easily
combine different server into a one and provide a single interaction point for the client.
● It provides us multiple powerful libraries for different use cases like for batch processing of multiple graphql
queries it provides libraries apollo-link-batchapollo-link-batch-http, for attempting multiple times in case of
server and network error it provides library apollo-link-retry and for more you can refer here.
7. ● The schema syntax we use in graphQL is called
SDL.
● SDL represents the relationship between the data
returned.
● The schema in the graphQL is stored as a string.
type Book {
title: String
author: Author
}
type Author {
name: String
books: [Book]
}
SDL (Schema Definition Language)
8. Scalar Types in GraphQL
Scalar types denotes the leaves of the data. GraphQL has five scalar types mentioned below.
● Int - Signed 32-bit integer
● Float - Signed double-precision floating-point value
● String - UTF-8 character sequence
● Boolean - true/false
● ID - A Unique Identifier
9. Resolvers
Resolvers is a method which is responsible for
generating the response against the graphql
operations.
Resovers can return the data or the promise
against any operations.
Signature of Resolvers
● parent: An object contains the result from the resolvers on the parent
type.
● args: An object contains the argument passed during the operations.
● context: An object available to all resolvers and share the state
across resolvers.
● info: Information about the execution state of the operations.
User: (parent, args, context, info) => response
10. Operations in GraphQL
There are 3 types of operations in GraphQL.
● Query: Query operation is just like we use get in http method, it is use to read data from the data provided to graphQL.
● Mutation: Mutation is use if we want to create, delete or update the data.
● Subscription: Subscription is the operation responsible for providing the updated data on each mutation. It means it helps
us in achieving reactivity on frontend (Client Side).
11. Important Links:
- GraphQL Resource: https://graphql.org/
- Apollo Resource: https://www.apollographql.com/docs/apollo-server/
- GraphQL Schema: https://graphql.org/learn/schema/