Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/thomasheyenbrock/zod-graphql-introspection

Validate the result of GraphQL Introspection with Zod
https://github.com/thomasheyenbrock/zod-graphql-introspection

Last synced: 10 days ago
JSON representation

Validate the result of GraphQL Introspection with Zod

Awesome Lists containing this project

README

        

# zod-graphql-introspection

Easily validate the results of standard GraphQL introspection with [`zod`](https://github.com/colinhacks/zod). "Standard" introspection refers to the results of the introspection query generated by the method [`getIntrospectionQuery`](https://github.com/graphql/graphql-js/blob/main/src/utilities/getIntrospectionQuery.ts) from `graphql-js`.

## Install

`zod` is a peer dependency of this package and needs to be installed separately.

```sh
npm install zod zod-graphql-introspection
```

## Example

```js
import { introspectionQuery } from "zod-graphql-introspection";

const introspection = {
__schema: {
queryType: { name: "Query" },
mutationType: null,
subscriptionType: null,
types: [
{
kind: "OBJECT",
name: "Query",
fields: [
{
name: "hello",
args: [
{
name: "name",
type: { kind: "SCALAR", name: "String", ofType: null },
defaultValue: '"world"',
},
],
type: { kind: "SCALAR", name: "String", ofType: null },
isDeprecated: false,
deprecationReason: null,
},
],
inputFields: null,
interfaces: [],
enumValues: null,
possibleTypes: null,
},
],
directives: [],
},
};

const result = introspectionQuery.safeParse(introspection);
expect(result.success).toBe(true);
```

The introspection data shown above is the minimal example for the following schema:

```graphql
type Query {
hello(name: String): String
}
```

### Different GraphQL versions

This package defaults to the latest major GraphQL version (currently `16`). It also provides a validation schema for the previous major version `15` by importing from `zod-graphql-introspection/15`.