0.2.0 • Published 5 years ago

graphql-mst v0.2.0

Weekly downloads
-
License
MIT
Repository
-
Last release
5 years ago

npm downloads npm codecov CircleCI MIT license

graphql-mst

Convert GraphQL Schema to mobx-state-tree models.

See demos in tests folder

Installing

yarn add graphql-mst
# or
npm install graphql-mst

Usage

import { generateFromSchema } from 'graphql-mst';

const schema = `
  type Foo {
    a: String
    b: Int
  }
  type Bar {
    c: [Foo]
  }
`;

const { Foo, Bar } = generateFromSchema(schema);

const foo = Foo.create({
  a: 'Hello',
  b: 10,
});

const bar = Bar.create({
  c: [foo, { a: 'World', b: 20 }],
});

Identifiers

const schema = `
  type Foo {
    userId: ID!
    fooId: ID!
  }
`;

const config = {
  Foo: {
    identifier: 'fooId', // this will be used as identifier for model 'Foo'
  },
};

const { Foo } = generateFromSchema(schema, config);

const lookup = types
  .model({ items: types.map(Foo) })
  .actions(self => ({ add: item => self.items.put(item) }))
  .create({ items: {} });

lookup.put({ userId: 10, fooId: 1 });
lookup.put({ userId: 20, fooId: 2 });

lookup.items.get(1); // { userId: 10, fooId: 1 }
lookup.items.get(2); // { userId: 20, fooId: 2 }

TODO and thoughts

  • Configure map type instead of array type
  • Default values for arguments as types.optional
  • reference types?
  • Date scalar? Custom scalar?