npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@autotelic/graphql-schema-tools

v0.4.0

Published

Tools for GraphQL schemas.

Downloads

665

Readme

GraphQL Schema Tools

Usage

npm i @autotelic/graphql-schema-tools

API

Contents

normalizeGQLSource: (source: string, options?: object) => object

When passed a string GQL source, normalizeGQLSource groups top-level declarations by kind and then alphabetizes them along with their fields, directives, arguments, values, types, interfaces, and locations - all by name.

Accepts an optional options object containing the following properties:

  • minify: boolean - If true, the returned source will have all redundant whitespace stripped out. Defaults to false.

Returns an object with the following properties:

  • source: string- The normalized GQL source.
  • error: GraphQLError | undefined - If an error occurs, it will be added here.
Example
const { normalizeGQLSource } = require('@autotelic/graphql-schema-tools')

const schema = `
  type Query {
    products(priceUnder: Float priceOver: Float category: String): [Products]! @paginate @cacheControl(maxAge: 2000 scope: PUBLIC)
    customer(id: ID): Customer
  }

 type Product {
    name: String!
    price: Float!
    category: String!
    id: ID!
  }

  type Customer @cacheControl(scope: PRIVATE maxAge: 1000) {
    name: String!
    address: String!
    id: ID!
  }

  directive @paginate on FIELD_DEFINITION
  directive @cacheControl(scope: CacheScope maxAge: Int) on OBJECT | FIELD_DEFINITION

  enum CacheScope {
    PUBLIC
    PRIVATE
  }
`

const { source, error } = normalizeGQLSource(schema)

Given the above schema, the returned source would look like:

directive @cacheControl(maxAge: Int, scope: CacheScope) on FIELD_DEFINITION | OBJECT

directive @paginate on FIELD_DEFINITION

enum CacheScope {
  PRIVATE
  PUBLIC
}

type Customer @cacheControl(maxAge: 1000, scope: PRIVATE) {
  address: String!
  id: ID!
  name: String!
}

type Product {
  category: String!
  id: ID!
  name: String!
  price: Float!
}

type Query {
  customer(id: ID): Customer
  products(category: String, priceOver: Float, priceUnder: Float): [Products]! @cacheControl(maxAge: 2000, scope: PUBLIC) @paginate
}

minifyGQLSource: (source: string) => string

Returns the provided source with all redundant whitespace stripped out.

Example
const { minifyGQLSource } = require('@autotelic/graphql-schema-tools')

const schema = `
  type Query {
    foo: String
    bar(foo: String, id: ID): String
  }
`

const minified = minifyGQLSource(schema)
// The resulting minified schema will look like:
// 'type Query{foo:String bar(foo:String,id:ID):String}'

enhanceGQLSyntaxError: (error: GraphQLError) => GraphQLError

If the provided error contains source and locations properties, enhanceGQLSyntaxError will return the provided GraphQLError with a modified message - containing a condensed snippet showing where in the source the error occurred.

Example
const { enhanceGQLSyntaxError } = require('@autotelic/graphql-schema-tools')

const schemaWithError = `
  type Foo {
    id: ID!
    bar: String
  }



}
    `

try {
  parse(schemaWithError)
} catch (error) {
  const enhancedError = enhanceGQLSyntaxError(error)
  console.log(enhancedError.message)
  // Logs: 'Syntax Error: Unexpected "}". Found near: `bar: String } }`.'
}

printSDL: (schema: GraphQLSchema, options: object) => DocumentNode

Returns an SDL string representation of the provided schema.

Accepts an optional options object containing the following properties:

  • minify: boolean - If true, the returned source will have all redundant whitespace stripped out. Defaults to false.
  • filterTypes: string[] | (GraphQLNamedType) => boolean - Accepts an array of type names that will be filtered out of the returned AST. Alternatively a custom filter function can be passed in.
  • filterDirectives: string[] | (GraphQLDirective) => boolean - Accepts an array of directive names that will be filtered out of the returned AST. Alternatively a custom filter function can be passed in.
Example
const { printSDL } = require('@autotelic/graphql-schema-tools')
const federatedSchema = require('./schema')

const SDL = printSDL(federatedSchema, {
  minify: true,
  filterDirectives: ['key', 'external', 'requires', 'provides', 'extends'],
  filterFields: {
    Query: ['_service', '_entities']
  },
  filterTypes: ['_Any', '_FieldSet', '_Service']
})

astFromSchema: (schema: GraphQLSchema, options: object) => DocumentNode

Returns an AST representation of the provided schema.

Accepts an optional options object containing the following properties:

  • filterTypes: string[] | (TypeDefinitionNode) => boolean - Accepts an array of type names that will be filtered out of the returned AST. Alternatively a custom filter function can be passed in.
  • filterDirectives: string[] | (DirectiveDefinitionNode) => boolean - Accepts an array of directive names that will be filtered out of the returned AST. Alternatively a custom filter function can be passed in.
  • filterFields: { [string]: string[] } | (FieldDefinitionNode, TypeDefinitionNode) => boolean - Accepts an object with keys of type names and values of arrays of field names to filter out of the returned AST. Alternatively a custom filter function can be passed in - the function will receive the current field (FieldDefinitionNode) and the parent type (TypeDefinitionNode).
Example
const { astFromSchema } = require('@autotelic/graphql-schema-tools')
const federatedSchema = require('./schema')

const document = astFromSchema(federatedSchema, {
  filterDirectives: ['key', 'external', 'requires', 'provides', 'extends'],
  filterFields: {
    Query: ['_service', '_entities']
  },
  filterTypes: ['_Any', '_FieldSet', '_Service']
})