Registry / devops / graphql-to-openapi

graphql-to-openapi

JSON →
library4.0.25jsnpmunverified

Converts a GraphQL schema and query into an OpenAPI specification. Current stable version is 4.0.25, released regularly. Key differentiator: focuses on generating OpenAPI from a specific GraphQL query (not full schema), providing a targeted mapping. Includes CLI and programmatic API. Ships TypeScript types.

npm install graphql-to-openapi
INSTALL
IMPORT
SIG · GRAPHQL-TO-OPENAPI
G
graphql-to-openapi
devopsjavascriptv4.0.25
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

graphqlToOpenApi
import { graphqlToOpenApi } from 'graphql-to-openapi'
const graphqlToOpenApi = require('graphql-to-openapi')
Package is ESM-only; named export, not default.
graphqlToOpenApi
import { graphqlToOpenApi } from 'graphql-to-openapi'
import graphqlToOpenApi from 'graphql-to-openapi'
graphqlToOpenApi is a named export, not the default.
GraphqlToOpenApiOptions
import type { GraphqlToOpenApiOptions } from 'graphql-to-openapi'
import { GraphqlToOpenApiOptions } from 'graphql-to-openapi'
It's a type, use `import type` for TypeScript.

Shows programmatic usage: parse schema and query, convert to OpenAPI, handle errors.

import { graphqlToOpenApi } from 'graphql-to-openapi'; import { buildSchema } from 'graphql'; const schema = buildSchema(` type Query { hello: String } `); const query = ` query MyQuery { hello } `; const { error, openApiSchema, queryErrors, schemaError } = graphqlToOpenApi({ schema, query, }); if (error) { console.error('Error:', error); } else if (schemaError) { console.error('Schema error:', schemaError); } else if (queryErrors.length > 0) { console.error('Query errors:', queryErrors); } else { console.log('OpenAPI schema:', JSON.stringify(openApiSchema, null, 2)); }
Debug
Known issues
gotchaThe input query must be named; otherwise graphql-to-openapi throws an error.
fix
Always provide a named operation (e.g., 'query MyQuery { ... }').
affects: >=0.1.0
gotchaUnknown scalar types default to 'string' in OpenAPI output without warning.
fix
Use --scalarConfigFile to override types for custom scalars.
affects: >=0.1.0
gotchaThe CLI output is JSON by default; use --yaml flag for YAML.
fix
Add --yaml to CLI command if YAML output is desired.
affects: >=0.1.0
Errors
Common errors & fixes
Error: GraphQL query must be named
The input query string does not specify a name for the operation.
fix
Add a name to the query, e.g., 'query MyQuery { ... }'.
TypeError: Cannot read properties of undefined (reading 'definitions')
The schema object passed is not valid or is undefined.
fix
Ensure the schema is a valid GraphQL schema object (use buildSchema or similar).
Upgrade
Version history
4.0.25latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
graphql-to-openapi — npm install graphql-to-openapi · libregistry