0.9.27 • Published 9 years ago

graphql-tlc v0.9.27

Weekly downloads
89
License
SEE LICENSE IN <N...
Repository
github
Last release
9 years ago

graphql-tlc

A simpler interface to GraphQL

Install

  • NPM - npm install graphql-tlc

Motivation

GraphQL normally requires a GraphQLSchema object passed along with each query you give it to validate, interpret & execute. Typically this schema is constructed by hand-crafting some verbose & noisy JavaScript.

See: starWarsSchema.js.

The equivalent schema in GraphQL Type Language is much more concise:

enum Episode { NEWHOPE, EMPIRE, JEDI }

type Human {
  id: ID!
  name: String
  friends: [Character]
  appearsIn: [Episode]
  homePlanet: String
}

type Droid {
  id: ID!
  name: String
  friends: [Character]
  appearsIn: [Episode]
  primaryFunction: String
}

union Character = Human | Droid

Given a specification of a data model in GraphQL Type Language, graphql-tlc automatically generates the GraphQLSchema instance that GraphQL requires and binds its resolve methods to a specified set of functions for querying (i.e., selecting) and mutating (i.e., insert, update and delete mutations).

Example

$ node --harmony-destructuring
> var tlc = require('graphql-tlc');
> var gql = require('graphql');
> var albums = [
... {'id': 1, 'name': 'Dark Side Of The Moon', 'releaseDate': 'March 1, 1973',
...   'artist': 'Pink Floyd'},
... {'id': 2, 'name': 'The Beatles', 'releaseDate': 'November 22, 1968',
...   'artist': 'The Beatles'},
... {'id': 3, 'name': 'The Wall', 'releaseDate': 'Auguest 1, 1982',
...   'artist': 'Pink Floyd'}];
> var dataResolver = {"query":  function (typename, predicate) {
...   console.assert(typename == "Album");
...   if (predicate == "all()") return albums;
...   else {
...     var predicates = predicate.split("&");
...     var filters = predicates.map(function(p) {
...       var [field, value] = p.split("=");
...       return function(elem) { return elem[field] == value; };
...     });
...     return albums.filter(function(elem) { return filters.every(function(f) { return f(elem); }); });
...   }
... }, "create": function (typename, inputs) {
...   inputs.id = albums.length + 1;
...   albums.push(inputs);
...   return inputs;
... }};
> var schema = tlc.getSchema(dataResolver,
... "type Album { id: ID! name: String releaseDate: String artist: String }");
> var printer = function(res) { console.log(JSON.stringify(res, null, 2)); };
> gql.graphql(schema, "{ Album(artist: \"Pink Floyd\") { name artist releaseDate } }").then(printer);

{
  "data": {
    "Album": [
      {
        "name": "Dark Side Of The Moon",
        "artist": "Pink Floyd",
        "releaseDate": "March 1, 1973"
      },
      {
        "name": "The Wall",
        "artist": "Pink Floyd",
        "releaseDate": "August 1, 1982"
      }
    ]
  }
}

> gql.graphql(schema, "{ Album(artist: \"Pink Floyd\", name: \"The Wall\") { name artist releaseDate } }").then(printer);

{
  "data": {
    "Album": [
      {
        "name": "The Wall",
        "artist": "Pink Floyd",
        "releaseDate": "August 1, 1982"
      }
    ]
  }
}

> gql.graphql(schema, "{ Album(id: 2) { name artist releaseDate } }").then(printer);

{
  "data": {
    "Album": [
      {
        "name": "The Beatles",
        "artist": "The Beatles",
        "releaseDate": "November 22, 1968"
      }
    ]
  }
}

> gql.graphql(schema, "{ Albums { name artist releaseDate } }").then(printer);

{
  "data": {
    "Albums": [
      {
        "name": "Dark Side Of The Moon",
        "artist": "Pink Floyd",
        "releaseDate": "March 1, 1973"
      },
      {
        "name": "The Beatles",
        "artist": "The Beatles",
        "releaseDate": "November 22, 1968"
      },
      {
        "name": "The Wall",
        "artist": "Pink Floyd",
        "releaseDate": "Auguest 1, 1982"
      }
    ]
  }
}

> gql.graphql(schema, "mutation m { createAlbum(name:\"The Division Bell\", releaseDate: \"March 28, 1994\", artist:\"Pink Floyd\") { id name } }").then(printer);

{
  "data": {
    "createAlbum": {
      "id": "4",
      "name": "The Division Bell"
    }
  }
}

Copyright (c) 2015 Jonathan L. Leonard

0.9.27

9 years ago

0.9.26

9 years ago

0.9.25

9 years ago

0.9.24

9 years ago

0.9.23

9 years ago

0.9.22

9 years ago

0.9.21

9 years ago

0.9.20

10 years ago

0.9.19

10 years ago

0.9.18

10 years ago

0.9.17

10 years ago

0.9.16

10 years ago

0.9.15

10 years ago

0.9.14

10 years ago

0.9.13

10 years ago

0.9.12

10 years ago

0.9.11

10 years ago

0.9.10

10 years ago

0.9.9

10 years ago

0.9.8

10 years ago

0.9.7

10 years ago

0.9.6

10 years ago

0.9.5

10 years ago

0.9.4

10 years ago

0.9.3

10 years ago

0.9.2

10 years ago

0.9.1

10 years ago

0.9.0

10 years ago