
GraphQL: GraphQL is a Query Language for APIs. GraphQL gives power to clients to ask/query for exactly what data they need in the response.
- GraphQL was initially developed by Facebook
- GraphQL Initially publicly released in 2015
- GraphQL is written in - JavaScript, Ruby, Scala
- REST API is an architectural style whereas GraphQL is a query language.
- Data Fetching -
- GraphQL has only single endpoint with POST method.
- REST can have many endpoints with GET, POST, PUT, PATCH method.
- Network Requests -GraphQL reduces the network requests by allowing clients fetch or retrieve all the data from the server that we need in a single query.
- Error Handling -Error handling in REST is pretty straightforward. so in this case REST is winner.
- Caching -Since REST is implemented using HTTP, so the client can use HTTP caching to reduce load on the server. GraphQL has no caching mechanism in place, so the clients has to take care of caching on their end.
Note - However, there in one good package called DataLoader for GraphQL.
You may also like - RESTful Web Services Interview Questions
Scalar types are primitive data types that can store only a single value.
GraphQL is a strongly typed language.
GraphQL supports following types:
- Int
- Float
- String
- Boolean
- ID - A unique identifier
The syntax for defining a scalar type in GraphQL.
No, GraphQL is a just a query language for APIs.
No, GraphQL is an just an alternative to REST for developing APIs, it's not the replacement of REST.
You may also like - Node.js Interview Questions Answers
Yes
There are a lots of different programming languages that support GraphQL.
For Example:-
- Javascript(Node.js)
- Java
- Python
- PHP
- C#/.Net
- Go
- Perl
- Ruby
- Scala
- Swift
The official link to read in details - GraphQL
Not Exactly!, GraphQL is a query language for APIs, it's very similar to REST or you can say it's an alternative of REST.
GraphiQL is a browser IDE for developing, exploring and querying GraphQL.
In Node.js (using express.js) you can enable it by just simply passing - graphiql: true
GraphQL supports following operations:
- Query
- Mutation
- Subscription
In GraphQL, Query is used to fetch the data.
The Queries defines what queries you can run on your GraphQL API.
RootQuery - It contains all the existing queries.
In GraphQL, Mutation is used to change data. e.g - to create, update and delete data.
In GraphQL, Subscription is used for listening to any data changes.
e.g - server will notify to the client on any data change if the client has subscribed to that event.
- Query - it is used only to fetch data.
- Mutations - it is used to change data. e.g - to create, update and delete data.
In GraphQL, Resolver is used to handle graphql queries - "GraphQL Query Handler".
Resolver is a functions that is used to handle graph query and generate response for graphql query.
Overfetching means fetching extra data for an API request. it increases the payload size.
Underfetching means not fetching enough expected data. it requires multiple API calls to the complete data.
In short, a GraphQL schema is a textual representation of your application's data graph and the operations on it.
Data graph defines the entities and the relationships between them.
The GraphQL schema defines the types (query, mutation, subscription, etc).
Schemas enable automatic code generation, validation and parsing, introspection, and type safety for your APIs.
Example
There are mainly 3 ways to write/build/design graphql schema.
- buildSchema (graphql)
- GraphQLSchema (graphql)
- makeExecutableSchema (graphql-tools)
There are a lots of approches to achive this, let's understand.
- Set Query Timeouts
- Limit Query Depth
- Whitelist the Queries
- Throttling (Rate Limiting) Clients
- Protecting the GraphQL Endpoint
- Caching - GraphQL is not as good as REST especially HTTP caching.
- Query Complexity.