温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MySQL怎么向GraphQL迁移

发布时间:2021-08-19 09:32:15 阅读:154 作者:chen 栏目:大数据
亿速云mysql数据库,读写分离,安全稳定,弹性扩容,低至0.3元/天!! 点击查看>>

本篇内容介绍了“MySQL怎么向GraphQL迁移”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

GraphQL 是一个开源的图形数据库(基于Node.js实现), 中文文档: https://graphql.js.cool/

sequelize-auto 将 MySQL 数据库转变成模型

[node] sequelize-auto -h <host> -d <database> -u <user> -x [password] -p [port] --dialect [dialect] -c [/path/to/config] -o [/path/to/models] -t [tableName] -C 参数: -h, --host 主机地址 [必须] -d, --database 数据名 [必须] -u, --user 用户名 -x, --pass 密码 -p, --port 端口号 -c, --config 配置文件,参考: https://sequelize.readthedocs.org/en/latest/api/sequelize/ -o, --output 输出目录 -e, --dialect 数据库引擎: postgres, mysql, sqlite -t, --tables 需要导入的表 -T, --skip-tables 需要排除的表 -C, --camel 使用用驼峰命名法 -n, --no-write 不需要写入文件 -s, --schema 数据库结构

使用数据模型

这里是生成的一个示例模型:

/* jshint indent: 2 */  module.exports = function(sequelize, DataTypes{   return sequelize.define('d_user', {     uid: {       type: DataTypes.INTEGER(11).UNSIGNED,       allowNullfalse,       primaryKeytrue     },     username: {       type: DataTypes.STRING(16),       allowNullfalse,       defaultValue''     },     mobile: {       type: DataTypes.STRING(16),       allowNullfalse,       defaultValue''     },     email: {       type: DataTypes.STRING(32),       allowNullfalse,       defaultValue''     },     password: {       type: DataTypes.STRING(32),       allowNullfalse,       defaultValue''     },     salt: {       type: DataTypes.STRING(8),       allowNullfalse,       defaultValue''     },     updatedAt: {       type: DataTypes.INTEGER(10).UNSIGNED,       allowNullfalse     }   }, {     tableName'user'   }); };

创建数据库模型:

const Sequelize require('sequelize'); const Db new Sequelize('数据库名''用户名''密码', { host'localhost', dialect'mysql' }) const User = Db.define('user', { uid: { type: Sequelize.INTEGER(11).UNSIGNED, allowNullfalse, primaryKeytrue }, username: { type: Sequelize.STRING(16), allowNullfalse, defaultValue'' }, mobile: { type: Sequelize.STRING(16), allowNullfalse, defaultValue'' }, email: { type: Sequelize.STRING(32), allowNullfalse, defaultValue'' }, password: { type: Sequelize.STRING(32), allowNullfalse, defaultValue'' }, salt: { type: Sequelize.STRING(8), allowNullfalse, defaultValue'' } }, { tableName'user', // 取消默认的时间戳, 否则会报 createdAt 不存在错误 timestampsfalse }); Db.sync(); module.exports = { Db, User };

graphql-sequelize 转换 MySQL -> GraphQL 结构

const { GraphQLObjectType,GraphQLSchema,GraphQLList,GraphQLInt,GraphQLString } = require('graphql'); const { attributeFields, resolver } = require('graphql-sequelize'); const { Db, User } = require('./db');  userType = new GraphQLObjectType({   name'User',   description'A user',   fieldsattributeFields(User) });  const Query new GraphQLObjectType({   name'Query',   description'Root query object',   fields: () => {     return {       user: {         type: new GraphQLList(userType),         args: {           uid: {             type: GraphQLInt           },           email: {             type: GraphQLString           }         },         resolve(root, args) {           return Db.models.user.findAll({ where: args });         }       }     };   } });  const Schema new GraphQLSchema({   query: Query });  module.exports = Schema;

启动服务器

const Express =require'express'); const GraphHTTP =require'express-graphql'); const Schema =require'./schema');  // Config const APP_PORT = 3000;  // Start const app = Express();  // GraphQL app.use('/graphql'GraphHTTP({   schema: Schema,   pretty: true,   graphiql: true }));  app.listen(APP_PORT, ()=> {   console.log(`App listening on port ${APP_PORT}`);

“MySQL怎么向GraphQL迁移”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

亿速云「云数据库 MySQL」免部署即开即用,比自行安装部署数据库高出1倍以上的性能,双节点冗余防止单节点故障,数据自动定期备份随时恢复。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

原文链接:http://www.36dsj.com/archives/92829

AI

开发者交流群×