@eclass/sequelize-graphql-tools
TypeScript icon, indicating that this package has built-in type declarations

5.0.1 • Public • Published

sequelize-graphql-tools

npm version Build Status npm downloads Maintainability devDependency Status

Utils to generate graphql schema, types, querys and mutations from sequelize models

Installation

npm i sequelize-graphql-tools graphql graphql-compose graphql-fields graphql-iso-date sequelize

Use

One model to GraphqlType

const { modelToType } = require('@eclass/sequelize-graphql-tools')
const db = require('./models') // Sequelize instance with all models imported

const options = {
  ignore: ['password'] // Hide password in fields
}
const UserTC = modelToType(db.User.name, db.User.rawAttributes, options)

All model to GraphqlType

const { createTypes } = require('@eclass/sequelize-graphql-tools')
const db = require('./models') // Sequelize instance with all models imported

const options = {
  ignore: ['Session'], // Ignore models
  fields: {
    User: { ignore: ['password'] } // Hide password in User fields
  }
}
const allTypes = createTypes(db, options)
const UserTC = allTypes.User

Append associations

const { createTypes, appendAssociations } = require('@eclass/sequelize-graphql-tools')
const db = require('./models') // Sequelize instance with all models imported

const options = {
  ignore: ['Session'], // Ignore models
  fields: {
    User: { ignore: ['password'] } // Hide password in User fields
  }
}
const allTypes = createTypes(db, options)
Object.keys(allTypes).forEach(name => {
  appendAssociations(allTypes, name, db[name].associations)
})

Generate all querys and mutations

const { GraphQLSchema, GraphQLObjectType } = require('graphql/type')
const { createTypes, appendAssociations } = require('@eclass/sequelize-graphql-tools')
const db = require('./models') // Sequelize instance with all models imported

const options = {
  ignore: ['Session'], // Ignore models
  fields: {
    User: { ignore: ['password'] } // Hide password in User fields
  }
}
const allTypes = createTypes(db, options)
Object.keys(allTypes).forEach(name => {
  appendAssociations(allTypes, name, db[name].associations)
})
const querys = Object.keys(allTypes).reduce((acc, name) => {
  return { ...acc, ...createQuery(db[name], allTypes[name].gqType) }
}, {})
const mutations = Object.keys(allTypes).reduce((acc, name) => {
  return {
    ...acc,
    ...createMutation(db[name], allTypes[name].gqType)
  }
}, {})
const query = new GraphQLObjectType({
  name: 'query',
  fields: () => ({ ...querys })
})
const mutation = new GraphQLObjectType({
  name: 'mutation',
  fields: () => ({ ...mutations })
})
const schema = new GraphQLSchema({ query, mutation })

Licencia

MIT

Readme

Keywords

Package Sidebar

Install

npm i @eclass/sequelize-graphql-tools

Weekly Downloads

22

Version

5.0.1

License

MIT

Unpacked Size

44.4 kB

Total Files

8

Last publish

Collaborators

  • eclass-bot
  • lgatica
  • enbonnet
  • segonzalezr
  • tsycl
  • asmaelrebo