1

我开始使用带有 couchbase 的 graphQL,并且在运行突变时遇到了一个小问题,我正在做的是保存 2 个对象:

key: email@email.it
{
    type: 'credential',
    user_id: 'xxxx-xxxx-xxxx-xxxx',
    password: 'jknelkjf'
}

key: xxxx-xxxx-xxxx-xxxx
{
    type: 'user',
    name: 'aaaa',
    surname: 'bbbb'
}

这是我的突变功能:

export default {
  createUser: {
    type: User,
    args: {
      email: { type: GraphQLString },
      password: { type: GraphQLString },
      name: { type: GraphQLString },
      surname: { type: GraphQLString }      
    },
    resolve(source, args, req) {

      const _id = uuid();
      return new Promise((resolve, reject) => {

        bcrypt.hash(args.password, 10)
        .then(hash => req.db.insert({}, args.email, {
          password: hash,
          user_id: _id,
          type: "credential"
        }))
        .then(cred => req.db.insert({}, cred.user_id, {
          name: args.name,
          surname: args.surname,
          type: "user"
        }))
        .then(user => {
          return resolve(user);
        })
        .catch(err => {
          return reject(err);
        });

      });
    }
  }
}

这些是我的模式

export const UserCredential = new GraphQLObjectType({
  name: 'UserCredential',
  fields: () => ({
    user_id: { type: GraphQLString },
    type: { type: GraphQLString },
    id: { type: GraphQLString },
    password: { type: GraphQLString }
  })
});

export const User = new GraphQLObjectType({
  name: 'User',
  fields: () => ({
    name: { type: GraphQLString },
    surname: { type: GraphQLString },
    type: { type: GraphQLString },
    id: { type: GraphQLString },
    credential: {
      type: UserCredential,
      resolve: (root, args, req) => {
        const query = `SELECT META(credential).id,credential.* FROM {{bucket}} as credential WHERE credential.type="credential" AND credential.user_id=$1`;
        return new Promise((resolve, reject) => {
          req.db.runQuery(null, query, [root.id])
          .then((res) => {
            return resolve(res[0]);
          })
          .catch((err) => {
            return reject(err);
          })
        });
      }
    }
  })
});

问题是,当我运行突变时,结果是凭证字段为空的用户对象,但是如果我在突变解析中添加超时,我会得到具有凭证对象的用户......插入函数类似于这个:

db.insert(result => {
    return db.find(result.id)
})

你有什么建议吗?

4

0 回答 0