GraphQL Beginner Quiz
30 questions on GraphQL Basics.
Question 1: What is GraphQL mainly used for?
- A. Database management
- B. API query language β (correct answer)
- C. Operating systems
- D. CSS styling
Explanation: GraphQL is a strongly-typed query language for APIs and a runtime for executing those queries, originally developed by Facebook.
Question 2: Which of the following is a major advantage of GraphQL over REST?
- A. It uses XML instead of JSON
- B. It prevents over-fetching and under-fetching of data by allowing the client to request exactly what it needs β (correct answer)
- C. It is a built-in browser language
- D. It automatically encrypts passwords
Explanation: In REST, an endpoint returns fixed data. In GraphQL, the client asks for specific fields, ensuring no unnecessary data is transferred (over-fetching) and no additional round trips are needed (under-fetching).
Question 3: What is the single endpoint commonly used in GraphQL architectures?
- A.
/api/v1/data
- B.
/graphql β (correct answer)
- C.
/rest
- D.
/query
Explanation: Unlike REST, which uses dozens of URLs for different resources, a GraphQL server typically exposes a single endpoint (usually /graphql) where all queries and mutations are sent via HTTP POST.
Question 4: What HTTP method is standardly used for sending GraphQL queries to the server?
- A. GET
- B. POST β (correct answer)
- C. PUT
- D. PATCH
Explanation: While some queries can run via GET, it is standard practice to send all GraphQL operations (queries and mutations) via POST requests so the query payload can be cleanly placed in the request body.
Question 5: In GraphQL, what keyword is used to request data without modifying it?
- A. fetch
- B. request
- C. query β (correct answer)
- D. get
Explanation: A query is the GraphQL equivalent of a REST GET request. It is used to fetch data and is strictly read-only.
Question 6: In GraphQL, what keyword is used to insert, update, or delete data?
- A. query
- B. mutation β (correct answer)
- C. modify
- D. post
Explanation: A mutation is used for operations that cause side effects on the server, mapping to REST's POST, PUT, PATCH, and DELETE operations.
Question 7: What does the exclamation mark ! signify in a GraphQL schema type definition (e.g., String!)?
- A. The field is deprecated
- B. The field is a primary key
- C. The field is non-nullable (required) β (correct answer)
- D. The field is encrypted
Explanation: The ! modifier indicates that the field must never be null. If a query requests a non-nullable field and the resolver returns null, a GraphQL execution error occurs.
Question 8: What is a GraphQL Resolver?
- A. A function that parses the JSON response
- B. A function responsible for fetching the data for a specific field in the schema β (correct answer)
- C. A database engine
- D. An authentication token
Explanation: Every field in a GraphQL schema has a corresponding resolver function on the server. When the field is queried, its resolver executes to connect to a database or API and return the value.
Question 9: What does SDL stand for in GraphQL?
- A. Secure Data Layer
- B. Standard Definition Language
- C. Schema Definition Language β (correct answer)
- D. Simple Data Link
Explanation: SDL is the syntax used to define the types, queries, and mutations in a GraphQL schema (e.g., type User { id: ID! name: String }).
Question 10: Which basic scalar types does GraphQL natively support?
- A. String, Int, Float, Boolean, ID β (correct answer)
- B. String, Number, Array, Object
- C. Text, Integer, Decimal, Bool
- D. Char, Double, Struct, UUID
Explanation: GraphQL has five built-in scalar (primitive) types. The ID type is serialized as a String but indicates a unique identifier.
Question 11: How do you pass a variable to a GraphQL query dynamically from the client?
- A. Append it to the URL query string
- B. Use the
$ prefix in the query definition and pass a separate JSON variables object β (correct answer)
- C. Hardcode it inside the schema
- D. Send it via HTTP Headers
Explanation: Clients send a JSON payload containing query and variables. Variables (like $id: ID!) prevent string interpolation bugs and improve query caching.
Question 12: What is GraphiQL (or Apollo Studio Explorer)?
- A. A database management tool
- B. An in-browser IDE for exploring GraphQL APIs, testing queries, and viewing auto-generated schema documentation β (correct answer)
- C. A React component library
- D. A CSS framework
Explanation: GraphiQL provides a powerful visual interface to write queries with auto-complete and read API documentation directly from the schema's introspection.
Question 13: What is introspection in GraphQL?
- A. Analyzing the performance of resolvers
- B. A feature that allows clients to query the GraphQL server for information about the schema itself (types, queries, mutations) β (correct answer)
- C. Encrypting the payload
- D. Validating database inputs
Explanation: Introspection is how tools like GraphiQL power their auto-complete. By querying __schema, the client discovers everything the API can do. (Often disabled in production for security).
Question 14: Which keyword in GraphQL is used for real-time data updates via WebSockets?
- A. query
- B. mutation
- C. subscription β (correct answer)
- D. watch
Explanation: A subscription maintains a persistent connection to the server. When a specific event occurs, the server pushes the updated data to subscribed clients.
Question 15: What is Apollo Client?
- A. A relational database
- B. A comprehensive state management library for JavaScript that enables you to manage both local and remote data with GraphQL β (correct answer)
- C. A Node.js web framework
- D. A GraphQL hosting provider
Explanation: Apollo Client is the industry-standard tool for integrating GraphQL into frontend frameworks like React, providing normalized caching, query states, and error handling.
Question 16: How do you query multiple resources in a single GraphQL request?
- A. Make multiple separate HTTP POST requests
- B. Use REST endpoints instead
- C. Simply add multiple query fields within the same
{ } block in a single request β (correct answer)
- D. Separate them by commas in the URL
Explanation: One of GraphQL's primary benefits is fetching multiple independent resources (like users and posts) in a single network round-trip by nesting them in the query body.
Question 17: What is a GraphQL Fragment?
- A. A broken query string
- B. A reusable unit of a query, allowing you to share logic between multiple queries and mutations β (correct answer)
- C. A chunk of a database table
- D. A server-side error
Explanation: Fragments (fragment UserParts on User { id name }) prevent repetition. You spread them into queries using ...UserParts.
Question 18: What happens if a specific field's resolver throws an error in GraphQL?
- A. The entire HTTP request returns a 500 error and no data is sent
- B. The query completes, the specific field returns
null (if nullable), and an errors array is appended to the response payload alongside successful data β (correct answer)
- C. The server crashes permanently
- D. The client receives a 404 error
Explanation: GraphQL typically returns a 200 OK status even if there are errors. Partial data is returned in the "data" object, while resolver failures populate the "errors" array.
Question 19: What is the N+1 problem in GraphQL?
- A. A mathematical query error
- B. A performance issue where resolving a list of items causes the server to execute an additional database query for every single item's nested fields β (correct answer)
- C. An issue with versioning schemas
- D. Adding too many mutations
Explanation: If you query 100 users and their profile pictures, a naive resolver might query the DB 1 time for users, and 100 times for pictures. This is solved using tools like DataLoader.
Question 20: How do you alias a field in a GraphQL query?
- A.
AS newName: fieldName
- B.
fieldName as newName
- C.
newName: fieldName β (correct answer)
- D.
alias(fieldName, "newName")
Explanation: Aliases let you rename the result of a field to avoid conflicts (e.g., querying the same field twice with different arguments: admin: user(id: 1), guest: user(id: 2)).
Question 21: In a resolver function (parent, args, context, info), what does the args parameter hold?
- A. The result of the previous resolver
- B. The arguments provided to the field in the GraphQL query (e.g., id, limit) β (correct answer)
- C. The database connection object
- D. The HTTP headers
Explanation: If a client queries user(id: "123"), the args object in the resolver will be { id: "123" }.
Question 22: What is the purpose of the context parameter in a resolver?
- A. To hold the query string
- B. To provide a shared object across all resolvers, typically used for authentication tokens, database connections, and user sessions β (correct answer)
- C. To handle routing
- D. To define the schema
Explanation: Context is initialized once per request and passed to every resolver, making it the perfect place to inject the currently logged-in user or database instances.
Question 23: Which tool is commonly used to solve the N+1 problem in GraphQL servers by batching and caching database requests?
- A. Apollo Client
- B. GraphiQL
- C. DataLoader β (correct answer)
- D. Redux
Explanation: DataLoader is a utility that collects all individual queries made during a tick of the event loop and batches them into a single request (like a SELECT ... WHERE IN SQL query).
Question 24: In a GraphQL schema, what does [String]! mean?
- A. The array can be null, but the strings cannot be
- B. The array cannot be null, but it can contain null values β (correct answer)
- C. The array cannot be null, and the strings inside it cannot be null
- D. An optional array of optional strings
Explanation: The outer ! applies to the array structure itself. So it must return a list (even an empty one []), but items inside it could theoretically be null. [String!]! would make both strict.
Question 25: Can GraphQL mutations return data?
- A. No, they only return a success boolean
- B. Yes, mutations allow you to specify a selection set to return the updated object's fields immediately after the operation β (correct answer)
- C. Yes, but only the object's ID
- D. No, you must run a query afterward
Explanation: Mutations work like queries; after the write operation succeeds, the resolver fetches and returns the requested fields of the newly modified data, keeping UI caches updated seamlessly.
Question 26: What are GraphQL Directives?
- A. Server routing paths
- B. Special annotations like
@include(if: Boolean) or @skip(if: Boolean) attached to fields to alter execution behavior dynamically β (correct answer)
- C. Database migrations
- D. Authentication protocols
Explanation: Directives provide a way to describe alternate runtime execution. For instance, @skip(if: $isMobile) tells the server not to fetch or return a field if the variable is true.
Question 27: Why does GraphQL rarely use traditional HTTP status codes for errors?
- A. HTTP is incompatible with GraphQL
- B. Because requests are typically successful at the HTTP transport layer (200 OK), with execution errors packaged inside the JSON
"errors" array payload β (correct answer)
- C. Because status codes slow down the network
- D. It relies exclusively on 404 codes
Explanation: A GraphQL query might successfully fetch 9 out of 10 fields. It returns 200 OK with the 9 fields in "data" and the 1 failed field detailed in "errors".
Question 28: What is Schema Stitching or Apollo Federation?
- A. Minifying JSON payloads
- B. Combining multiple independent GraphQL schemas (microservices) into a single unified graphical API gateway β (correct answer)
- C. Adding CSS styles to GraphiQL
- D. Compiling resolvers into WebAssembly
Explanation: Federation allows large organizations to split their monolithic API into microservices (Users API, Products API) while presenting one unified graph endpoint to the client frontend.
Question 29: How is API versioning typically handled in GraphQL compared to REST?
- A. By using URLs like
/graphql/v2
- B. GraphQL relies on schema evolution; you deprecate old fields using
@deprecated and add new fields, avoiding strict versions entirely β (correct answer)
- C. By sending version headers
- D. GraphQL does not allow schema changes
Explanation: Because clients request exactly what they need, adding new fields doesn't affect existing clients. Old fields are marked @deprecated but kept active for backwards compatibility.
Question 30: What defines the root entry points for a GraphQL API?
- A. The Root, Request, and Response objects
- B. The Query, Mutation, and Subscription root types defined in the schema β (correct answer)
- C. The HTTP endpoints defined in Express
- D. The SQL table names
Explanation: The schema defines special entry points. Every read operation must start from fields defined on the root Query type. Every write begins at the root Mutation type.