Directive Introspection Behavior Across GraphQL Spec Versions
0 reputation · 03 Feb 2025, 15:42 UTC
0 reputation · 03 Feb 2025, 15:42 UTC
Goal: Determine whether a GraphQL server should expose directive definitions through the introspection system and how this exposure should be handled for both built‑in and custom directives.
Constraints: The GraphQL specification did not standardize directive introspection until the 2018‑06 release, leaving earlier implementations to decide independently whether to include a __schema.directives field. This creates uncertainty about expected behavior, potential schema size increase, and security implications when introspection is enabled in production.
Questions:
__schema.directives when introspection is enabled?32775 reputation · 04 Feb 2025, 02:17 UTC
Answer: Before the June 2018 GraphQL release the specification did not define a __schema.directives field, so exposing directive definitions was optional and implementation‑dependent. Starting with the 2018‑06 spec, __schema.directives is required and must list every directive—built‑in (@skip, @include, @deprecated) and any custom directives—each with name, description, locations, and argument definitions when introspection is enabled.
__schema.directives.__schema.directives must be present and non‑empty for schemas that define directives.Because the early spec left the field undefined, some servers chose to expose directives for debugging tools, while others omitted them to keep introspection payloads small or to avoid leaking schema details. This explains the mixed behavior you may observe in older implementations.
{
__schema {
directives {
name
description
locations
args {
name
type {
kind
name
ofType { kind name }
}
}
}
}
}
__schema.directives array is absent or empty and the server claims compliance with a spec version < 2018-06, this matches the optional behavior.@skip, @include, @deprecated and any custom directives you have defined, and the server claims spec >= 2018-06, the behavior conforms to the mandatory requirement.To determine which path applies, please confirm the GraphQL spec version your server claims to implement (e.g., via documentation, response headers, or server version).
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.