Overview
GraphDoc supports four schema loading methods:- HTTP endpoint - Query a live GraphQL API via introspection
- JSON file - Use a pre-generated introspection result
- GraphQL IDL/SDL file - Load from schema definition language files
- JavaScript file - Import modularized schema definitions
You must provide either
--endpoint or --schema-file, but not both. GraphDoc determines the loader based on the file extension when using --schema-file.HTTP endpoint
The HTTP endpoint method queries a live GraphQL server using introspection to retrieve the schema. This is the most common approach for documenting production APIs.Basic usage
With authentication
Most production APIs require authentication. Use the--header flag to include authentication tokens:
Multiple headers
You can include multiple headers by repeating the--header flag:
Query parameters
Some APIs require query string parameters. Use the--query flag:
Configuration example
Inpackage.json:
package.json
How it works
When you provide an endpoint, GraphDoc:- Sends a POST request with the GraphQL introspection query
- Includes any headers specified with
--header - Appends any query parameters specified with
--query - Parses the introspection result from the response
- Extracts the schema from
data.__schemaor__schemaproperty
Error handling
HTTP 401 Unauthorized
HTTP 401 Unauthorized
HTTP 404 Not Found
HTTP 404 Not Found
Error:
Unexpected HTTP Status Code 404 (Not Found)Cause: The endpoint URL is incorrect or the GraphQL API is not available at that path.Solution: Verify the endpoint URL. Common paths are /graphql, /api/graphql, or /v1/graphql.Connection refused
Connection refused
Error:
Error: connect ECONNREFUSEDCause: The server is not running or is not accessible.Solution: Ensure the GraphQL server is running and accessible at the specified URL.JSON file
The JSON file method uses a pre-generated GraphQL introspection query result. This is useful when you already have an introspection result or when the API is not accessible during documentation generation.File format
The JSON file must contain the complete result of a GraphQL introspection query:schema.json
data wrapper):
schema.json
Usage
Configuration example
package.json
Generating introspection JSON
You can generate an introspection JSON file using various tools:Use cases
- Offline documentation: Generate docs without accessing the live API
- CI/CD pipelines: Use a cached schema to avoid API calls during builds
- Legacy APIs: Document APIs that are no longer accessible
- Version control: Track schema changes by committing JSON files
GraphQL IDL file
The GraphQL Interface Definition Language (IDL), also called Schema Definition Language (SDL), is the human-readable syntax for defining GraphQL schemas.Supported extensions
GraphDoc recognizes these file extensions as IDL files:.graphql.gql.gqls.graphqls
File format
schema.graphql
Usage
Configuration example
package.json
How it works
When you provide an IDL file, GraphDoc:- Reads the file contents (located at
lib/schema-loader/idl.ts:19-20) - Builds a GraphQL schema using
buildSchema()fromgraphql-js(line 22) - Executes the introspection query against the built schema (lines 21-24)
- Extracts the schema from the introspection result
Use cases
- Schema-first development: Document your schema before implementation
- Version control friendly: IDL files are human-readable and diff-friendly
- Standard format: Works with any tool that outputs GraphQL SDL
- Manual schema definition: Perfect for hand-crafted schemas
JavaScript file
The JavaScript file method supports modularized schema definitions, which is common in projects usinggraphql-tools or similar libraries.
File format
GraphDoc supports JavaScript files that export schema definitions. The file must have a.js extension and export one of these formats:
Array export (default export)
schema.js
Function returning array
schema.js
Usage
Configuration example
package.json
How it works
When you provide a JavaScript file, GraphDoc (fromlib/schema-loader/js.ts:15-49):
- Requires the module (line 19)
- Checks if the module exports a
defaultproperty (lines 23-25) - Determines if it’s an array or function:
- If array: Uses it directly (line 29)
- If function: Calls it and uses the returned array (line 32-33)
- Joins all array elements into a single string (lines 29, 33)
- Builds a schema using
buildSchema()(line 41) - Executes introspection query against the schema (lines 40-43)
- Extracts the schema from the result
Real-world example
Here’s the complete Star Wars example from the GraphDoc test suite:test/starWars.js
Use cases
- Modularized schemas: Perfect for projects using
graphql-toolswith separate type definition files - Code generation: When your schema is generated by a build process
- Dynamic schemas: When schema definitions need to be computed at runtime
- Apollo/graphql-tools compatibility: Works with the modularized schema pattern from Apollo documentation
Error handling
Module not found
Module not found
Error:
Cannot find module './schema.js'Cause: The file path is incorrect or the file doesn’t exist.Solution: Verify the file path is correct and relative to your working directory.Invalid export format
Invalid export format
Error:
Unexpected schema definition on "...", must be an array or functionCause: The JavaScript file doesn’t export an array or function.Solution: Ensure your file exports either:export default [...](array)export default () => [...](function returning array)
Choosing the right schema source
Use this guide to choose the best schema source for your needs:HTTP Endpoint
Best for:
- Live APIs
- Production documentation
- Automated documentation updates
- Always-up-to-date schemas
- Requires network access
- May need authentication
- API must be running
JSON File
Best for:
- Offline documentation
- CI/CD pipelines
- Cached schemas
- Legacy APIs
- Must be manually updated
- Can become outdated
- Verbose format
GraphQL IDL
Best for:
- Schema-first development
- Version control
- Human-readable schemas
- Manual documentation
- Must be kept in sync with code
- Single-file only
JavaScript File
Best for:
- Modularized schemas
- graphql-tools projects
- Dynamic schemas
- Build processes
- Requires Node.js execution
- More complex setup
Common workflows
Development workflow
Production workflow
CI/CD workflow
.github/workflows/docs.yml
Troubleshooting
Schema validation errors
Schema validation errors
If GraphDoc reports schema validation errors, ensure your schema is valid GraphQL. Use tools like:
- GraphQL Playground
- GraphiQL
graphql-jsvalidation functions
Introspection disabled
Introspection disabled
Some production APIs disable introspection for security. In this case:
- Generate the schema from your development environment
- Save it to a JSON file
- Use the JSON file as your schema source
Large schema files
Large schema files
For very large schemas (thousands of types), consider:
- Using JSON files instead of endpoints to reduce network overhead
- Enabling
--verboseto monitor progress - Increasing Node.js memory limit:
node --max-old-space-size=4096 $(which graphdoc) ...