Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/schwer/graphql-to-openapi

Convert a graphql query + graphql schema into an openapi spec.
https://github.com/schwer/graphql-to-openapi

graphql openapi

Last synced: 20 days ago
JSON representation

Convert a graphql query + graphql schema into an openapi spec.

Awesome Lists containing this project

README

        

# graphql-to-openapi

[![NPM](https://img.shields.io/npm/v/graphql-to-openapi.svg)](https://npmjs.com/graphql-to-openapi)
[![Known Vulnerabilities](https://snyk.io/test/github/schwer/graphql-to-openapi/badge.svg)](https://snyk.io/test/github/schwer/graphql-to-openapi)
[![codecov](https://codecov.io/gh/schwer/graphql-to-openapi/branch/master/graph/badge.svg?token=XTRQ95F57X)](https://codecov.io/gh/schwer/graphql-to-openapi)

GraphQL → OpenAPI

Convert a graphql query + graphql schema into an openapi spec.

[Demo](https://schwer.github.io/graphql-to-openapi)

Not to be confused with the obviously more useful
[openapi-to-graphql](https://github.com/ibm/openapi-to-graphql).

# Usage via CLI

```
npx graphql-to-openapi --yaml --schema --query
```

GraphQL → OpenAPI

## Unknown Scalar Configuration via the CLI

Unknown scalars are scalar types in the graphql schema that do not obviously map to an openapi type.

The default behavior for unknown scalars is to treat them as `string` types in the output openapi schema.
If you desire to override that behavior, supply a scalar config file to the graphq-to-openapi tool:

```
Example CLI usage:

npx graphql-to-openapi --scalarConfigFile myScalarConfig.json --yaml --schema --query

---

myScalarConfig.json:
{
"DateTimeScalar": {
"type": "string",
"description": "YYYY-MM-DD date",
"example": "2017-01-01",
"format": "date",
"pattern": "YYYY-MM-DD",
"minLength": 0,
"maxLength": 10
}
}
```

# Usage as a module

```typescript
import { graphqlToOpenApi } from 'graphql-to-openapi';

const { error, openApiSchema, queryErrors, schemaError } = graphqlToOpenApi({
schema,
query,
});

// error will contain any graphql-to-openapi errors if they exist.
// graphql-to-openapi only throws an error if you've failed to name
// the input query. The name of the input query is used to define the
// openapi route name.

// schemaError is an error in the parsing of the input schema.

// queryErrors are errors associated with parsing and validating the input query.
// This includes any validation issues when matching the query with the schema.

// openApiSchema will contain the deserialized openapi schema for the
// specified query.
```

## For Developers contributing to this project

### To get started:

1. Clone the github repo `git clone [email protected]/schwer/graphql-to-openapi`
1. `npm install // install dependencies`
1. `npm run test:watch`

### Updating the documentation:

1. The `docs` subdirectory contains a `create-react-app`.
1. `cd docs && npm run start`