1

我无法使用 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")
      ),
    },
  }),
});
4

2 回答 2

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;

数据库.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

于 2016-09-23T19:02:59.650 回答
0

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

于 2016-06-03T01:35:48.570 回答