简体   繁体   English

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

[英]MongoDB Stitch GraphQL Custom Mutation Resolver returning null

GraphQL is a newer feature for MongoDB Stitch, and I know it is in beta, so thank you for your help in advance. GraphQL 是 MongoDB Stitch 的新功能,我知道它处于测试阶段,所以提前感谢您的帮助。 I am excited about using GraphQL directly in Stitch so I am hoping that maybe I just overlooked something.我对直接在 Stitch 中使用 GraphQL 感到很兴奋,所以我希望我可能只是忽略了一些东西。

The documentation for the return Payload displays the use of bsonType, but when actually entering the JSON Schema for the payload type it asks for you to use "type" instead of "bsonType".返回有效负载的文档显示了 bsonType 的使用,但是当实际输入有效负载类型的 JSON 模式时,它要求您使用“type”而不是“bsonType”。 It still works using "bsonType" to me which is odd as long as at least one of the properties uses "type".只要至少一个属性使用“type”,它仍然可以使用“bsonType”对我来说很奇怪。

Below is the function:下面是 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}`))
}

Here is the Input Type in the customer resolver:这是客户解析器中的输入类型:

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

Below is the Payload Type:下面是有效载荷类型:

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

When entering the wrong "type" the error states:输入错误的“类型”时,错误状态为:

Expected valid values are:[array boolean integer number null object string]预期的有效值为:[数组 boolean integer 编号 null ZA8CFDE6331BD59EB2AC9666F8911C4B6]

When entering the wrong "bsonType" the error states:输入错误的“bsonType”时,错误状态:

Expected valid values are:[string object array objectId boolean bool null regex date timestamp int long decimal double number binData]预期的有效值为:[字符串 object 数组 objectId boolean bool null 正则表达式日期时间戳 int long decimal double number binData]

I've tried every combination I can think of including changing all "bsonType" to "type".我已经尝试了所有我能想到的组合,包括将所有“bsonType”更改为“type”。 I also tried changing the _id to a string when using "type" or objectId when "bsonType".我还尝试在使用“type”时将 _id 更改为字符串,或者在使用“bsonType”时将 objectId 更改为字符串。 No matter what combination I try when I use the mutation it does what it is supposed to and adds the note into the lead, but the return payload always displays null.无论我在使用突变时尝试哪种组合,它都会执行应有的操作并将注释添加到前导中,但返回的有效负载始终显示 null。 I need it to return the _id and note so that it will update the InMemoryCache in Apollo on the front end.我需要它返回 _id 并注意,以便它更新前端 Apollo 中的 InMemoryCache。

Hi Bernard – There is an unfortunate bug in the custom resolver form UI at the moment which doesn't allow you to only use bsonType in the input/payload types – we are working on addressing this.嗨 Bernard – 目前自定义解析器表单 UI 中存在一个不幸的错误,它不允许您仅在输入/有效负载类型中使用 bsonType – 我们正在努力解决这个问题。 In actually you should be able to use either type/bsonType or a mix of the two as long as they agree with your data.实际上,只要它们同意您的数据,您就应该能够使用 type/bsonType 或两者的混合。 I think that the payload type definition you want is likely:我认为您想要的有效负载类型定义很可能是:

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

If that doesn't work, it might be helpful to give us a sample of the data that you would like returned.如果这不起作用,向我们提供您希望返回的数据样本可能会有所帮助。

I noticed that you might be missing a return before your call to collection.findOneAndUpdate()我注意到您在调用collection.findOneAndUpdate()之前可能会错过return

I tried this function (similar to yours) and got GraphiQL to return values (with String for all the input and payload types)我试过这个 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