matthewmueller-graphql-schema-gen

0.0.3 • Public • Published

graphql-schema-gen

Generate JavaScript GraphQL schema from the GraphQL language

Build Status Dependency Status NPM version

Installation

npm install graphql-schema-gen

Usage

var createQueryFn = require('graphql-schema-gen');

var query = createQueryFn(`
scalar Date

union Animal =
  Cat |
  Dog

enum Role {
  # Means a user has full administrative access to everything
  Admin
  # Means a user has ability to moderate comments
  Moderator
  # Means a user has read only access
  None
}

type Cat {
  nightVision: String
}
type Dog {
  woof: String
}

interface Node {
  id: ID!
}

# A type to represent a person
type Person implements Node {
  id: GlobalID
  name: String
  dateOfBirth: Date
  role: Role
  friends(): [Person]!
  pet(): Animal
  hello(name: String = "World"): String
}

type Query {
  me(): Node
}
`, {
  Date: {
    serialize(value) {
      return /^\d\d\d\d\-\d\d\-\d\d$/.test(value) ? value : null;
    },
    parse(value) {
      return /^\d\d\d\d\-\d\d\-\d\d$/.test(value) ? value : null;
    }
  },
  Node: {
    resolveType(node, info) {
      return info.schema.getType(node.type);
    }
  },
  Animal: {
    resolveType(node, info) {
      return info.schema.getType(node.type);
    }
  },
  Query: {
    me() {
      return {name: 'Forbes Lindesay', dateOfBirth: '1830-06-11', role: 'Admin', type: 'Person'};
    }
  },
  Person: {
    id(obj) {
      return obj.name;
    },
    pet() {
      return {
        woof: 'Woof Woof!',
        type: 'Dog'
      };
    },
    friends() {
      return [{name: 'jonn', dateOfBirth: 'whatever', role: 'None', type: 'Person'}];
    },
    hello(person, {name}, context) {
      return 'Hello ' + name;
    }
  }
});

query(`
{
  me {
    id
    ...on Person {
      role
      dateOfBirth
      name,
      friends { id, name, dateOfBirth, role }
      pet { ...on Dog {woof}}
      hello
      helloForbes: hello(name: "Forbes")
    }
  }
}
`, {}).then(function (result) {
  if (result.errors) {
    setTimeout(() => { throw result.errors[0]; }, 0);
  }
  console.dir(result, {colors: true, depth: 100});
});

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i matthewmueller-graphql-schema-gen

Weekly Downloads

1

Version

0.0.3

License

MIT

Last publish

Collaborators

  • mattmueller