1.0.0 • Published 2 years ago

zod-graphql-introspection v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

zod-graphql-introspection

Easily validate the results of standard GraphQL introspection with zod. "Standard" introspection refers to the results of the introspection query generated by the method getIntrospectionQuery from graphql-js.

Install

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

npm install zod zod-graphql-introspection

Example

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:

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.