simple-graphql
Advanced tools
Comparing version 0.2.3 to 0.2.4
@@ -28,2 +28,6 @@ declare class DataType { | ||
static cls:any; | ||
transaction:any; | ||
options:any; | ||
@@ -30,0 +34,0 @@ |
@@ -58,3 +58,3 @@ 'use strict'; | ||
var Context = function () { | ||
function Context(sequelize) { | ||
function Context(sequelize, options) { | ||
_classCallCheck(this, Context); | ||
@@ -64,3 +64,3 @@ | ||
this.options = { | ||
hooks: [] | ||
hooks: _lodash2.default.get(options, 'hooks', []) | ||
}; | ||
@@ -67,0 +67,0 @@ this.dbModels = {}; |
@@ -220,3 +220,3 @@ 'use strict'; | ||
build: function build(sequelize, models, options) { | ||
var context = new _Context2.default(sequelize); | ||
var context = new _Context2.default(sequelize, options); | ||
@@ -223,0 +223,0 @@ // 添加Model |
{ | ||
"name": "simple-graphql", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"description": "The simple way to generates GraphQL schemas and Sequelize models from your models definition.", | ||
@@ -56,2 +56,3 @@ "main": "lib/index.js", | ||
"pre-commit": "^1.2.2", | ||
"sqlite3": "^3.1.8", | ||
"standard": "^10.0.2" | ||
@@ -58,0 +59,0 @@ }, |
{ | ||
"name": "simple-graphql", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"description": "The simple way to generates GraphQL schemas and Sequelize models from your models definition.", | ||
@@ -56,2 +56,3 @@ "main": "lib/index.js", | ||
"pre-commit": "^1.2.2", | ||
"sqlite3": "^3.1.8", | ||
"standard": "^10.0.2" | ||
@@ -58,0 +59,0 @@ }, |
151
README.md
@@ -31,7 +31,6 @@ # Simple-GraphQL | ||
`Simple-GraphQL` | ||
### Define the model | ||
**Examples** | ||
Todo.js | ||
### Define the model | ||
```javascript | ||
@@ -41,3 +40,3 @@ // @flow | ||
const TodoType = SG.modelRef('Todo') | ||
const TodoType = SG.modelRef('Todo') // Reference to Todo model type | ||
@@ -103,5 +102,13 @@ export default SG.model('Todo').fields({ | ||
### Config the Sequelize database connection. | ||
### Generate the GraphQL Schema and start the server | ||
```javascript | ||
import Sequelize from 'sequelize' | ||
import SG from 'simple-graphql' | ||
import express from 'express' | ||
import graphqlHTTP from 'express-graphql' | ||
import Todo from './Todo' | ||
// Config the Sequelize database connection. | ||
const sequelize = new Sequelize('test1', 'postgres', 'Password', { | ||
@@ -118,37 +125,127 @@ host: 'localhost', | ||
}) | ||
export default sequelize | ||
``` | ||
### Generate the GraphQL Schema | ||
```javascript | ||
import SG from 'simple-graphql' | ||
// Generate the GraphQL Schema | ||
const schema = GS.build(sequelize, [Todo], {}) | ||
//import Todo model and sequlize config ... | ||
const schema = GS.build(sequelize, [Todo], {}) | ||
//After bulid, all sequelize models have defined, then call sequelize.sync will automatic create the schema in database. | ||
// After GS.bulid completed, all sequelize models have defined, and call sequelize.sync will automatic create the schema in database. | ||
sequelize.sync({ | ||
force: false, | ||
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)) | ||
export default | ||
``` | ||
### Start the GraphQL server | ||
```javascript | ||
const express = require('express'); | ||
const graphqlHTTP = require('express-graphql'); | ||
// Start the GraphQL server | ||
const app = express() | ||
const app = express(); | ||
app.use('/graphql', graphqlHTTP({ | ||
schema: MyGraphQLSchema, | ||
schema: schema, | ||
graphiql: true | ||
})); | ||
app.listen(4000); | ||
})) | ||
app.listen(4000) | ||
``` | ||
## Model Definition | ||
``` | ||
Const model = SimpleGraphQL | ||
.model(#name: string, #option: ModelOptionConfig) // Define a Model | ||
.fields(#fields: {[string]:FieldType | FieldTypeConfig}) // Add fields to current model | ||
.links(#links: {[string]:LinkFieldType | LinkFieldTypeConfig}) // Add link fields to current model | ||
.queries(#queries: {[string]: QueryConfig}) // Add GraphQL queries to current model | ||
.mutations(#queries: {[string]: MutationConfig}) // Add GraphQL mutations to current model | ||
.methods(#methods: {[string]:any} // Add instance method to current Model | ||
.statics(#methods: {[string]:any} // Add statics method to current Model | ||
.hasOne(#config: HasOneConfig) | ||
.belongsTo(#config: BelongsToConfig) | ||
.hasMany(#config: HasManyConfig) | ||
.belongsToMany(#config: BelongsToManyConfig) | ||
``` | ||
## Configutation | ||
- [ModelOptionConfig](modeloptionconfig) | ||
- [ModelTableOptionConfig](modeltableoptionconfig) | ||
### ModelOptionConfig | ||
Attribute|Description | ||
------------ | ------------- | ||
description?:string | Model description, using on GraphQL Type description. | ||
singularQuery?:string | if false, the sigular GraphQL query will not be genereated. | ||
pluralQuery?:boolean\|Object | if false, the plural GraphQL query will not be genereated. | ||
addMutation?:boolean\|Object | if false, the add GraphQL mutation will not be genereated. | ||
deleteMutation?:boolean\|Object | if false, the delete GraphQL mutation will not be genereated. | ||
updateMutation?:boolean\|Object | if false, the update GraphQL mutation will not be genereated. | ||
table?:[ModelTableOptionConfig](modeltableoptionconfig) | Reference to [Options](http://docs.sequelizejs.com/en/latest/api/sequelize/#definemodelname-attributes-options-model) of model define [sequelize.define] | ||
### ModelTableOptionConfig | ||
``` | ||
type ModelTableOptionConfig = { | ||
defaultScope?:Object, | ||
scopes?:Object, | ||
omitNull?:boolean, | ||
timestamps?:boolean, | ||
createdAt?:string|boolean, | ||
updatedAt?:string|boolean, | ||
paranoid?:boolean, | ||
deletedAt?:string|boolean, | ||
underscored?:boolean, | ||
underscoredAll?:boolean, | ||
freezeTableName?:boolean, | ||
name?:{ | ||
singular?:string, | ||
plural?:string, | ||
}, | ||
indexes?:Array<{ | ||
name?:string, | ||
type?:'UNIQUE' | 'FULLTEXT' | 'SPATIAL', | ||
method?:'USING' | 'USING' | 'HASH' | 'GIST' | 'GIN', | ||
unique?:boolean, | ||
concurrently?:boolean, | ||
fields?:Array<string | { | ||
attribute?:string, | ||
length?:number, | ||
order?:'ASC' | 'DESC', | ||
collate?:string | ||
}> | ||
}>, | ||
tableName?:string, | ||
getterMethods?:{[string]:() => any}, | ||
setterMethods?:{[string]:(any) => void}, | ||
instanceMethods?:{[string]:any}, | ||
classMethods?:{[string]:any}, | ||
schema?:string, | ||
engine?:string, | ||
charset?:string, | ||
comment?:string, | ||
collate?:string, | ||
rowFormat?:string, | ||
initialAutoIncrement?:string, | ||
validate?: ValidateConfig, | ||
hooks?:{ | ||
beforeBulkCreate?:(Object, Object) => void | Array<(Object, Object) => void>, | ||
beforeBulkDestroy?:(Object) => void | Array<(Object) => void>, | ||
beforeBulkUpdate?:(Object) => void | Array<(Object) => void>, | ||
beforeValidate?:(Object, Object) => void | Array<(Object, Object) => void>, | ||
afterValidate?:(Object, Object) => void | Array<(Object, Object) => void>, | ||
validationFailed?:(Object, Object, Object) => void | Array<(Object, Object, Object) => void>, | ||
beforeCreate?:(Object, Object) => void | Array<(Object, Object) => void>, | ||
beforeDestroy?:(Object, Object) => void | Array<(Object, Object) => void>, | ||
beforeUpdate?:(Object, Object) => void | Array<(Object, Object) => void>, | ||
beforeSave?:(Object, Object) => void | Array<(Object, Object) => void>, | ||
beforeUpsert?:(Object, Object) => void | Array<(Object, Object) => void>, | ||
afterCreate?:(Object, Object) => void | Array<(Object, Object) => void>, | ||
afterDestroy?:(Object, Object) => void | Array<(Object, Object) => void>, | ||
afterUpdate?:(Object, Object) => void | Array<(Object, Object) => void>, | ||
afterSave?:(Object, Object) => void | Array<(Object, Object) => void>, | ||
afterUpsert?:(Object, Object) => void | Array<(Object, Object) => void>, | ||
afterBulkCreate?:(Object, Object) => void | Array<(Object, Object) => void>, | ||
afterBulkDestroy?:(Object) => void | Array<(Object) => void>, | ||
afterBulkUpdate?:(Object) => void | Array<(Object) => void>, | ||
} | ||
} | ||
``` | ||
## Document | ||
@@ -155,0 +252,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
477866
94
4606
254
13