本篇內容介紹了“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, allowNull: false, primaryKey: true }, username: { type: DataTypes.STRING(16), allowNull: false, defaultValue: '' }, mobile: { type: DataTypes.STRING(16), allowNull: false, defaultValue: '' }, email: { type: DataTypes.STRING(32), allowNull: false, defaultValue: '' }, password: { type: DataTypes.STRING(32), allowNull: false, defaultValue: '' }, salt: { type: DataTypes.STRING(8), allowNull: false, defaultValue: '' }, updatedAt: { type: DataTypes.INTEGER(10).UNSIGNED, allowNull: false } }, { 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, allowNull: false, primaryKey: true }, username: { type: Sequelize.STRING(16), allowNull: false, defaultValue: '' }, mobile: { type: Sequelize.STRING(16), allowNull: false, defaultValue: '' }, email: { type: Sequelize.STRING(32), allowNull: false, defaultValue: '' }, password: { type: Sequelize.STRING(32), allowNull: false, defaultValue: '' }, salt: { type: Sequelize.STRING(8), allowNull: false, defaultValue: '' } }, { tableName: 'user', // 取消默認的時間戳, 否則會報 createdAt 不存在錯誤 timestamps: false }); 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', fields: attributeFields(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遷移”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。