graphql-sequelize
Installation
$ npm install --save graphql-sequelize
graphql-sequelize assumes you have graphql and sequelize installed.
Resolve helpers
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.
Features
- Automatically converts args to where if arg keys matches model attributes
- Automatically converts an arg named 'limit' to a sequelize limit
- Automatically converts an arg named 'order' to a sequelize order
- Only loads the attributes defined in the query (automatically adds primary key and foreign keys)
- Batching of nested associations (see dataloader-sequelize)
Relay & Connections
Examples
; let User = sequelize; let Task = sequelize; UserTasks = User; let taskType = name: 'Task' description: 'A task' fields: id: type: GraphQLInt description: 'The id of the task.' title: type: GraphQLString description: 'The title of the task.' ; let userType = name: 'User' description: 'A user' fields: id: type: GraphQLInt description: 'The id of the user.' name: type: GraphQLString description: 'The name of the user.' tasks: type: taskType resolve: ; let schema = query: name: 'RootQueryType' fields: user: type: userType // args will automatically be mapped to `where` args: id: description: 'id of the user' type: GraphQLInt resolve: ; let schema = query: 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: 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: ;
field helpers
field helpers help you automatically define a models attributes as fields for a GraphQL object type.
var Model = sequelize; ; ; /*{ id: { type: new GraphQLNonNull(GraphQLInt) }, email: { type: new GraphQLNonNull(GraphQLString) }, firstName: { type: GraphQLString }, lastName: { type: GraphQLString }}*/ userType = name: 'User' description: 'A user' fields: _;
Providing custom types
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; ;typeMapper; //map fields; /*{ id: { type: new GraphQLNonNull(GraphQLInt) }, email: { type: new GraphQLNonNull(GraphQLString) }, isValid: { type: new GraphQLNonNull(GraphQLString) },}*/
Renaming generated fields
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; ; /*{ id: { type: new GraphQLNonNull(GraphQLInt) }, Email: { type: new GraphQLNonNull(GraphQLString) }, FirstName: { type: GraphQLString }, LastName: { type: GraphQLString }}*/ ; /*{ id: { type: new GraphQLNonNull(GraphQLInt) }, email: { type: new GraphQLNonNull(GraphQLString) }, firstname: { type: GraphQLString }, lastname: { type: GraphQLString }}*/
ENUM attributes with non-alphanumeric characters
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
.
VIRTUAL attributes and GraphQL fields
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: SequelizeSTRING lastName: type: SequelizeSTRING fullName: type: SequelizeVIRTUAL { return ` `; }
To work properly fullName
needs to be more fully specified:
firstName: type: SequelizeSTRING lastName: type: SequelizeSTRING fullName: type: SequelizeSTRING 'firstName' 'lastName' { return ` `; }
args helpers
defaultArgs
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; ; /*{ id: { type: new GraphQLNonNull(GraphQLInt) }}*/ var Model = sequelize; ; /*{ project_id: { type: GraphQLString }, where: { type: JSONType }}*/
defaultListArgs
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
.
args: _