繁体   English   中英

MongoDB Stitch GraphQL 自定义突变解析器返回 null

[英]MongoDB Stitch GraphQL Custom Mutation Resolver returning null

GraphQL 是 MongoDB Stitch 的新功能,我知道它处于测试阶段,所以提前感谢您的帮助。 我对直接在 Stitch 中使用 GraphQL 感到很兴奋,所以我希望我可能只是忽略了一些东西。

返回有效负载的文档显示了 bsonType 的使用,但是当实际输入有效负载类型的 JSON 模式时,它要求您使用“type”而不是“bsonType”。 只要至少一个属性使用“type”,它仍然可以使用“bsonType”对我来说很奇怪。

下面是 function:

      const mongodb = context.services.get("mongodb-atlas");
      const collection = mongodb.db("<database>").collection("<collection>");
      const query = { _id: BSON.ObjectId(input.id) }
      const update = {
        "$push": {
          "notes": {
            "createdBy": context.user.id, 
            "createdAt": new Date, 
            "text": input.text
          }
        }
      };

      const options = { returnNewDocument: true }

      collection.findOneAndUpdate(query, update, options).then(updatedDocument => {
        if(updatedDocument) {
      console.log(`Successfully updated document: ${updatedDocument}.`)
    } else {
      console.log("No document matches the provided query.")
    }
    return {
      _id: updatedDocument._id,
      notes: updatedDocument.notes
    }
  })
  .catch(err => console.error(`Failed to find and update document: ${err}`))
}

这是客户解析器中的输入类型:

  "type": "object",
  "title": "AddNoteToLeadInput",
  "required": [
    "id",
    "text"
  ],
  "properties": {
    "id": {
      "type": "string"
    },
    "text": {
      "type": "string"
    }
  }
}

下面是有效载荷类型:

{
  "type": "object",
  "title": "AddNoteToLeadPayload",
  "properties": {
    "_id": {
      "type": "objectId"
    },
    "notes": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "createdAt": {
            "type": "string"
          },
          "createdBy": {
            "type": "string"
          },
          "text": {
            "type": "string"
          }
        }
      }
    }
  }
}

输入错误的“类型”时,错误状态为:

预期的有效值为:[数组 boolean integer 编号 null ZA8CFDE6331BD59EB2AC9666F8911C4B6]

输入错误的“bsonType”时,错误状态:

预期的有效值为:[字符串 object 数组 objectId boolean bool null 正则表达式日期时间戳 int long decimal double number binData]

我已经尝试了所有我能想到的组合,包括将所有“bsonType”更改为“type”。 我还尝试在使用“type”时将 _id 更改为字符串,或者在使用“bsonType”时将 objectId 更改为字符串。 无论我在使用突变时尝试哪种组合,它都会执行应有的操作并将注释添加到前导中,但返回的有效负载始终显示 null。 我需要它返回 _id 并注意,以便它更新前端 Apollo 中的 InMemoryCache。

嗨 Bernard – 目前自定义解析器表单 UI 中存在一个不幸的错误,它不允许您仅在输入/有效负载类型中使用 bsonType – 我们正在努力解决这个问题。 实际上,只要它们同意您的数据,您就应该能够使用 type/bsonType 或两者的混合。 我认为您想要的有效负载类型定义很可能是:

{
  "type": "object",
  "title": "AddNoteToLeadPayload",
  "properties": {
    "_id": {
      "bsonType": "objectId"
    },
    "notes": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "createdAt": {
            "bsonType": "date"
          },
          "createdBy": {
            "type": "string"
          },
          "text": {
            "type": "string"
          }
        }
      }
    }
  }
}

如果这不起作用,向我们提供您希望返回的数据样本可能会有所帮助。

我注意到您在调用collection.findOneAndUpdate()之前可能会错过return

我试过这个 function (类似于你的)并让 GraphiQL 返回值(所有输入和有效负载类型都使用String

exports = function(input){

  const mongodb = context.services.get("mongodb-atlas");
      const collection = mongodb.db("todo").collection("dreams");
      const query = { _id: input.id }
      const update = {
        "$push": {
          "notes": {
            "createdBy": context.user.id, 
            "createdAt": "6/10/10/10",
            "text": input.text
          }
        }
      };

      const options = { returnNewDocument: true }

return collection.findOneAndUpdate(query, update, options).then(updatedDocument => {
        if(updatedDocument) {
      console.log(`Successfully updated document: ${updatedDocument}.`)
    } else {
      console.log("No document matches the provided query.")
    }
    return {
      _id: updatedDocument._id,
      notes: updatedDocument.notes
    }

  })
  .catch(err => console.error(`Failed to find and update document: ${err}`))
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM