GraphQL
What is GraphQL?
- A specification, query Language for your API. Find the GraphQL Spec here.
How is GraphQL better than REST (for some specific scenarios)?
- REST may require the web client to call multiple REST endpoints (APIs) to retrieve the full response you need and then do heavy operations on the client side such as sorting, parsing, filtering to stitch the data together.
- With GraphQL, a web client calls a single GraphQL endpoint and GraphQL moves a lot of the sorting, parsing, filtering, transformation logic to the web server leaving the client to simply rendering the response… making it extremely fast, low latency for applications such as mobile, VR etc. Ex — Facebook’s live likes / emojis on live videos are powered by GraphQL subscriptions.
(fn → GraphQL resolver functions)
How can I learn and play with GraphQL?
- Start with a GraphQL playground ex-https://snowtooth.moonhighway.com/ or https://pet-library.moonhighway.com/
How does REST operations map to GraphQL?
- GET → Query
- POST / PUT / DELETE → Mutation
- Webhooks → Subscriptions (listens for real time changes over web sockets)
**What are some quick examples? ** Copy paste below examples in the Playground: https://pet-library.moonhighway.com/
query {
totalPets(status: CHECKEDOUT)
allPets {
name
weight
category
}
}mutation{createAccount(
input:{
name: "Demo"
username: "Demo"
password: "Demo"
}
), {
name
username
}}