Registry / database / graphql-sequelize

graphql-sequelize

JSON →
library9.5.1jsnpmunverified

Connects GraphQL resolvers to Sequelize models for MySQL/Postgres databases. Version 9.5.1 supports graphql ^0.5.0 through ^16, graphql-relay, and sequelize >=3.0.0. Key features include automatic argument-to-where conversion for model attributes, built-in limit/order argument handling, Relay connection support, and before/after hooks to manipulate queries and results. Typically used with dataloader-sequelize to prevent N+1 queries. Active maintenance but with limited updates; known breaking changes in v9 with removal of default import.

npm install graphql-sequelize
INSTALL
IMPORT
SIG · GRAPHQL-SEQUELIZE
G
graphql-sequelize
databasejavascriptv9.5.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

resolver
import { resolver } from 'graphql-sequelize'
const resolver = require('graphql-sequelize').resolver
Since v9, graphql-sequelize is ESM-only. Use named import.
default (package itself)
import graphqlSequelize from 'graphql-sequelize'
import * as graphqlSequelize from 'graphql-sequelize'
Default import was removed in v9. Use named imports instead.
type definitions
import type { ResolverOptions } from 'graphql-sequelize'
import { ResolverOptions } from 'graphql-sequelize'
Type imports are available. Use 'import type' in TypeScript for type-only imports.

Sets up a minimal GraphQL schema with Sequelize models and resolver for users and tasks.

import { resolver } from 'graphql-sequelize'; import { GraphQLObjectType, GraphQLString, GraphQLInt, GraphQLNonNull, GraphQLList, GraphQLSchema, graphql } from 'graphql'; import { Sequelize, DataTypes } from 'sequelize'; const sequelize = new Sequelize('sqlite::memory:'); const User = sequelize.define('user', { name: DataTypes.STRING }); const Task = sequelize.define('task', { title: DataTypes.STRING }); User.hasMany(Task, { as: 'tasks' }); const taskType = new GraphQLObjectType({ name: 'Task', fields: { id: { type: new GraphQLNonNull(GraphQLInt) }, title: { type: GraphQLString } } }); const userType = new GraphQLObjectType({ name: 'User', fields: { id: { type: new GraphQLNonNull(GraphQLInt) }, name: { type: GraphQLString }, tasks: { type: new GraphQLList(taskType), resolve: resolver(User.associations.tasks) } } }); const schema = new GraphQLSchema({ query: new GraphQLObjectType({ name: 'Query', fields: { users: { type: new GraphQLList(userType), args: { name: { type: GraphQLString } }, resolve: resolver(User, { list: true }) } } }) }); sequelize.sync().then(() => { return graphql({ schema, source: '{ users { name tasks { title } } }' }); }).then(response => { console.log(JSON.stringify(response)); });
Debug
Known issues
breakingDefault import removed in v9 - `import graphqlSequelize from 'graphql-sequelize'` no longer works.
fix
Use named imports: `import { resolver } from 'graphql-sequelize'`.
affects: >=9.0.0
breakingRemoved `relay` and `mutation` exports in v9 – only `resolver` and helper utilities remain.
fix
Use `graphql-relay` directly for Relay utilities, and define mutations manually.
affects: >=9.0.0
gotchaBefore/after hooks must return `findOptions`/`result` or a promise thereof – forgetting to return will cause silent failures.
fix
Always explicitly return the modified object or a promise from `before` and `after` hooks.
affects: *
gotchaAutomatic `where` conversion from args only works if arg keys exactly match model attributes – nested or virtual attributes are ignored.
fix
Use `before` hook to manually construct `where` for non-attribute args.
affects: *
deprecated`contextToOptions` is deprecated in v9 – use a custom `before` hook to transfer context.
fix
Replace `contextToOptions` with manual assignment in `before` hook.
affects: >=9.0.0
Errors
Common errors & fixes
Cannot find module 'graphql-sequelize'
Package not installed or missing in node_modules.
fix
Run `npm install graphql-sequelize` and ensure both `graphql` and `sequelize` are installed.
TypeError: resolver is not a function
Using default import incorrectly after v9.
fix
Use named import: `import { resolver } from 'graphql-sequelize'`.
Expected 'findOptions' to be an object but got undefined
`before` hook did not return the `findOptions` object.
fix
Ensure `before` returns `findOptions` or a Promise resolving to it.
Cannot read property 'tasks' of undefined
Association not defined or incorrectly referenced in resolver.
fix
Verify association is defined on the model: `User.hasMany(Task, { as: 'tasks' })` and access via `User.associations.tasks`.
Upgrade
Version history
9.5.1latest on npm
Audit
Dependencies
graphqlrequiredPeer dependency for GraphQL type system and resolver support
graphql-relayoptionalPeer dependency for Relay connection handling
sequelizerequiredPeer dependency for Sequelize model definitions
Agent activity
4 hits · last 30 days
node
4
Resources
graphql-sequelize — npm install graphql-sequelize · libregistry