GraphQL API in a nutshell.
- What is GraphQL API.
GraphQL was initially developed by Facebook in 2012 and later on Open sourced in 2015. It is a query language for API and a runtime for executing the Queries by using a type system defined for the data.
Key aspects of the GraphQL include.
- Declarative Data fetching.
This means that clients can request the exact data that they need and nothing more. This is among GraphQL's pillar functionalities. It prevents over-fetching or under-fetching of data, improving efficiency.
- Single EndPoint.
Unlike REST, which may require multiple endpoints for different resources, GraphQL uses a single endpoint for all queries and mutations.
- Strongly Typed.
GraphQL API's have a strong type system defined using a schema. The schema serves as a contract between the client and the server, ensuring consistency.
- Real-time data.
Graphql can be used for real-time applications by utilizing subscriptions. Subscriptions allow clients to receive updates when the underlying data changes.
- GraphQL Schema.
At it's core, GraphQL has a schema. This defines the types of data that can be queried and the operations that can be performed.
A GraphQL Schema consists of:
- Types.
These represent the data structures in the API, like User, Product, or Post.
- Queries.
These are entry points for fetching data.Clients use queries to request specific data from the server.
- Mutations.
Entry points for modifying data. Clients use mutations to create, update, delete data.
- Subscriptions.
These are used for real-time updates. Clients can subscribe to changes in the data.
- GraphQL servers.
To implement GraphQL API, one needs a server that understands GraphQL queries, resolves them, and returns the requested data.
Popular options for building GraphQL servers include:
a. Apollo Servers.
A widely used GraphQL server library for Node.Js.
b. Express-GraphQL.
This is the middleware for adding GraphQL to Express.js applications.
c. Graphene-Django.
This is the Python Library for building GraphQL APIs with Django.
d. GraphQL clients.
Clients use GraphQL to request data from the Server.
- Popular GraphQL libraries include:
- Apollo client.
This is a JavaScript Library that is used for making GraphQL queries and mutations in Web and Mobile applications.
- Relay.
This is a JavaScript framework for building efficient and scalable GraphQL powered applications.
- Use cases for GraphQL.
GraphQL is well suited for a variety of use cases:
a. Mobile applications.
GraphQL's flexibility allows mobile apps to request only the data they need, saving bandwidth and improving performance.
b. Real-time applications.
With subscriptions, GraphQL is suitable for Chat apps, live feeds, and other real-time systems.
c. Aggregating data.
GraphQL can serve as a single point of access to aggregate data from multiple sources.
d. Complex Queries.
For applications requiring complex queries with various filters, GraphQL simplifies the data-fetching process.
e. Microservices.
GraphQL can act as a facade for multiple MicroServices, providing a unified API to Clients.
f. Prototyping.
It is a great choice for rapid prototyping since you can quickly adapt the API to changing requirements.
- Basic GraphQL query.
query {
user(id:1){
name
}
}
- Basic GraphQL Mutation.
mutation{
createUser(input:{name:"John Doe", email:"john@example.com"}){
id
name
}
}
Thank you and see you in the next.
Special credits to @ainasanghi on X(Formerly Twitter).