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

graphql-schema

v0.5.1

Published

Create GraphQL schemas with a fluent/chainable interface

Downloads

669

Readme

graphql-schema

Create GraphQL schemas with a fluent/chainable interface.

Notice to <=0.3.0 users:

The API has been changed significantly. Rather than hacking ES7 classes, graphql-schema now implements a fluent/chainable API. As a bonus, we can define entire schemas.

Installation

npm install graphql-schema

Basic Usage

const rootQueryType = objectType('RootQueryType', 'TODO: Description')
  .field('hello', GraphQLString, 'Say hello to someone')
    .arg('name', GraphQLString, 'The name of the person to say hello to')
    .resolve(root, {name} => `Hello, ${name}`)
  .end();

becomes

var rootQueryType = new GraphQLObjectType({
  name: 'RootQueryType',
  description: 'TODO: Description'
  fields: {
    hello: {
      type: GraphQLString,
      description: 'Say Hello to someone',
      args: {
        name: {
          name: 'name',
          type: GraphQLString,
          description: 'The name of the person to say Hello to'
        }
      }
      resolve: (root, {name}) => `Hello, ${name}`;
    }
  }
});

Full Example

import { interfaceType, objectType, enumType, schemaFrom, listOf, notNull } from 'graphql-schema';

const episodeEnum = enumType('Episode',
    'One of the films in the Star Wars Trilogy')
  .value('NEWHOPE', 4, 'Released in 1977.')
  .value('EMPIRE', 5, 'Released in 1980.')
  .value('JEDI', 6, 'Released in 1983.')
  .end();

const characterInterface = interfaceType('Character',
    'A character in the Star Wars Trilogy')
  .field('id', notNull(GraphQLString), 'The id of the character.')
  .field('name', GraphQLString, 'The name of the character.')
  .field('friends', listOf(characterInterface),
    'The friends of the character, or an empty list if they have none')
  .field('appearsIn', listOf(episodeEnum), 'Which movies they appear in.')
  .resolve((obj) => {
    if (starWarsData.Humans[obj.id] !== undefined) {
      return humanType;
    }
    if (starWarsData.Droids[obj.id] !== undefined) {
      return droidType;
    }
    return null;
  })
  .end();

const humanType = objectType('Human', [characterInterface],
    'A humanoid creature in the Star Wars universe.')
  .field('id', notNull(GraphQLString), 'The id of the human.')
  .field('name', GraphQLString, 'The name of the human.')
  .field('friends', listOf(characterInterface),
    'The friends of the human, or an empty list if they have none', (human) => {
      return getFriends(human);
    })
  .field('appearsIn', listOf(episodeEnum), 'Which movies they appear in.')
  .field('homePlanet', GraphQLString,
    'The home planet of the human, or null if unknown.')
  .end();

const droidType = objectType('Droid', [characterInterface],
    'A mechanical creature in the Star Wars universe.')
  .field('id', notNull(GraphQLString), 'The id of the droid.')
  .field('name', GraphQLString, 'The name of the droid.')
  .field('friends', listOf(characterInterface),
    'The friends of the droid, or an empty list if they have none', (droid) => {
      return getFriends(droid);
    })
  .field('appearsIn', listOf(episodeEnum), 'Which movies they appear in.')
  .field('primaryFunction', GraphQLString, 'The primary function of the droid.')
  .end();

const queryType = objectType('Query')
  .field('hero', characterInterface, () => artoo)
  .field('human', humanType)
    .arg('id', notNull(GraphQLString))
    .resolve((root, {id}) => starWarsData.Humans[id])
  .field('droid', droidType)
    .arg('id', notNull(GraphQLString))
    .resolve((root, {id}) => starWarsData.Droids[id])
  .end();

const mutationType = objectType('Mutation')
  .field('updateCharacterName', characterInterface)
    .arg('id', notNull(GraphQLString))
    .arg('newName', notNull(GraphQLString))
    .resolve((root, {id, newName}) => updateCharacterName(id, newName))
  .end();

const starWarsSchema = schemaFrom(queryType, mutationType);

Cyclic Types

graphql-schema supports cyclic types. Instead of passing in a reference, just pass in a function instead:

const userType = objectType('User')
  .field('friends', () => listOf(userType))
  .end();

API

enumType(name, description)

Define a new GraphQLEnumType

.value(name, value, description)
.deprecated(deprecationReason)

interfaceType(name, description)

Define a new GraphQLInterfaceType.

.field(name, type, description)
.deprecated(deprecationReason)
.arg(name, type, defaultValue, description)
.resolve(fn)

objectType(name, [interfaces], description)

Define a new GraphQLObjectType.

.field(name, type, description)
.deprecated(deprecationReason)
.arg(name, type, defaultValue, description)
.resolve(fn)

schemaFrom(queryRootType, mutationRootType)

Define a new GraphQLSchema from the given root types.

listOf(type)

Define a new GraphQLList(type).

notNull(type)

Define a new GraphQLNonNull(type).

Thanks

Thanks to Florent Cailhol for the chainable interface idea!