0
0
GraphqlHow-ToBeginner · 3 min read

How to Enable Introspection in Apollo GraphQL Server

To enable introspection in Apollo Server, set the introspection option to true in the server configuration. This allows clients and tools to query the schema for its structure, which is useful for development and debugging.
📐

Syntax

The introspection option is a boolean flag in Apollo Server's configuration object. Setting it to true enables schema introspection queries, while false disables them.

This option is passed when creating a new ApolloServer instance.

javascript
const server = new ApolloServer({
  typeDefs,
  resolvers,
  introspection: true // Enable introspection
});
💻

Example

This example shows a simple Apollo Server setup with introspection enabled. You can query the server's schema using tools like GraphQL Playground or Apollo Studio.

javascript
import { ApolloServer, gql } from 'apollo-server';

const typeDefs = gql`
  type Query {
    hello: String
  }
`;

const resolvers = {
  Query: {
    hello: () => 'Hello world!'
  }
};

const server = new ApolloServer({
  typeDefs,
  resolvers,
  introspection: true // Enable introspection
});

server.listen().then(({ url }) => {
  console.log(`Server ready at ${url}`);
});
Output
Server ready at http://localhost:4000/
⚠️

Common Pitfalls

One common mistake is disabling introspection in production by setting introspection: false but forgetting to enable it during development, which blocks schema exploration tools.

Another pitfall is relying on older Apollo Server versions where introspection was enabled by default but now requires explicit enabling.

javascript
/* Wrong: introspection disabled, blocking schema tools */
const server = new ApolloServer({
  typeDefs,
  resolvers,
  introspection: false
});

/* Right: introspection enabled for development */
const serverDev = new ApolloServer({
  typeDefs,
  resolvers,
  introspection: true
});
📊

Quick Reference

OptionTypeDescription
introspectionbooleanEnables or disables GraphQL schema introspection queries
defaulttrue (Apollo Server 2.x), false (Apollo Server 3+)Default behavior depends on Apollo Server version
use caseEnable during development for schema exploration; disable in production for security

Key Takeaways

Set the introspection option to true in Apollo Server config to enable schema introspection.
Introspection helps tools and clients explore your GraphQL schema during development.
Disable introspection in production to improve security, but keep it enabled in development.
Apollo Server versions 3 and above disable introspection by default, so enable it explicitly.
Always verify your Apollo Server version and configure introspection accordingly.