mysql - 如何为 MySQL 模型制作 GraphQL 模式?

标签 mysql node.js graphql

我在 mysql 中有下表。

用户

CREATE TABLE users(
    id INTEGER AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(255) UNIQUE NOT NULL,
    email VARCHAR(255) UNIQUE NOT NULL,
    password VARCHAR(100) NOT NULL,
    created_at TIMESTAMP DEFAULT NOW()
);

帖子

CREATE TABLE posts(
    id INTEGER AUTO_INCREMENT PRIMARY KEY,
    created_by INTEGER NOT NULL,
    created_at TIMESTAMP DEFAULT NOW(),
    post_title VARCHAR(100) NOT NULL UNIQUE,
    post_body VARCHAR(255) NOT NULL,
    slug VARCHAR(100) NOT NULL UNIQUE,
    FOREIGN KEY (created_by) REFERENCES users(id)
);

我正在使用 nodejs 和 mysql npm 包。如何为模型制作 graphQL 架构?

我搜索了很多,但未能找到任何解决方案。大多数人为此目的使用 sequelize。比mysql包好吗?

最佳答案

是的 您可以使用sequelize orm 将graphql 连接到Mysql 数据库

引用:- http://docs.sequelizejs.com/manual/installation/getting-started

给出了示例架构和解析器

架构.js

const typeDefinitions = `



type Author {

  authorId: Int
  firstName: String
  lastName: String
  posts: [Post]

}

type Post {

  postId: Int
  title: String 
  text: String
  views: Int
  author: Author

}

input postInput{
  title: String 
  text: String
  views: Int
}


type Query {

  author(firstName: String, lastName: String): [Author]
  posts(postId: Int, title: String, text: String, views: Int): [Post]

}



type Mutation {

createAuthor(firstName: String, lastName: String, posts:[postInput]): Author

updateAuthor(authorId: Int, firstName: String, lastName: String, posts:[postInput]): String

}


schema {
  query: Query
  mutation:Mutation
}
`;

export default [typeDefinitions];

连接器.js

import rp from 'request-promise';
var Sequelize = require('sequelize');
var db = new Sequelize('test', 'postgres', 'postgres', {
  host: '192.168.1.168',
  dialect: 'postgres',

  pool: {
    max: 5,
    min: 0,
    idle: 10000
  }

});


const AuthorModel = db.define('author', {
  authorId: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true, field: "author_id" },
  firstName: { type: Sequelize.STRING, field: "first_name" },
  lastName: { type: Sequelize.STRING, field: "last_name" },
},{
        freezeTableName: false,
        timestamps: false,
        underscored: false,
        tableName: "author"
    });


const PostModel = db.define('post', {
    postId: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true, field: "post_id" },
  text: { type: Sequelize.STRING },
  title:  { type: Sequelize.STRING },
  views: { type: Sequelize.INTEGER },
},{
        freezeTableName: false,
        timestamps: false,
        underscored: false,
        tableName: "post"
    });


AuthorModel.hasMany(PostModel, {
    foreignKey: 'author_id'
});
PostModel.belongsTo(AuthorModel, {
    foreignKey: 'author_id'
});

const Author = db.models.author;
const Post = db.models.post;

export { Author, Post };

解析器.js

import { Author } from './connectors';
import { Post } from './connectors';


const resolvers = {

  Query: {
    author(_, args) {
      return Author.findAll({ where: args });
    },
    posts(_, args) {
      return Post.findAll({ where: args });
    }
  },

  Mutation: {

    createAuthor(_, args) {
      console.log(args)
      return Author.create(args, {
        include: [{
          model: Post,
        }]
      });
    },

    updateAuthor(_, args) {

      var updateProfile = { title: "name here" };
      console.log(args.authorId)
      var filter = {
        where: {
          authorId: args.authorId
        },
        include: [
          { model: Post }
        ]
      };
      Author.findOne(filter).then(function (product) {
        Author.update(args, { where: { authorId: args.authorId } }).then(function (result) {
          product.posts[0].updateAttributes(args.posts[0]).then(function (result) {
            //return result;
          })
        });
      })
      return "updated";
    },

  },


  Author: {
    posts(author) {
      return author.getPosts();
    },
  },
  Post: {
    author(post) {
      return post.getAuthor();
    },
  },
};

export default resolvers;

关于mysql - 如何为 MySQL 模型制作 GraphQL 模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45721117/

相关文章:

reactjs - Relay + React 的测试策略

javascript - 如何在 gatsby 博客网站中验证 graphql

MySQL - 'Allow NULL' 的使用

php - 没有递归的PHP中的树算法

node.js - 在重定向之前设置 Express 响应 header

javascript - 如何检查函数是否是非阻塞的?

javascript - 为什么 Node.js 通常使用 'var' 而不是 'let' ?

php - 无法在我的实时环境中在 PHP 中显示来自 MySQL 的图像 blob,适用于开发

python - 从 MySQL 数据库 Python 填充下拉列表

reactjs - 卡在后期预览组件中