GraphQL API Design Fundamentals: Schema Changes, Operations, and Patterns Quiz

Test your knowledge of GraphQL API design essentials, including schema evolution via deprecation, the differences between queries and mutations, the use of variables, cursor-based pagination techniques, and best practices in error handling. This quiz is perfect for developers looking to strengthen their understanding of reliable and scalable GraphQL APIs.

  1. Deprecation Usage in Schema Evolution

    Which directive is typically used in GraphQL to mark a field as deprecated when evolving a schema but still needs to be accessible to existing clients?

    1. @deprecated
    2. @disable
    3. @remove
    4. @obsolete

    Explanation: The @deprecated directive is the standard way to indicate that a field or type is no longer recommended for use but is still available. @disable, @obsolete, and @remove are not recognized directives in GraphQL for this purpose. Using the proper directive helps communicate upcoming changes to clients and allows for smoother transitions.

  2. Queries vs Mutations

    In GraphQL, when is it appropriate to use a mutation instead of a query, for example, when creating a new user account?

    1. When retrieving information only
    2. When making changes to the server's state
    3. When validating query fields
    4. When requesting read-only public data

    Explanation: Mutations in GraphQL are used for operations that modify data or change the server's state, such as creating, updating, or deleting resources. Queries are strictly for retrieving or reading data. Validating query fields and accessing read-only data do not require mutations and are suited for queries instead.

  3. Using Variables in GraphQL

    Why are variables recommended over hardcoded values in GraphQL operations, such as filtering users by status?

    1. Variables promote code reusability and protect against injection risks.
    2. Variables speed up deprecated fields.
    3. Variables disable error messages.
    4. Variables replace all query names automatically.

    Explanation: Variables in GraphQL make operations more flexible and reusable, allowing different values to be passed each time without modifying the query itself. Using variables also helps prevent common security risks like injection. The other options are incorrect because variables do not interact with error messages, do not affect deprecated fields' speed, and do not automatically change query names.

  4. Cursor-Based Pagination Concept

    What is the main advantage of using cursor-based pagination over offset-based pagination in a GraphQL API listing ordered items?

    1. It always returns the first page only.
    2. It disables error handling for paginated data.
    3. It requires less code to implement.
    4. It avoids issues with shifting data and improves consistency during navigation.

    Explanation: Cursor-based pagination is effective at preventing issues from data changing between requests, ensuring users experience consistent results. It does not return just the first page, nor is it always less code to implement compared to offset-based approaches. Pagination has no effect on disabling error handling.

  5. Returning Errors in GraphQL Responses

    Where does GraphQL specify errors when a query fails partially due to an invalid field?

    1. Only in the HTTP status code
    2. In a hidden header
    3. In the 'errors' top-level field of the response
    4. Attached to the query string

    Explanation: When a GraphQL query has problems, such as querying a non-existent field, errors are reported in a designated 'errors' array at the top level of the JSON response. Headers and HTTP status codes are not the primary mechanisms for conveying these errors, and nothing is attached to the query string.

  6. Safe Schema Evolution

    What is a safe and recommended approach to removing a field from a GraphQL schema when updating an API?

    1. Delete the field immediately without notice.
    2. Rename the field and keep it active.
    3. Make the field return 'null' for all requests.
    4. Deprecate the field first, then remove it after communicating with clients.

    Explanation: Deprecating a field allows clients time to adjust before the field is removed, helping ensure smoother transitions and reducing unexpected breakage. Instantly deleting the field or returning null without warning may disrupt consumers. Simply renaming keeps the field but does not address deprecation or removal.

  7. Best Practice for Mutations

    When designing a mutation for updating a user's email, which practice ensures clear API design and ease of use?

    1. Allow any string to be used as input without validation
    2. Send an array of unrelated objects
    3. Specify exact input fields and return the updated user object
    4. Return 'true' only if the email updates

    Explanation: Clearly defined input fields and providing the updated object in the response help clients understand what changed and make integration easier. Accepting any string or returning just 'true' lacks clarity. An array of unrelated objects is confusing and unnecessary.

  8. GraphQL Variable Syntax

    Which of the following correctly illustrates using a GraphQL variable for an integer 'userId' in a query?

    1. query getUser($userId Int!) { user(id = $userId) { name } }
    2. query getUser($user: ID!) { user(id: userId) { name } }
    3. query getUser { user(id: userId) { name } }
    4. query getUser($userId: Int!) { user(id: $userId) { name } }

    Explanation: The correct syntax includes the variable with a colon and exclamation mark indicating required type, and uses the variable with a dollar sign in the body. The other options either miss necessary punctuation, use wrong types, or lack the dollar sign, making them invalid.

  9. Applying Pagination Arguments

    If a GraphQL API supports cursor-based pagination, which arguments are commonly used to request the next set of results after a given edge?

    1. offset and limit
    2. page and size
    3. skip and take
    4. first and after

    Explanation: In cursor-based pagination, 'first' specifies how many results to return and 'after' tells the server where to start. Offset-limit and skip-take are approaches used in other pagination types, and page-size is generally used for page-based pagination, not cursor-based systems.

  10. Handling Invalid Mutations

    When a mutation input fails validation, such as providing an invalid email format, how should a GraphQL API respond?

    1. Return a 200 OK with no body at all
    2. Silently ignore the mutation and return success
    3. Send only partial fragments of the data
    4. Include a descriptive error in the 'errors' field of the response

    Explanation: A GraphQL API should provide useful feedback to clients about validation failures by including an error message in the 'errors' response field. Ignoring the error or returning incomplete fragments confuses the client, while an empty response without explanation is unhelpful.