Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/thomasheyenbrock/zod-graphql-introspection
- Owner: thomasheyenbrock
- License: mit
- Created: 2022-10-02T11:30:35.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2022-10-02T11:57:30.000Z (about 2 years ago)
- Last Synced: 2024-11-29T20:49:15.220Z (26 days ago)
- Language: TypeScript
- Size: 12.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
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`.