julienvincent / modelizr

Generate GraphQL queries from models that can be mocked and normalized.
http://julienvincent.github.io/modelizr
176 stars 6 forks source link
graphql graphql-client graphql-query normalizr

modelizr

Coverage Status Build Status npm version Gitter

A Combination of normalizr, fakerjs and GraphQL that allows you to define multipurpose models that can generate GraphQL queries, mock deeply nested data and normalize

Installation

$ yarn add modelizr

What can I use this for?


Read my medium post on why I wrote modelizr.

What does it look like?

import { Modelizr } from 'modelizr'

const ModelData = {
  Person: {
    normalizeAs: "People",
    fields: {
      id: Number,
      firstName: String,
      Books: ["Book"]
    }
  },

  Book: {
    normalizeAs: "Books",
    fields: {
      id: Number,
      title: String,
      Author: "Person"
    }
  }
}

const {query, models: {Person, Book}} = new Modelizr({
  models: ModelData,
  config: {
    endpoint: "http:// ..."
  }
})

query(
  Person({id: 1}
    Book("Books")
  ),

  Book("Books", {ids: [4, 5]})
).then((res, normalize) => {
  normalize(res.body) // -> normalized response.
})

This will generate the following query and make a request using it.

{
  Person(id: 1) {
    id,
    firstName,
    Books {
      id,
      title
    }
  },

  Books(ids: [4, 5]) {
    id,
    title,
    Author {
      id,
      firstName
    }
  }
}

Documentation

NOTE: Documentation for pre-v1.0.0 can be found Here

All documentation is located at julienvincent.github.io/modelizr

Example

navigate to http://localhost:8000 in your browser