【问题标题】:Rethinkdb and GraphQL?重新思考数据库和 GraphQL?
【发布时间】:2016-09-24 03:02:59
【问题描述】:

我在使用 rethinkdb 查询解析我的 graphQL 字段时遇到问题。我感觉这与 javascript 范围有关,但我几乎尝试了所有组合。这就是我目前所拥有的。

var connection = null;

r.connect({host: 'localhost', port: 28015 }, function(err, conn){
    if(err) throw err;
    connection = conn;
});

var queryType = new GraphQLObjectType({
  name: 'Query',
  fields: () => ({
    node: nodeField,
    viewer: {
      type: GraphQLString,
      resolve: () => ( r.db("test")
                          .table("authors")
                          .get("f433f6f6-f843-4636-a1d3-eb34b89aec67")
                          .getField("name")
      ),
    },
  }),
});

【问题讨论】:

  • resolve 函数应该在异步操作的情况下返回一个承诺。您还可以在解析函数中使用 async-await。
  • 对此有何解决方案?准备好使用 rethinkdb / graphql 走类似的道路。
  • 问题是db请求结束时缺少run(conn)函数。

标签: rethinkdb graphql graphql-js


【解决方案1】:

在我使用 Promises 解决之前,我一直在处理同样的问题。这是我的架构:

const db = require('../db');

const {
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLInt,
} = require('graphql');

const queryType = new GraphQLObjectType({
  name: 'RootQuery',
  fields:{
    usersCount: {
      type: GraphQLInt,
      resolve: () => {
        return db.getUsers().then((result) =>{
          return result.length
        });
      }
    }
  }
});

const mySchema = new GraphQLSchema({
    query: queryType
});

module.exports = mySchema;

db.js

var Promise = require("bluebird");

var r =require('rethinkdb');

const dbConfig = {
    host: 'localhost',
    port: 28015,
    db: 'test'
};

const connect = ()  => {
  return new Promise((resolve, reject) => {
    r.connect({
      host: dbConfig.host,
      port: dbConfig.port
    }).then((connection)=>{
      resolve(connection);
    }).catch((e) =>{
      reject(e);
    });
  })
};

const getUsers = () => {
  return new Promise( (resolve, reject ) => {
    connect().then( (connection) => {
      r.db('test').table('users').run(connection).then((cursor) => {
         return cursor.toArray()
       }).then( (result) => {
          resolve(result);
       }).then(() => {
           connection.close();
       }).catch((err) => {
          connection.close();
          reject(err);
       });
    }).catch((error) => {
      console.log("Connection error: ", error);
      reject(error);
    });
  });
}

exports.getUsers = getUsers;

我希望这会有所帮助。你可以在这里找到一个完整的工作示例:https://github.com/luiscript/graphql-rethinkdb-x

【讨论】:

    【解决方案2】:

    我认为这不是 GraphQL 的问题。您是否尝试在 RethinkDB 查询的末尾添加 .run()?没有run(),查询不会发送到数据库。

    【讨论】:

    • @helfer 它可能有助于添加答案,为什么需要将run() 添加到解析函数中。
    • run() 必须添加到 RethinkDB 查询的末尾,而不是解析函数。没有run(),查询只是组合,但不会发送到数据库。
    • 哎呀..我应该小心点。我实际上是指解析函数中的“查询结束时”。谢谢!
    • 并传递一个连接变量给它,对吧?问题是,我必须在每个解析函数中做r.connect({host: x, post: y}, (err, conn) => { // What I want to return }) 吗?似乎是多余的。我不能使连接变量成为全局变量,或者能够导出/导入它吗?顺便说一句,请原谅我根本不喜欢堆栈溢出的编辑窗口的格式
    猜你喜欢
    • 1970-01-01
    • 2015-12-15
    • 2017-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多