如何将 GraphQL 连接到 MySQL

Posted

技术标签:

【中文标题】如何将 GraphQL 连接到 MySQL【英文标题】:How to connect GraphQL to MySQL 【发布时间】:2017-08-11 17:38:24 【问题描述】:

我已经使用 express cli 建立了一个简单的 express 项目,并按照教程创建了db.jsschema.js 但此时我无论如何都无法调试此错误/查看任何部分graphiql 文档中的架构。


  "errors": [
    
      "message": "Schema must be an instance of GraphQLSchema. Also ensure that there are not multiple versions of GraphQL installed in your node_modules directory."
    
  ]

App.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var index = require('./routes/index');
var users = require('./routes/users');

var schema = require('./schema');
var graphqlHTTP = require('express-graphql');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded(extended: false));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', index);
app.use('/users', users);

app.use('/api', graphqlHTTP(schema, graphiql: true));

// catch 404 and forward to error handler
app.use(function(req, res, next) 
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
);

// error handler
app.use(function(err, req, res, next) 
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development'
        ? err
        : ;

    // render the error page
    res.status(err.status || 500);
    res.render('error');
);

module.exports = app;

db.js

var Sequelize = require('sequelize');
var mysql = require('mysql');
var _ = require('lodash');

var Conn = new Sequelize('DB', 'Username', 'password', 
    host: 'DB IP',
    dialect: 'mysql'
);

var Sectors = Conn.define('sectors', 
    name: 
        type: Sequelize.STRING,
        allowNull: false
    
);

Sectors.sync(force: true).then(function () 
  // Table created
  return Sectors.create(
    name: 'test'
  );
);

exports.default = Conn;

Schema.js

var 
    GraphQLSchema,
    GraphQLObjectType,
    GraphQLID,
    GraphQLString,
    GraphQLInt,
    GraphQLBoolean,
    GraphQLList,
    GraphQLNonNull
 = require('graphql');

var Db = require('./db');

var Sectors = new GraphQLObjectType(
  name: 'sectors',
  description: 'list of all the sectors',
  fields: () => 
    return 
      id: 
        type: GraphQLInt,
        resolve (sectors) 
          return sectors.id;
        
      ,
      name: 
        type: GraphQLString,
        resolve (sectors) 
          return sectors.name;
        
      
    ;
  
);

var Query = new GraphQLObjectType(
  name: 'Query',
  description: 'Root query object',
  fields: () => 
    return 
      sectors: 
        type: new GraphQLList(Sectors),
        args: 
          id: 
            type: GraphQLInt
          ,
          name: 
            type: GraphQLString
          
        ,
        resolve (root, args) 
          return Db.models.sectors.findAll( where: args );
        
      
    ;
  
);

var Schema = new GraphQLSchema(query: Query);
exports.default = Schema;

【问题讨论】:

你是否在 node_modules 中安装了多个版本的 graphql...虽然我认为 :) 如果你还没有清除 node_modules 并重新安装可能值得 您是否正确导出和导入 Schema? Node Module 没有工作,关于 Schema 我不知道我只是跟着 Lee Benson,除了我使用 vanilla Js 和 mysql。没有错误! 你认为你可能需要安装 graphql-sequelize 吗? 我只是用这个问题作为一个关于如何为 GraphQL 连接 MySQL 的单页教程!谢谢! 【参考方案1】:

啊哈,原因是出口错误

exports.default = Schema

喜欢这个

module.exports = Schema

【讨论】:

【参考方案2】:

您可能想到了export default Schema 之类的东西并出错了,但请改用module.exports = Schema。用“s”导出,我在开始时犯了同样的错误。 我建议更频繁地使用 console.log() 来调试你的 js 文件

【讨论】:

以上是关于如何将 GraphQL 连接到 MySQL的主要内容,如果未能解决你的问题,请参考以下文章

Amplify Flutter:如何连接到现有的 GraphQL 端点

如何在连接到 mLab 的 GraphQL 中为“更新”数据编写 Mutation

如何使用另一种编程语言连接到 apollo graphql-websocket?

将 Strapi 连接到 NextJS/Apollo 客户端 GraphQL - 未提供所需类型“XXX”的变量“$input”

用户连接到另一个用户作为朋友关系 - AWS GraphQL 转换

无法连接到 Graphql 订阅?