01 / 02

What are directives?

Difficulty: 5/10
built-in directives, custom directives, schema evolution

In GraphQL, directives are special annotations used to modify the behavior of queries, mutations, or the schema itself. They allow developers to conditionally include or exclude fields, modify query execution, or customize schema behavior. Directives increase flexibility and make GraphQL more dynamic by enabling control over how data is fetched, returned, or validated.

Built-in Directives:
  1. 1

    @include(if: Boolean) : Used to conditionally include a field or fragment based on a boolean value.

  2. 2

    @skip(if: Boolean): Used to conditionally exclude a field or fragment based on a boolean value.

@include(if: Boolean)
@skip(if: Boolean)

Scenario Questions

0-2 years experience

  1. 1How would you add a @deprecated directive to a field in your schema, and what does a client see when they query that field?
  2. 2If you need to conditionally include a field based on a variable, which built‑in directive do you use and how do you write the query?
  3. 3What happens if you call @skip without providing the required Boolean argument in a query?

2-5 years experience

  1. 1You introduced a custom @auth directive on several fields, but some unauthorized users still receive data. Walk me through how you’d debug the problem.
  2. 2Explain the trade‑offs between using @include/@skip versus handling the same conditional logic inside resolvers.
  3. 3Why might marking many fields with @deprecated cause issues for client code‑generation tools?

5-8 years experience

  1. 1Design a role‑based access control system using directives that must work across multiple microservices each exposing its own GraphQL API. What performance and security concerns do you need to address?
  2. 2How would you implement a logging directive that adds minimal overhead and doesn’t break query caching?
  3. 3In a federated GraphQL gateway, how do directives impact schema stitching and introspection performance at scale?

8+ years experience

  1. 1Your team is migrating a legacy REST service to GraphQL but wants to keep existing client behavior that relies on custom pagination and caching directives. How would you plan the migration to preserve directive semantics across services?
  2. 2Discuss the long‑term maintenance implications of proliferating custom directives in a large codebase. What governance model would you put in place to avoid versioning chaos?
  3. 3If multiple teams define overlapping directives with similar names, what architectural guidelines would you establish to prevent conflicts and ensure discoverability?

Follow-up Questions

  • Can you show the exact SDL syntax for adding that directive?
  • What would you monitor in production to ensure the directive isn’t causing latency?
  • How does the presence of a directive affect GraphQL introspection?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.