GraphQL Interview Questions. GraphQL is a Query Language for APIs | GraphQL vs REST | Apollo GraphQL, etc
GraphQL Interview Questions
Q:- What is GraphQL?

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
Q:- What are the difference between REST and GraphQL?
  1. REST API is an architectural style whereas GraphQL is a query language.
  2. Data Fetching -
    • GraphQL has only single endpoint with POST method.
    • REST can have many endpoints with GET, POST, PUT, PATCH method.
  3. 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.
  4. Error Handling -
    Error handling in REST is pretty straightforward. so in this case REST is winner.
  5. 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
Q:- What is scalar type in GraphQL?

Scalar types are primitive data types that can store only a single value.

GraphQL is a strongly typed language.

GraphQL supports following types:

  1. Int
  2. Float
  3. String
  4. Boolean
  5. ID - A unique identifier

The syntax for defining a scalar type in GraphQL.

field_name: data_type
Q:- Is GraphQL a Database?

No, GraphQL is a just a query language for APIs.

Q:- Can GraphQL replace REST?

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
Q:- Can I use GraphQL with MongoDB?

Yes

Q:- Which programming languages supports GraphQL?

There are a lots of different programming languages that support GraphQL.

For Example:-

  1. Javascript(Node.js)
  2. Java
  3. Python
  4. PHP
  5. C#/.Net
  6. Go
  7. Perl
  8. Ruby
  9. Scala
  10. Swift

The official link to read in details - GraphQL

Q:- Is GraphQL a REST API?

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.

Q:- What is GraphiQL?

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

app.use('/graphql', graphqlHTTP({ schema: schema, rootValue: root, graphiql: true, }));
Q:- What are the operations GraphQL supports?

GraphQL supports following operations:

  1. Query
  2. Mutation
  3. Subscription
Q:- What is Query?

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.

type RootQuery { user(id: ID): User # Equivalent to GET /api/users/:id users: [User] # Equivalent to GET /api/users posts(id: ID!): Post # Equivalent to GET /api/posts/:id posts: [Post] # Equivalent to GET /api/posts }
Q:- What is Mutation?

In GraphQL, Mutation is used to change data. e.g - to create, update and delete data.

Q:- What is Subscription?

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.

Q:- What is diff between Query and Mutations in GraphQL?
  • Query - it is used only to fetch data.
  • Mutations - it is used to change data. e.g - to create, update and delete data.
Q:- What is Resolver in GraphQL?
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.

Q:- What is Overfetching and Underfetching in GraphQL?

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.

Q:- What is schema in GraphQL?
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

type User { id: ID! name: String! email: String! status: Boolean } type Post { id: ID! title: String! author: User! date: String! }
Q:- How to design/build schema in GraphQL?

There are mainly 3 ways to write/build/design graphql schema.

  1. buildSchema (graphql)
  2. GraphQLSchema (graphql)
  3. makeExecutableSchema (graphql-tools)
Q:- How can you secure GraphQL Query?

There are a lots of approches to achive this, let's understand.

  1. Set Query Timeouts
  2. Limit Query Depth
  3. Whitelist the Queries
  4. Throttling (Rate Limiting) Clients
  5. Protecting the GraphQL Endpoint
Q:- What are the drawbacks of GraphQL?
  1. Caching - GraphQL is not as good as REST especially HTTP caching.
  2. Query Complexity.