Using Schema Introspection

1. Understanding Introspection System

FieldReturns
__schemaWhole schema metadata
__type(name)Single type info
__typenameConcrete type at runtime

2. Querying Schema Types

Example: List all types

{
  __schema {
    types { name kind description }
    queryType { name }
    mutationType { name }
    subscriptionType { name }
  }
}

3. Querying Type Fields

Example: Inspect a type

{
  __type(name: "User") {
    name
    fields {
      name
      type { name kind ofType { name } }
      args { name type { name } defaultValue }
      isDeprecated
      deprecationReason
    }
  }
}

4. Querying Input Fields

Example: Inspect input type

{
  __type(name: "CreateUserInput") {
    inputFields { name type { name } defaultValue description }
  }
}

5. Querying Enum Values

Example: Inspect enum

{
  __type(name: "Role") {
    enumValues(includeDeprecated: false) {
      name description isDeprecated deprecationReason
    }
  }
}

6. Querying Interfaces

Example: Interfaces and implementers

{
  __type(name: "Node") {
    interfaces { name }
    possibleTypes { name }
  }
}

7. Querying Possible Types

UseDetail
Apollo cachepossibleTypes config for fragment matching
CodegenGenerate discriminated unions
ValidationVerify spread compatibility

8. Disabling Introspection

Example: Disable in production

new ApolloServer({
  schema,
  introspection: process.env.NODE_ENV !== "production"
});
Warning: Disabling introspection isn't real security — schema can still be reverse-engineered. Pair with persisted-query allowlists.

9. Using __typename Field

WhereReturns
Object/Interface/UnionConcrete type name string
Always queryableEven on abstract types
Apollo cacheUsed as part of cache normalization key

10. Building Schema Documentation

ToolOutput
graphql-markdownMarkdown docs
SpectaQLStatic HTML
MagidocVue-based site
GraphQL InspectorSchema diff + docs

11. Implementing Schema Diffing

Example: graphql-inspector diff

graphql-inspector diff main.graphql pr.graphql
# Reports breaking, dangerous, non-breaking changes
Change ClassExamples
BreakingField removed, type changed, arg required added
DangerousNew nullable field, default change
SafeNew type, new optional arg