# mgs-graphql-node

> The simple way to generates GraphQL schemas and Sequelize models from your models definition,microservice supported

Latest version **1.1.0** (published 2020-03-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install mgs-graphql-node
pnpm add mgs-graphql-node
yarn add mgs-graphql-node
bun add mgs-graphql-node
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2020-03-13 |
| First published | 2020-03-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 15 |
| Unpacked size | 145.3 KB |
| Known vulnerabilities | 0 (+36 in 7 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | MGS Team |
| Maintainers | chheng |
| Keywords | GraphQL, ORM, Relay, Sequelize, MySQL, PostgreSQL, SQLite, MSSQL, MicroService |

## Links

- npm: https://www.npmjs.com/package/mgs-graphql-node
- Repository: https://github.com/kuncloud/simple-graphql
- Issues: https://github.com/kuncloud/simple-graphql/issues
- npm.io page: https://npm.io/package/mgs-graphql-node

## Dependencies (15)

- [lodash](https://npm.io/package/lodash.md) 4.17.10
- [moment](https://npm.io/package/moment.md) 2.22.2
- [mysql2](https://npm.io/package/mysql2.md) 1.5.3
- [fastify](https://npm.io/package/fastify.md) ^2.12.1
- [camelcase](https://npm.io/package/camelcase.md) 5.0.0
- [sequelize](https://npm.io/package/sequelize.md) 4.38.0
- [dataloader](https://npm.io/package/dataloader.md) 1.4.0
- [node-fetch](https://npm.io/package/node-fetch.md) 2.2.0
- [apollo-server](https://npm.io/package/apollo-server.md) 2.0.8
- [graphql-relay](https://npm.io/package/graphql-relay.md) 0.5.5
- [graphql-tools](https://npm.io/package/graphql-tools.md) 3.0.4
- [graphql-binding](https://npm.io/package/graphql-binding.md) 2.2.2
- [apollo-link-http](https://npm.io/package/apollo-link-http.md) 1.5.4
- [apollo-link-context](https://npm.io/package/apollo-link-context.md) 1.0.8
- [graphql-parse-fields](https://npm.io/package/graphql-parse-fields.md) 1.2.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.1.0 (latest) — 2020-03-13

## README

# Simple-GraphQL

[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

`Simple-GraphQL` generates [GraphQL](https://github.com/graphql/graphql-js) schemas and [Sequelize](http://docs.sequelizejs.com/en/v3/) models from your models definition(support [FlowType](https://flow.org/) static type check). The generated GraphQL schema is compatible with [Relay](https://facebook.github.io/relay/).

>[GraphQL](https://github.com/graphql/graphql-js) is a query language for your API, and a server-side runtime for executing queries by using a type system you define for your data. 

>[Sequelize](http://docs.sequelizejs.com/en/v3/) is a promise-based ORM for Node.js. It supports the dialects `PostgreSQL`, `MySQL`, `SQLite` and `MSSQL` and features solid transaction support, relations, read replication and more.

>[FlowType](https://flow.org/) is a static type checker for your JavaScript code. It does a lot of work to make you more productive. Making you code faster, smarter, more confidently, and to a bigger scale.

## Document

-   [Transaction](https://github.com/logerzhu/simple-graphql/wiki/Transaction)

## Install

```shell
npm install graphql graphql-relay simple-graphql --save
```

## Roadmap
  - [ ] Query cache with [dataloader](https://github.com/facebook/dataloader)
  - [ ] Test
  - [ ] [ < place for your ideas > ](https://github.com/logerzhu/simple-graphql/issues/new)

## Demo & Usage
```
// @flow
const Sequelize = require('sequelize')
const express = require('express')
const graphqlHTTP = require('express-graphql')
const SG from 'simple-graphql'

// 定义Schema
const TodoSchema = SG.schema('Todo').fields({
  title: {
    $type: String,
    required: true
  },
  description: String,
  completed: {
    $type: Boolean,
    required: true
  },
  dueAt: Date
}).queries({
  dueTodos: {
    description: 'Find all due todos',
    $type: ['Todo'],
    args: {
      dueBefore: {
        $type: Date,
        required: true
      }
    },
    resolve: async function ({ dueBefore}, context, info, {models:{Todo}}) {
      return Todo.find({
        where: {
          completed: false,
          dueAt: {
            $lt: dueBefore
          }
        }
      })
    }
  }
}).mutations({
  completedTodo: {
    description: 'Mark the todo task completed.',
    inputFields: {
      todoId: {
        $type: 'Todo',
        required: true
      }
    },
    outputFields: {
      changedTodo: 'Todo'
    },
    mutateAndGetPayload: async function ({todoId}, context, info, {models:{Todo}}) {
      const todo = await Todo.findOne({where: {id: todoId}})
      if (!todo) {
        throw new Error('Todo entity not found.')
      }
      if (!todo.completed) {
        todo.completed = true
        await todo.save()
      }
      return {changedTodo: todo}
    }
  }
})

// 定义Sequelize 链接
const sequelize = new Sequelize('test1', 'postgres', 'Password', {
  host: 'localhost',
  dialect: 'sqlite',
  pool: {
    max: 5,
    min: 0,
    idle: 10000
  },
  // SQLite only
  storage: ':memory:'
})

// 生成GraphQL的schema
const schema = SG.build({sequelize:sequelize, schemas:[TodoSchema]}).graphQLSchema

// 自动建立数据库表
sequelize.sync({
  force: false, // if true, it will drop all existing table and recreate all.
  logging: console.log
}).then(() => console.log('Init DB Done'), (err) => console.log('Init DB Fail', err))

// 启动http服务器
const app = express()

app.use('/graphql', graphqlHTTP({
  schema: schema,
  graphiql: true
}))
app.listen(4000)

```

## License

[MIT](https://github.com/logerzhu/simple-graphql/blob/master/LICENSE)

---
_Source: https://npm.io/package/mgs-graphql-node · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
