0.9.0 • Published 4 years ago
graphql-adapter v0.9.0
graphql-adapter
graphql schema 自动生成器
安装
$ 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}`);
});
实现接口
query(查询)
- one(查找单个)
- list(查找列表)
- listPage(查找列表带总量)
- aggregation(聚合接口 min | max | sum | count)
mutation(异变)
- create(新建)
- remove(删除)
- update(更新)
subscription(监听)
- created(新建事件)
- removed(删除事件)
- updated(更新事件)
参考项目
0.9.0
4 years ago
0.8.5
4 years ago
0.8.3
4 years ago
0.8.1
4 years ago
0.8.0
4 years ago
0.7.1
4 years ago
0.7.0
4 years ago
0.6.1
4 years ago
0.6.0
4 years ago
0.5.3
4 years ago
0.5.2
4 years ago
0.5.1
4 years ago
0.4.5
4 years ago
0.4.4
4 years ago
0.5.0
4 years ago
0.3.10
4 years ago
0.4.1
4 years ago
0.4.0
4 years ago
0.4.3
4 years ago
0.4.2
4 years ago
0.3.8
4 years ago
0.3.7
4 years ago
0.3.6
4 years ago
0.3.5
4 years ago
0.3.4
4 years ago
0.3.2
4 years ago
0.3.1
4 years ago
0.3.3
4 years ago
0.3.0
4 years ago
0.2.11
4 years ago
0.2.10
4 years ago
0.2.9
4 years ago
0.2.8
4 years ago
0.2.7
4 years ago
0.2.5
4 years ago
0.2.4
4 years ago
0.2.3
4 years ago
0.2.2
4 years ago
0.2.1
4 years ago
0.2.0
4 years ago
0.1.4
4 years ago
0.1.3
4 years ago
0.1.2
4 years ago
0.1.0
4 years ago
0.1.1
4 years ago
0.0.7
4 years ago
0.0.5
4 years ago
0.0.4
4 years ago
0.0.3
4 years ago
0.0.2
4 years ago
0.0.1
4 years ago