GraphQL Flashcards
What is GraphQL?
GraphQL is a new API standard designed and developed by Facebook. It is an open-source server-side technology that is now maintained by a large community of companies and individuals worldwide. It is also an execution engine that works as a data query language and used to fetch declarative data.
What was the reason behind the development of GraphQL?
GraphQL was initially developed by Facebook as an internal solution for their mobile apps. It was designed to optimize RESTful API calls and provide a flexible, robust, and efficient alternative to REST. It is not a replacement for REST. It is an alternative to writing APIs using REST.
After its release, it has become prevalent among developers and is also a popular solution for building web services along with REST.
What are the top companies that use GraphQL?
There are many big organizations such as Facebook, Github, Pinterest, Intuit, coursera, shopify, dailymotion, yelp etc. that uses GraphQL. Actually GraphQL was designed and developed by Facebook itself.
How GraphQL utilizes the data loading process?
When the users fetch the data in GraphQL, it retrieves only the minimum amount of data required by the client.
Even if the object model contains a lot of fields, the client can request only the required fields.
What is the similarity of Graph database and GraphQL
Fundamentally, there is no relationship. Besides the fact that GraphQL and graph databases have the word “graph” in their name, they were designed for two entirely different purposes.
GraphQL, the query language, was created to make it easier for app developers to fetch data by asking specifically for what they want, while graph databases were designed to allow for performant analysis of relationships between entities.
The main advantage I can think of for using a graph database as a data source is a shared mental model between edges, nodes, and properties between GraphQL and graph databases. You can model your GraphQL schema similarly to how we think about graph databases’ main abstractions.
Is GraphQL only suitable for React / JavaScript Developers?
No. It is not correct to say that GraphQL is only for React or JavaScript Developers. GraphQL is a cross-platform, open-source data query, and manipulation language for API technology, so it can be used in any scenario where API technology is required.
On the backend, the GraphQL servers are available for multiple languages such as Java, Python, .NET, C#, PHP, R, Haskell, JavaScript, Perl, Ruby, Scala, Go, Elixir, Erlang, and Clojure, etc. So, it can be implemented with any programming language and framework to build a web server.
What are the reasons behind using GraphQL while we already had an API named REST?
Yes, we already had an API called REST for data communication, which follows a clear and well-structured resource-oriented approach. It also provides some great ideas such as stateless servers, structured access to resources, etc. But, REST is not so flexible to cope up with the rapidly changing requirements of the clients. In that case, when the data gets more complex, the routes get longer. Sometimes, it is challenging to fetch the data with a single request. That’s why Facebook takes a step to develop a new API technology named GraphQL to cope up with the REST’s limitations.
How GraphQL facilitate developers to choose protocols other than HTTP to implement your server?
The GraphQL API is usually operated over HTTP, and it is a transport layer agnostic technology, so you can choose protocols other than HTTP to implement your server.
What is Authentication and Authorization in GraphQL?
Authentication and Authorization are the processes used in services.
Sometimes people get confused in these two terms and exchange their definitions for each other.
Authentication:
Authentication is a process that is used to claim an identity. Authentication is done when you want to log in to a service with a username and password. Here, you have to authenticate yourself. In GraphQL, Authentication can be implemented with common patterns such as OAuth.
OAuth is an open protocol that is used to allow secure Authorization in a simple and standard method from web, mobile, and desktop applications.
Authorization:
On the other hand, Authorization is a process that is used to give permission rules that specify the access rights of individual users and user groups to certain parts of the system. For authorization implementation in GraphQL, it is recommended to delegate any data access logic to the business logic layer and not handle it directly.
How to do Error Handling in GraphQL?
A successful GraphQL query is supposed to return a JSON object with a root field called “data.” If your request query fails or partially fails, you will see a second root field called “errors” in the response.
{
“data”: { … },
“errors”: [ … ]
}
How to do Server-side Caching in GraphQL? How is it different from REST?
The biggest concern with GraphQL technology is that it is difficult to maintain the server-side cache compared to the REST. In REST API, we can easily cache data for each endpoint. This is because the structure of the data does not change. On the other hand, in GraphQL API, it’s not clear what the client will request next, so it does not matter to put a caching layer right behind the API. That’s why doing Server-side Caching difficult in GraphQL.
How to be cache in server side of Apollo?
Using cacheControl
votes: Int @cacheControl(maxAge: 30)
What type of response you get after a GraphQL query?
In GraphQL, when a client requests a query, the server returns the JSON format response. The response returned by the server is based on the query the client uses for the request.
What is Over-fetching in GraphQL?
Over-fetching is a response where the client gets too much data or extra data for an API request. In over-fetching, you have a lot of additional data in the response you don’t use. Over-fetching unnecessarily increases the payload size.
What is Under-fetching?
Under-fetching is a response where the client doesn’t get enough data. The under-fetching response doesn’t have enough data with a call to an endpoint, so you have to call a second endpoint to fulfill your request or multiple API calls to fetch the complete data.
How GraphQL fixes the issues of Over-Fetching or Under-fetching?
Over-Fetching and Under-fetching both are the performance issues that can be solved by using GraphQL. You won’t see these problems if you have precisely the right endpoints to give your products exactly the correct data. These problems occur when you have to maintain multiple endpoints to get the exact right data. It has increased the data load, which finally results in a performance issue.
GraphQL resolves this issue because it facilitates you to request the specific data which you want from the server. Here, you can specify what you need in a single request and get the exact result that you need in just one response from the server.
What are the most significant advantages of using GraphQL over REST?
A list of the most significant advantages of using GraphQL over REST:
There is only one endpoint in GraphQL, but REST has multiple endpoints. That’s why GraphQL is more cost-effective than REST. You don’t have to use your resources for various endpoints.
In REST API, you have to use multiple requests to retrieve a complex data-set, but in GraphQL, you can execute a complex query easily in just a single request.
You can change the request data format in GraphQL, but it is not possible to do the same in REST.
The development speed in GraphQL is faster than REST.
GraphQL provides high consistency across all platforms, while In REST, it is hard to get consistency across all platforms.
GraphQL doesn’t support an automatic caching system, while REST uses caching automatically.
Is REST also a query language like GraphQL?
No. REST is not a query language like GraphQL. It is a web service API.