$ yarn add graphql-adapter // or npm i graphql-adapter --save
根据orm
(sequelize)框架自动解析框架model
的属性,添加基于model
crud的graphql接口
const http = require("http");
const {ApolloServer, PubSub} = require("apollo-server-express");
const express = require("express");
const {generateSchema} = require("../dist");
const {Sequelize, DataTypes} = require("sequelize");
const sequelize = new Sequelize({dialect: "mysql"});
const models = {
user: sequelize.define("user", {name: DataTypes.STRING})
};
const server = new ApolloServer({
schema: generateSchema(models, {pubSub: new PubSub()}),
});
const app = express();
server.applyMiddleware({app});
const httpServer = http.createServer(app);
server.installSubscriptionHandlers(httpServer);
const PORT = 8082;
httpServer.listen(PORT, () => {
console.log(`🚀 Server ready at http://localhost:${PORT}${server.graphqlPath}`);
console.log(`🚀 Subscriptions ready at ws://localhost:${PORT}${server.subscriptionsPath}`);
});
const http = require("http");
const {ApolloServer, PubSub} = require("apollo-server-express");
const express = require("express");
const {generateSchema} = require("../dist");
const {sequelize, models} = require("./db");
sequelize.sync();
const server = new ApolloServer({
schema: generateSchema(models, {
pubSub: new PubSub(),
handlerFindOptions: ((action, options, fields) => {
console.log("action", action, options, fields);
return options;
}),
handlerAggregateOptions: ((action, options) => {
console.log("action", action, options);
return options;
}),
filterSubscription: (response) => {
console.log("Subscription", response);
return true;
},
created: {
filter: (response) => {
console.log("created.filter", response);
return true;
}
},
configMap: {
User: {
created: {
filter: (response) => {
console.log("User.created.filter", response);
return true;
}
},
}
}
}),
tracing: true,
});
const app = express();
server.applyMiddleware({app});
const httpServer = http.createServer(app);
server.installSubscriptionHandlers(httpServer);
const PORT = 8082;
httpServer.listen(PORT, () => {
console.log(`🚀 Server ready at http://localhost:${PORT}${server.graphqlPath}`);
console.log(`🚀 Subscriptions ready at ws://localhost:${PORT}${server.subscriptionsPath}`);
});
- one(查找单个)
- list(查找列表)
- listPage(查找列表带总量)
- aggregation(聚合接口 min | max | sum | count)
- create(新建)
- remove(删除)
- update(更新)
- created(新建事件)
- removed(删除事件)
- updated(更新事件)