无法通过Graphql从Couchdb服务器获取数据

时间:2019-02-09 04:28:47

标签: express couchdb graphql graphql-js

我正在使用在Mac上的Docker Build中运行的Couchdb服务器。 要访问数据,我通过节点运行一个快速应用程序。 在为Graphql构建我的架构并使用Graphql接口对其进行测试之后,我能够从特定用户获取数据,但无法从我的用户解析器获取所有用户的文档。

这是我的代码:

//解析器

var resolvers = {
    users: () => {
        var statement = "SELECT META(user).id, user.* FROM `" + bucket._name + "` AS user WHERE user.type = 'user'";
        var query = Couchbase.N1qlQuery.fromString(statement);
        return new Promise((resolve, reject) => {
            bucket.query(query, (error, result) => {
                if(error) {
                    return reject(error);
                }
                resolve(result);
            });
        });
      },
    user: (data) => {
        var id = data.id;
        return new Promise((resolve, reject) => {
          bucket.get(id, (error, result) => {
            if (error) {
              return reject(error);
            }
            resolve(result.value);
          });
        });
      }
};

//表达设置

app.use("/graphql", ExpressGraphQL({
  schema: schema,
  rootValue: resolvers,
  graphiql: true
}));

app.listen(3000, () => {
  console.log("lisntening...")
});

//架构

var schema = BuildSchema(`
  type Query{
    user(id: String!): User,
    users: [User]
  }
  type User{
    id: String,
    profileImage: String,
    birthdate: String,
    reviews: [String],
    premium: Boolean
  }
`);

当我尝试这样查询时:

{
  users{
    id
  }
}

我收到以下错误消息味精:

{
  "errors": [
    {
      "message": "syntax error - at user",
      "locations": [
        {
          "line": 31,
          "column": 3
        }
      ],
      "path": [
        "users"
      ]
    }
  ],
  "data": {
    "users": null
  }
}

第二个解析器的特定用户查询正常工作!

这是我在数据库中的测试文档:

{
  "id": "ohuibjnklmönaio",
  "profileImage": "pic",
  "birthdate": "date",
  "reviews": [
    "a",
    "b"
  ],
  "premium": true,
  "type": "user"
}

0 个答案:

没有答案