GraphQL Flashcards

(10 cards)

1
Q

What is GraphQL, and how does it differ from REST?

A

GraphQL is a query language for APIs, allowing clients to request specific data. Unlike REST’s fixed endpoints, GraphQL uses a single endpoint with flexible queries.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a GraphQL schema?

A

A schema defines the API’s data structure and operations. Example: type User { id: ID!, name: String } specifies a User type with required ID and optional name.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you write a GraphQL query?

A

”```graphql
query {
user(id: 1) {
id
name
}
}
//Explanation: Requests specific fields for a user with ID 1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you handle authentication in GraphQL?

A

Pass JWT in headers, verify in resolvers. Example: context: ({ req }) => { const user = verifyToken(req.headers.authorization); return { user }; } adds user to context.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are GraphQL subscriptions?

A

Subscriptions enable real-time updates via WebSockets. Example: subscription { messageAdded(channelId: "1") { content } } listens for new messages.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you optimize GraphQL queries to prevent over-fetching?

A

Use DataLoader to batch and cache requests. Example: new DataLoader(keys => db.batchUsers(keys)) reduces database calls for user queries.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the N+1 problem in GraphQL, and how do you solve it?

A

N+1 problem occurs when resolving N items triggers N additional queries. Solve with batching via DataLoader. Example: Batch user queries instead of fetching each individually.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a GraphQL resolver?

A

A resolver is a function that fetches data for a field. Example: user: (parent, args) => db.findUserById(args.id) retrieves user data from a database.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you set up a GraphQL server with Apollo?

A

``javascript const { ApolloServer, gql } = require(‘apollo-server’); const typeDefs = gqltype Query { hello: String }; const resolvers = { Query: { hello: () => ‘Hello’ } }; const server = new ApolloServer({ typeDefs, resolvers }); server.listen(); // Explanation: Defines schema and resolvers, starts server on localhost.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a GraphQL mutation, and how is it used?

A

A mutation modifies server-side data. Example: mutation { createUser(name: "Alice") { id name } } creates a user and returns their data.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly