New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

graphql-adapter

Package Overview
Dependencies
Maintainers
1
Versions
51
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-adapter

一个基于 sequelize ORM框架自动生成 graphql schema 的库

latest
Source
npmnpm
Version
0.9.0
Version published
Maintainers
1
Created
Source

graphql-adapter

graphql schema 自动生成器

安装

$ yarn add graphql-adapter // or npm i graphql-adapter --save

原理

根据orm(sequelize)框架自动解析框架model的属性,添加基于modelcrud的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)

image text

mutation(异变)

  • create(新建)
  • remove(删除)
  • update(更新)

image text

subscription(监听)

  • created(新建事件)
  • removed(删除事件)
  • updated(更新事件)

image text

参考项目

Keywords

graphql

FAQs

Package last updated on 24 Aug 2020

Did you know?

Socket

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.

Install

Related posts