【问题标题】:MongoDB Stitch GraphQL Custom Mutation Resolver returning nullMongoDB Stitch GraphQL 自定义突变解析器返回 null
【发布时间】:2020-09-21 08:58:45
【问题描述】:

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

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

下面是函数:

      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"
          }
        }
      }
    }
  }
}

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

预期的有效值为:[array boolean integer number null object string]

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

预期有效值为:[string object array objectId boolean bool null regex date timestamp int long decimal double number binData]

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

【问题讨论】:

    标签: mongodb graphql apollo-client mongodb-stitch


    【解决方案1】:

    我注意到您在致电collection.findOneAndUpdate() 之前可能会遗漏return

    我尝试了这个函数(类似于你的)并让 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}`))
    }
    

    【讨论】:

    • 谢谢!它现在返回正确的有效载荷。我很高兴这是我忽略的事情,而不是更大的问题。在逻辑上审查我的代码时,对我来说返回将在承诺完成后出现是有道理的,但现在肯定会记住第一次返回。同样,至少现在我对 bsonType 与类型的混淆已被 Drew 的回应消除了。现在我可以继续并创建许多自定义解析器!你们摇滚!
    【解决方案2】:

    您好 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"
              }
            }
          }
        }
      }
    }
    

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

    【讨论】:

    • 嗨,德鲁。谢谢您的答复。您在上面发布的类型定义对我来说看起来是正确的,我想我尝试了类似的东西。我只是重新测试了它,但仍然为空。 GraphiQL 对其进行测试:mutation AddNoteToLead($id: String!, $text: String!) { addNoteToLead(input: {id: $id, text: $text}) { _id } } 变量:{ "id": "5eca0bd8467c891c2865a779", "text": "Test note 1" } 响应:{ "data": { "addNoteToLead": null } } 我将在下面的后续评论中发布示例文档。
    • _id:ObjectId("5eca0bd8467c891c2865a779") locked:false name:"Company name" map:"null" website:"http://www.company.com/about-us" phone:"123-456-7890" stage:"Lead" timezone:"E" badAddressNoWebsite:false address:"123 State St." city:"TBD" state:"TBD" category:"Metals" notes:Array 0:Object createdBy:"5ed35b3898f98ad5a206b752" createdAt:2020-06-03T02:48:51.145+00:00 text:"Test note 1" 1:Object createdBy:"5ed35b3898f98ad5a206b752" createdAt:2020-06-03T02:48:56.309+00:00 text:"Test note 2"
    • 嗨 Bernard - 这似乎是一个单独的函数或类型验证错误导致解析器返回 null。使用上面的文档,我能够插入然后使用上面的输入/有效负载类型创建一个简单的自定义解析器并成功查询它。注意:我确实将日期更改为字符串并更改了函数以简化测试。如果您尝试从客户端应用程序调用,您可能会得到更详细的错误日志。
    • 我将日期更改为字符串,并且从客户端应用程序发送自定义突变只是返回了一个没有错误的空值。我还尝试了对功能的一些更改,但我不确定问题是否存在。我知道 returnNewDocument: true 正在做它应该做的事情,因为我控制台记录了返回值。我还认为 findOneandUpdate() 可能有问题,所以我尝试使用 updateOne() 并使用 findOne() 返回。运气不好后,我创建了一个新的测试集合、函数和只有 id 和 name 字段的 Mutation 解析器,并且测试返回的有效负载也为 null
    猜你喜欢
    • 2020-02-21
    • 2021-03-01
    • 2021-04-01
    • 2018-04-27
    • 2019-06-28
    • 2020-12-22
    • 2021-12-22
    • 2019-10-01
    • 2019-02-03
    相关资源
    最近更新 更多