在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):mickhansen/graphql-sequelize开源软件地址(OpenSource Url):https://github.com/mickhansen/graphql-sequelize开源编程语言(OpenSource Language):JavaScript 99.9%开源软件介绍(OpenSource Introduction):graphql-sequelizeShould be used with dataloader-sequelize to avoid N+1 queries Installation
graphql-sequelize assumes you have graphql and sequelize installed. Resolve helpersimport { resolver } from "graphql-sequelize";
resolver(SequelizeModel[, options]); 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
Relay & ConnectionsOptionsThe resolver(SequelizeModel, {
// Whether or not this should return a list. Defaults to whether or not the
// field type is an instance of `GraphQLList`.
list: false,
// Whether or not relay connections should be handled. Defaults to `true`.
handleConnection: true,
/**
* Manipulate the query before it's sent to Sequelize.
* @param findOptions {object} - Options sent to Seqeulize model's find function
* @param args {object} - The arguments from the incoming GraphQL query
* @param context {object} - Resolver context, see more at GraphQL docs below.
* @returns findOptions or promise that resolves with findOptions
*/
before: (findOptions, args, context) => {
findOptions.where = { /* Custom where arguments */ };
return findOptions;
},
/**
* Manipulate the Sequelize find results before it's sent back to the requester.
* @param result {object|array} - Result of the query, object or array depending on list or not.
* @param args {object} - The arguments from the incoming GraphQL query
* @param context {object} - Resolver context, see more at GraphQL docs below.
* @returns result(s) or promise that resolves with result(s)
*/
after: (result, args, context) => {
result.sort(/* Custom sort function */);
return result;
},
/*
* Transfer fields from the graphql context to the options passed to model calls
* Inherits from global resolver.contextToOptions
*/
contextToOptions: {
a: 'a',
b: 'c'
}
});
resolver.contextToOptions = {}; /* Set contextToOptions globally */ The Examplesimport {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: {
// Field for retrieving a user by ID
user: {
type: userType,
// args will automatically be mapped to `where`
args: {
id: {
description: 'id of the user',
type: new GraphQLNonNull(GraphQLInt)
}
},
resolve: resolver(User)
},
// Field for searching for a user by name
userSearch: {
type: new GraphQLList(userType),
args: {
query: {
description: "Fuzzy-matched name of user",
type: new GraphQLNonNull(GraphQLString),
}
},
resolve: resolver(User, {
// Custom `where` clause that fuzzy-matches user's name and
// alphabetical sort by username
before: (findOptions, args) => {
findOptions.where = {
name: { "$like": `%${args.query}%` },
};
findOptions.order = [['name', 'ASC']];
return findOptions;
},
// Custom sort override for exact matches first
after: (results, args) => {
return results.sort((a, b) => {
if (a.name === args.query) {
return 1;
}
else if (b.name === args.query) {
return -1;
}
return 0;
});
}
})
}
}
})
});
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 helpersfield 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: Object.assign(attributeFields(Model), {
// ... extra fields
})
}); Providing custom types
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)
},
}
*/ Renaming generated fieldsattributeFields accepts a 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
}
}
*/ ENUM attributes with non-alphanumeric charactersGraphQL enum types only support ASCII alphanumeric characters, digits and underscores with leading non-digit.
If you have other characters, like a dash ( For example:
VIRTUAL attributes and GraphQL fieldsIf you have For example, firstName: { type: Sequelize.STRING },
lastName: { type: Sequelize.STRING },
fullName: {
type: Sequelize.VIRTUAL,
get: function() { return `${this.firstName} ${this.lastName}`; },
}, To work properly firstName: { type: Sequelize.STRING },
lastName: { type: Sequelize.STRING },
fullName: {
type: new Sequelize.VIRTUAL(Sequelize.STRING, ['firstName', 'lastName']),
get: function() { return `${this.firstName} ${this.lastName}`; },
}, args helpersdefaultArgs
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
}
}
*/ If you would like to pass "where" as a query variable - you should pass it as a JSON string and declare its type as SequelizeJSON:
defaultListArgs
{
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 import {defaultListArgs} from 'graphql-sequelize'
args: Object.assign(defaultListArgs(), {
// ... additional args
})
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论