
Security News
ESLint Adds Official Support for Linting HTML
ESLint now supports HTML linting with 48 new rules, expanding its language plugin system to cover more of the modern web development stack.
graphql-sequelize-teselagen
Advanced tools
$ npm install --save graphql-sequelize
graphql-sequelize assumes you have graphql and sequelize installed.
A helper for resolving graphql queries targeted at Sequelize models or associations. Please take a look at the tests to best get an idea of implementation.
import {resolver} from 'graphql-sequelize';
let User = sequelize.define('user', {
name: Sequelize.STRING
});
let Task = sequelize.define('task', {
title: Sequelize.STRING
});
User.Tasks = User.hasMany(Task, {as: 'tasks'});
let taskType = new GraphQLObjectType({
name: 'Task',
description: 'A task',
fields: {
id: {
type: new GraphQLNonNull(GraphQLInt),
description: 'The id of the task.',
},
title: {
type: GraphQLString,
description: 'The title of the task.',
}
}
});
let userType = new GraphQLObjectType({
name: 'User',
description: 'A user',
fields: {
id: {
type: new GraphQLNonNull(GraphQLInt),
description: 'The id of the user.',
},
name: {
type: GraphQLString,
description: 'The name of the user.',
},
tasks: {
type: new GraphQLList(taskType),
resolve: resolver(User.Tasks)
}
}
});
let schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'RootQueryType',
fields: {
user: {
type: userType,
// args will automatically be mapped to `where`
args: {
id: {
description: 'id of the user',
type: new GraphQLNonNull(GraphQLInt)
}
},
resolve: resolver(User)
}
}
})
});
let schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'RootQueryType',
fields: {
users: {
// The resolver will use `findOne` or `findAll` depending on whether the field it's used in is a `GraphQLList` or not.
type: new GraphQLList(userType),
args: {
// An arg with the key limit will automatically be converted to a limit on the target
limit: {
type: GraphQLInt
},
// An arg with the key order will automatically be converted to a order on the target
order: {
type: GraphQLString
}
},
resolve: resolver(User)
}
}
})
});
field helpers help you automatically define a models attributes as fields for a GraphQL object type.
var Model = sequelize.define('User', {
email: {
type: Sequelize.STRING,
allowNull: false
},
firstName: {
type: Sequelize.STRING
},
lastName: {
type: Sequelize.STRING
}
});
import {attributeFields} from 'graphql-sequelize';
attributeFields(Model, {
// ... options
exclude: Array, // array of model attributes to ignore - default: []
only: Array, // only generate definitions for these model attributes - default: null
globalId: Boolean, // return an relay global id field - default: false
map: Object, // rename fields - default: {}
allowNull: Boolean, // disable wrapping mandatory fields in `GraphQLNonNull` - default: false
commentToDescription: Boolean, // convert model comment to GraphQL description - default: false
cache: Object, // Cache enum types to prevent duplicate type name error - default: {}
});
/*
{
id: {
type: new GraphQLNonNull(GraphQLInt)
},
email: {
type: new GraphQLNonNull(GraphQLString)
},
firstName: {
type: GraphQLString
},
lastName: {
type: GraphQLString
}
}
*/
userType = new GraphQLObjectType({
name: 'User',
description: 'A user',
fields: _.assign(attributeFields(Model), {
// ... extra fields
})
});
attributeFields
uses the graphql-sequelize typeMapper
to map Sequelize types to GraphQL types. You can supply your own
mapping function to override this behavior using the mapType
export.
var Model = sequelize.define('User', {
email: {
type: Sequelize.STRING,
allowNull: false
},
isValid: {
type: Sequelize.BOOLEAN,
allowNull: false
}
});
import {attributeFields,typeMapper} from 'graphql-sequelize';
typeMapper.mapType((type) => {
//map bools as strings
if (type instanceof Sequelize.BOOLEAN) {
return GraphQLString
}
//use default for everything else
return false
});
//map fields
attributeFields(Model);
/*
{
id: {
type: new GraphQLNonNull(GraphQLInt)
},
email: {
type: new GraphQLNonNull(GraphQLString)
},
isValid: {
type: new GraphQLNonNull(GraphQLString)
},
}
*/
attributeFields accepts a map
option to customize the way the attribute fields are named. The map
option accepts
an object or a function that returns a string.
var Model = sequelize.define('User', {
email: {
type: Sequelize.STRING,
allowNull: false
},
firstName: {
type: Sequelize.STRING
},
lastName: {
type: Sequelize.STRING
}
});
attributeFields(Model, {
map:{
email:"Email",
firstName:"FirstName",
lastName:"LastName"
}
});
/*
{
id: {
type: new GraphQLNonNull(GraphQLInt)
},
Email: {
type: new GraphQLNonNull(GraphQLString)
},
FirstName: {
type: GraphQLString
},
LastName: {
type: GraphQLString
}
}
*/
attributeFields(Model, {
map:(k) => k.toLowerCase()
});
/*
{
id: {
type: new GraphQLNonNull(GraphQLInt)
},
email: {
type: new GraphQLNonNull(GraphQLString)
},
firstname: {
type: GraphQLString
},
lastname: {
type: GraphQLString
}
}
*/
GraphQL enum types only support ASCII alphanumeric characters and underscores.
If you have other characters, like a dash (-
) in your Sequelize enum types,
they will be converted to camelCase. For example: foo-bar
becomes fooBar
.
If you have Sequelize.VIRTUAL
attributes on your sequelize model, you need to explicitly set the return type and any field dependencies via new Sequelize.VIRTUAL(returnType, [dependencies ... ])
.
For example, fullName
here will not always return valid data when queried via GraphQL:
firstName: { type: Sequelize.STRING },
lastName: { type: Sequelize.STRING },
fullName: {
type: Sequelize.VIRTUAL,
get: function() { return `${this.firstName} ${this.lastName}`; },
},
To work properly fullName
needs to be more fully specified:
firstName: { type: Sequelize.STRING },
lastName: { type: Sequelize.STRING },
fullName: {
type: new Sequelize.VIRTUAL(Sequelize.STRING, ['firstName', 'lastName']),
get: function() { return `${this.firstName} ${this.lastName}`; },
},
defaultArgs(Model)
will return an object containing an arg with a key and type matching your models primary key and
the "where" argument for passing complex query operations described here
var Model = sequelize.define('User', {
});
defaultArgs(Model);
/*
{
id: {
type: new GraphQLNonNull(GraphQLInt)
}
}
*/
var Model = sequelize.define('Project', {
project_id: {
type: Sequelize.UUID
}
});
defaultArgs(Model);
/*
{
project_id: {
type: GraphQLString
},
where: {
type: JSONType
}
}
*/
defaultListArgs
will return an object like:
{
limit: {
type: GraphQLInt
},
order: {
type: GraphQLString
},
where: {
type: JSONType
}
}
Which when added to args will let the resolver automatically support limit and ordering in args for graphql queries.
Should be used with fields of type GraphQLList
.
import {defaultListArgs} from 'graphql-sequelize'
args: _.assign(defaultListArgs(), {
// ... additional args
})
FAQs
GraphQL & Relay for MySQL & Postgres via Sequelize
We found that graphql-sequelize-teselagen demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint now supports HTML linting with 48 new rules, expanding its language plugin system to cover more of the modern web development stack.
Security News
CISA is discontinuing official RSS support for KEV and cybersecurity alerts, shifting updates to email and social media, disrupting automation workflows.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.