What is introspection in GraphQL and what are its security implications?
Learn what GraphQL introspection is, how tools use __schema, and why exposing it in production is a security risk you should disable or gate.
Expected Interview Answer
Introspection is GraphQL's built-in ability to query the schema itself — clients can ask a server which types, fields, arguments, and descriptions it exposes using special meta-fields like __schema and __type.
Tools such as GraphiQL, Apollo Studio, and code generators rely on introspection to auto-document APIs and validate queries. However, because it reveals the entire API surface — including internal, deprecated, or not-yet-released fields — an exposed introspection endpoint hands attackers a complete map of your data model. The common mitigation is to disable introspection in production, or gate it behind authentication, while keeping it on in development.
- Powers auto-generated documentation and IDE tooling
- Enables client-side query validation and code generation
- Lets teams explore an unfamiliar API without external docs
- Supports schema diffing to catch breaking changes
- Disabling it in production shrinks the attack surface
AI Mentor Explanation
Introspection is like a team being allowed to read the full match rulebook and the opposition's complete playbook before the game. It is invaluable for coaching and practice, but if you hand the rival captain your entire bowling plan and field-placement chart, they exploit every weakness — so professional teams keep the playbook locked away once the real match begins.
Step-by-Step Explanation
Step 1
Understand the meta-fields
GraphQL exposes __schema and __type on the query root; querying them returns the full type system, field names, arguments, and descriptions.
Step 2
See how tooling uses it
GraphiQL, Apollo Studio, and codegen tools run an introspection query on load to build docs, autocomplete, and typed clients.
Step 3
Recognize the exposure
In production, an open introspection endpoint reveals every type — including internal or unreleased fields — giving attackers a complete API map.
Step 4
Disable or gate it
Turn introspection off in production (e.g. Apollo Server's introspection: false) or restrict it to authenticated internal users.
Step 5
Add layered defenses
Pair this with query depth/complexity limits and disabling field suggestions so error messages don't leak schema hints either.
What Interviewer Expects
- Knows __schema and __type meta-fields
- Can name tools that depend on introspection
- Explains why exposing the full schema is a risk
- Recommends disabling or gating it in production
- Mentions complementary defenses like depth limiting
Common Mistakes
- Thinking introspection is a separate endpoint rather than part of the query system
- Believing disabling introspection fully hides the API (field suggestions can still leak it)
- Leaving introspection on in production by default
- Confusing introspection with general query validation
Best Answer (HR Friendly)
“Introspection is a feature that lets a GraphQL API describe itself, so tools can automatically show what data is available. It's great for developers, but on a live system it can reveal too much, so teams usually turn it off in production to keep the API's details private.”
Code Example
query IntrospectSchema {
__schema {
queryType { name }
types {
name
kind
fields {
name
description
}
}
}
}const server = new ApolloServer({
typeDefs,
resolvers,
// Off in production, on elsewhere for tooling
introspection: process.env.NODE_ENV !== 'production',
});Follow-up Questions
- How would you still allow introspection for internal developers but not the public?
- What is a query depth or complexity limit and why does it complement disabling introspection?
- How can field suggestions in error messages leak schema details even with introspection off?
- What tools break when introspection is disabled, and how do you support them?
- How does introspection relate to persisted queries as a security measure?
MCQ Practice
1. Which meta-field is used to query a GraphQL server's entire type system?
__schema is the root introspection meta-field that returns all types, the query/mutation roots, and directives; __typename only returns the current object's type name.
2. What is the primary security recommendation for introspection?
Disabling introspection in production (or restricting it to authenticated internal users) reduces the attack surface by hiding the full schema map from the public.
3. Why is disabling introspection not a complete defense on its own?
Even with introspection off, helpful 'did you mean' field suggestions in error messages can leak schema details, so they should also be disabled and paired with depth/complexity limits.
Flash Cards
What is GraphQL introspection? — A built-in system where clients query the schema itself via __schema and __type to discover types, fields, and arguments.
Which tools depend on introspection? — GraphiQL, Apollo Studio, IDE autocomplete, and client code generators use it to build docs and typed clients.
Main risk of introspection in production — It exposes the complete API surface — including internal or unreleased fields — giving attackers a full map of your data model.
How to mitigate the risk? — Disable introspection in production or gate it behind auth, and add query depth/complexity limits plus disabled field suggestions.