Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to migrate MySQL to GraphQL

Shulou Source: shulou.com Published: 2022-06-02 05:22:11 10月01日 Update

This article introduces the relevant knowledge of "how to migrate MySQL to GraphQL". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

GraphQL is an open source graphics database (based on Node.js implementation), Chinese document: https://graphql.js.cool/

Sequelize-auto transforms MySQL database into model

[node] sequelize-auto-h-d-u-x [password]-p [port]-- dialect [dialect]-c [/ path/to/config]-o [/ path/to/models]-t [tableName]-C Parameter:-h,-host host address [must]-d,-- database data name [must]-u,-- user username-x,-- pass password-p -- port port number-- c,-- config configuration file, reference: https://sequelize.readthedocs.org/en/latest/api/sequelize/-o,-- output output directory-- e,-- dialect database engine: postgres, mysql, sqlite-t,-- tables to be imported by tables-- tables to be excluded by skip-tables-- C,-- camel using hump nomenclature-n -- no-write does not need to write to file-s,-- schema database structure

Use the data model

Here is a sample model that is generated:

/ * jshint indent: 2 * / module.exports = function (sequelize, DataTypes) {return sequelize.define ('dashed username, {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'}) }

Create a database model:

Const Sequelize = require ('sequelize') Const Db = new Sequelize ('database name', 'user name', 'password', {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', / / cancel the default timestamp, otherwise createdAt will be reported without error timestamps: false}) Db.sync (); module.exports = {Db, User}

Graphql-sequelize transforms MySQL-> GraphQL structure

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

Start the server

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} `) This is the end of "how to migrate MySQL to GraphQL". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Data database model content password file more user user name knowledge structure output utility achievement next host parameters slogan dilemma graphics Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology OPPO Reno Shulou Tech Info Redmi Shulou Information