https://github.com/jcoreio/graphql-sequelize-extra
some missing pieces of the graphql-sequelize bridge
https://github.com/jcoreio/graphql-sequelize-extra
es2015
Last synced: about 1 year ago
JSON representation
some missing pieces of the graphql-sequelize bridge
- Host: GitHub
- URL: https://github.com/jcoreio/graphql-sequelize-extra
- Owner: jcoreio
- License: mit
- Created: 2017-12-19T04:35:29.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T01:56:56.000Z (over 3 years ago)
- Last Synced: 2025-03-18T11:45:23.678Z (about 1 year ago)
- Topics: es2015
- Language: JavaScript
- Size: 3.67 MB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 18
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# @jcoreio/graphql-sequelize-extra
[](https://circleci.com/gh/jcoreio/graphql-sequelize-extra)
[](https://codecov.io/gh/jcoreio/graphql-sequelize-extra)
[](https://github.com/semantic-release/semantic-release)
[](http://commitizen.github.io/cz-cli/)
[](https://badge.fury.io/js/%40jcoreio%2Fgraphql-sequelize-extra)
some missing pieces of the graphql-sequelize bridge
# Installation
```sh
npm install --save @jcoreio/graphql-sequelize-extra
```
# API
## `associationFields(model, options)`
```js
const { associationFields } = require('@jcoreio/graphql-sequelize-extra')
```
Creates GraphQL fields for the given sequelize `model`'s `associations`.
### Arguments
#### `model: Class>`
#### `options: Object`
##### `options.getType: (model: Class>) => Object` (required)
Gets the GraphQL type for the given sequelize model.
##### `options.getArgs: (model: Class>) => Object` (optional)
Gets the GraphQL args for the given sequelize model.
##### `options.getResolver: (model: Class>, association: Sequelize.Association) => Function` (optional)
Gets the GraphQL `resolve` function for the given sequelize model and association.
### Returns
A plain object of GraphQL fields for the given `model`'s
`associations`.
### Advice
You should use `associationFields` within a `fields` thunk since it some
types it needs would fail to be defined if there are circular
associations:
```js
const { models } = sequelize
function getType(model) {
return types[model.name]
}
const types = mapValues(
models,
(model) =>
new graphql.GraphQLObjectType({
name: model.name,
fields: () => ({
...attributeFields(model),
...associationFields(model, { getType }),
}),
})
)
```
If you have circular associations, you should [use some form of
nested GraphQL query attack prevention](https://stackoverflow.com/questions/37337466/how-do-you-prevent-nested-attack-on-graphql-apollo-server).