egg.js连接graphql

一、安装egg-graphql  mysql2 egg-sequelize graphql dataloader

 "dataloader": "^2.0.0",
    "egg": "^2.15.1",
    "egg-graphql": "^2.8.0",
    "egg-scripts": "^2.11.0",
    "egg-sequelize": "^5.2.2",
    "egg-validate": "^2.0.2",
    "graphql": "^14.7.0",
    "lodash": "^4.17.19",
    "mysql2": "^2.1.0"
// config.default.js
const userConfig = {
        myAppName: 'egg-customizescreen',
        sequelize: {
            dialect: 'mysql',
            database: 'graphql', // 数据库名称
            host: 'localhost',
            port: '3306',
            username: 'root',
            password: 'root',
        },
        proxyworker: {
            port: 10086,
        },
        router: '/graphql',
        // 路径 建议命名为graphql
        middleware: ['graphql'],
        // graphQL 路由前的拦截器
        onPreGraphQL: function* (ctx) {},
        // 开发工具 graphiQL 路由前的拦截器,建议用于做权限操作(如只提供开发者使用)
        onPreGraphiQL: function* (ctx) {},
        security: {
            csrf: {
                ignore: () => true,
            },
        }
    };
// plugin.js
'use strict';

/** @type Egg.EggPlugin */
module.exports = {
    // had enabled by egg
    // static: {
    //   enable: true,
    // }
    // 数据库连接
    sequelize: {
        enable: true,
        package: 'egg-sequelize',
    },
    // graphql连接
    graphql: {
        enable: true,
        package: 'egg-graphql',
    },
    // 数据验证
    validate : {
        package: 'egg-validate',
    }
};

二、参考https://zhuanlan.zhihu.com/p/30604868

  • 1、在app目录下创建model、graphql目录

egg.js连接graphql_第1张图片

  • 2、model --》user.js
'use strict';

module.exports = (app) => {
    const {STRING} = app.Sequelize;

    const User = app.model.define('user', {
        name: STRING(30),
        password: STRING(32),
    });
    // User.create({
    //     name: 'alice123',
    //     password: '666666',
    //     created_at: new Date(),
    //     updated_at: new Date(),
    // });
    return User;
};
  • 3、query --》schema.graphql
type User {
  id: ID!
  name: String!
  password: String!
}

  • 4、graphql =》user =》connector.js
'use strict';
const DataLoader = require('dataloader');

class UserConnector {
    constructor(ctx) {
        this.ctx = ctx;
        this.loader = new DataLoader(this.fetch.bind(this));
    }

    /**
     * DataLoader缓存数据
     * @param ids
     * @returns {Promise.<*[]>}
     */
    fetch(ids) {
        const users = this.ctx.app.model.User.findAll({
            where: {
                id: ids,
            },
        });
        return users;
    }

    /**
     * 查询多个用户信息
     * @param ids
     * @returns {Promise.>|Promise>}
     */
    fetchByIds(ids) {
        return this.loader.loadMany(ids);
    }

    /**
     * 查询所有
     * @returns {*}
     */
    fetchList() {
        const users = this.ctx.app.model.User.findAll();
        return users;
    }

    /**
     * 查询单个
     * @param id
     * @returns {Promise | Promise.}
     */
    fetchById(id) {
        return this.loader.load(id);
    }
}

module.exports = UserConnector;

  • 5、graphql =》user =》resolver.js
'use strict';

module.exports = {
    Query: {
        // 查询单个用户
        user(root, {id}, ctx) {
            return ctx.connector.user.fetchById(id);
        },
        // 查询多个用户
        users(root, {id}, ctx) {
            return ctx.connector.user.fetchByIds(id);
        },
        // 查询所有用户
        userList(root, {id}, ctx) {
            return ctx.connector.user.fetchList();
        }
    },
};

  • 6、graphql =》user =》schema.graphql
type Query {
  user(id: ID!): User
  users(id: [ID!]): [User]
  userList: [User]
}

如果报这个错误:TypeError: DataLoader must be constructed with a function which accepts Array and returns Promise>, but the function did not return a Promise of an Array of the same length as the Array of keys.

 是因为dataloader的版本升级修改实现

将查询语句修改为

fetch(ids) {
        const users = this.ctx.app.model.User.findAll({
            where: {
                id: ids,
            },
        });
        return users;
    }

如果报错:Must provide Source. Received: undefined. at devAssert 请参考https://blog.csdn.net/ligaoming_123/article/details/107905216

  • 7、使用sequelize-cli建表

具体步骤请参考官网https://eggjs.org/zh-cn/tutorials/sequelize.html#%E5%88%9D%E5%A7%8B%E5%8C%96%E9%A1%B9%E7%9B%AE

请注意,建的数据表要比model加一个s复数形式

你可能感兴趣的:(GraphQl,Egg)