Registry / testing / openapi-to-graphql

openapi-to-graphql

JSON →
library3.0.7jsnpmunverified

Generates a GraphQL schema from OpenAPI Specifications (v2.0 and v3.0.x). Current stable version is 3.0.7. Development has paused; the project is succeeded by GraphQL Mesh's OpenAPI handler. Key differentiators: automatic creation of GraphQL types from REST API definitions, support for multiple APIs, nested objects via OAS links. Requires GraphQL.js as a peer dependency.

npm install openapi-to-graphql
INSTALL
IMPORT
SIG · OPENAPI-TO-GRAPHQL
O
openapi-to-graphql
testingjavascriptv3.0.7
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.

createGraphQLSchema
import { createGraphQLSchema } from 'openapi-to-graphql'
const createGraphQLSchema = require('openapi-to-graphql')
The package is ESM-compatible but also supports require(). Use named import for this function.
GraphQLSchema
import { GraphQLSchema } from 'graphql'
import GraphQLSchema from 'graphql'
GraphQLSchema is a named export from the graphql package, not a default one.
default
import openapiToGraphql from 'openapi-to-graphql'
import { default } from 'openapi-to-graphql'
The package has a default export that is the same as createGraphQLSchema. Use default import for convenience.

Generates a GraphQL schema from an OpenAPI spec and serves it via Express with GraphiQL.

import { createGraphQLSchema } from 'openapi-to-graphql'; import express from 'express'; import { graphqlHTTP } from 'express-graphql'; const oas = { openapi: '3.0.0', info: { title: 'Sample API', version: '1.0.0' }, paths: { '/hello': { get: { operationId: 'hello', responses: { '200': { description: 'OK', content: { 'application/json': { schema: { type: 'string' } } } } } } } } }; async function main() { const { schema } = await createGraphQLSchema(oas); const app = express(); app.use('/graphql', graphqlHTTP({ schema, graphiql: true })); app.listen(4000); console.log('GraphQL server running at http://localhost:4000/graphql'); } main();
Debug
Known issues
deprecatedDevelopment on OpenAPI-to-GraphQL has paused. It is recommended to use GraphQL Mesh's OpenAPI handler instead.
fix
Switch to @graphql-mesh/openapi from GraphQL Mesh.
affects: >=3.0.0
breakingVersion 3.x changed the main export from 'createGraphQLSchema' to also export 'default'
fix
Use named import { createGraphQLSchema } or default import.
affects: >=3.0.0
gotchaPeer dependency graphql must be installed separately. npm will not install it automatically in some versions.
fix
Run 'npm install graphql' alongside openapi-to-graphql.
affects: *
deprecatedSupport for OpenAPI v2.0 (Swagger) may be removed in future versions.
fix
Migrate to OpenAPI v3.0+ specifications.
affects: >=3.0.0
Errors
Common errors & fixes
Cannot find module 'openapi-to-graphql'
Package not installed or missing in node_modules.
fix
Run 'npm install openapi-to-graphql --save'
Cannot read properties of undefined (reading 'prototype')
Missing peer dependency graphql.
fix
Install graphql via 'npm install graphql'
graphql is not defined
graphql is not imported or installed when using the schema.
fix
Import GraphQL.js: 'import { graphql, GraphQLSchema } from 'graphql''
Cannot find module 'express-graphql'
Missing express-graphql package when serving.
fix
Install it: 'npm install express-graphql --save'
Upgrade
Version history
3.0.7latest on npm
Audit
Dependencies
graphqlrequiredPeer dependency required to generate GraphQL schema
Agent activity
10 hits · last 30 days
node
8
Amazon
1
Resources
openapi-to-graphql — npm install openapi-to-graphql · libregistry