Essential GraphQL Interview Questions and Answers
by Sachin, on Jul 20, 2022 8:18:32 PM
Q1. 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.
Q2. What is Authentication and Authorization in GraphQL?
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 AuthorizationAuthorization in a simple and standard method from web, mobile, and desktop applications.
Authorization: On the other hand, AuthorizationAuthorization 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.
Q3. 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.
Q4. 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 fulfil your request or multiple API calls to fetch the complete data.
Q5. What are 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.
Q6. What are the main operations that GraphQL supports?
GraphQL supports three types of operations: query, mutation, and subscription. The query is used for the request, and it is a read operation, the mutation is used for write operations, and subscription is used for listening for any data changes. The server sends a notification message to the client after any data changes, if the client is subscribed to that event.
Q7. What is Apollo in GraphQL?
Apollo is a platform for the implementation of GraphQL. As we know that GraphQL is a query language, so in order to use this query language easily, we need a platform, Apollo provides that platform.
Apollo provides two open-sourced libraries to create client and server. Here, the client is used to fetch data from a GraphQL server, and the server is used to create an API for GraphQL client.
Q8. What is difference between Mutation and Query?
Technically any GraphQL query could be implemented to cause a data write. But there is a convention that any operations that cause writes should be sent explicitly via a mutation.
Besides the difference in the semantic, there is one important technical difference:
Query fields can be executed in parallel by the GraphQL engine while Mutation top-level fields MUST execute serially according to the spec.
Q9. What is GraphQL schema?
Every GraphQL server has two core parts that determine how it works: a schema and resolve functions.
The schema is a model of the data that can be fetched through the GraphQL server. It defines what queries clients are allowed to make, what types of data can be fetched from the server, and what the relationships between these types are.
Q10. What is subscription in GraphQL?
In GraphQL, the subscription is used for listening for any data changes. The server sends a notification message to the client after any data changes, if the client is subscribed to that event.
Q11. What do you understand by Fragment in GraphQL?
In GraphQL, the fragment is used when the query is very large, and your query contains the reusable units. You can create a fragment by taking the reusable part and use that fragment in the query. The fragment concept was designed to arrange the code in a simple way and avoid duplicate code.
Q12. Is GraphQL a server or database?
Q13. What are the goals for GraphQL?
The goal of GraphQL is to provide developers with a comprehensive view of data stored within an API, the ability to only receive relevant data and an architecture that makes APIs easier to scale and adapt over time.
Q14. 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.
Q15. 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.