微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 FaunaDB 中使用 UDF 循环输入数组?

如何解决如何在 FaunaDB 中使用 UDF 循环输入数组?

我有一个关于映射 ID 数组(输入数据)并将所有相关文档返回到这些 ID 的问题。我设置了一个 UDF 来检索单个 ID 的文档,并希望通过一些调整来实现这一目标。我似乎无法弄清楚如何映射 inputdata 并创建一个变量 (data:) 来存储新的文档数组。任何帮助表示赞赏。这是有效的单条目 UDF:

Query(
  Lambda(
    ["inputdata"],Let(
      {
        data: Map(
          Paginate(
            Match(
              Index("certificate_by_dealer"),Ref(Collection("Dealers"),Select("dealer",Var("inputdata")))
            )
          ),Lambda(["ref"],Get(Var("ref")))
        )
      },Select(["data"],Var("data"))
    )
  )
)

是否有一个简单的...或任何解决方案可以使 ID 数组作为输入数据工作?

调用函数为:

Call("user_dealers_all_certificates",{
  ids: [301393590798516736,301393590798516749]
}

不幸的是我没有得到任何结果。 (添加引号解决了问题)

这是实现建议的 UDF:

Query(
  Lambda(
    ["inputdata"],Let(
      { dataIds: Select("ids",Var("inputdata")) },Union(
        Map(
          Var("dataIds"),Lambda(
            ["id"],Select(
              ["data"],Paginate(
                Match(
                  Index("certificate_by_dealer"),Var("id"))
                )
              )
            )
          )
        )
      )
    )
  )
)

添加引号创建了正确的响应:

Call("user_dealers_all_certificates",{ids: ["302122229239382536","301394099049595400"]})

[
  Ref(Collection("Certificate"),"302122488174739977"),Ref(Collection("Certificate"),"302120872550859273")
]

但是 GraphQL 查询返回错误数据:

query {
  allUserDealersCertificate(data: {ids: ["302122229239382536","301394099049595400"]}){
   data {
_id
  }
}
}

回复

{
  "errors": [
    {
      "message": "Lambda expects an array with 1 elements. Array contains 4.","extensions": {
        "code": "invalid argument"
      }
    }
  ]
}

分页的 GraphQL 错误:架构中为真:

{
  "data": {
    "allUserDealersCertificate": [
      null,null
    ]
  },"errors": [
    {
      "message": "Cannot return null for non-nullable type (line 3,column 5):\n    _id\n    ^","path": [
        "allUserDealersCertificate","_id"
      ],"locations": [
        {
          "line": 3,"column": 5
        }
      ]
    },{
      "message": "Cannot return null for non-nullable type (line 3,1,"column": 5
        }
      ]
    }
  ]
}

解决方法

根据您提供的查询,我觉得有必要指出 Match 函数执行完全匹配。它不会(也不能)为您展开结构化数组。 Ref 函数也不能。

您需要在 Map 上调用 inputdata,并获取每个 ID 的结果。然后,您可以将这些结果Union 放在一个列表中。

我不知道您正在处理的数据的确切形状,所以这里有一个查询,它适用于仪表板中可用的预填充数据:

Let(
  {
    // the pre-populated data has 3 stores,with ids 301,302,and 303
    // here,we want the products in stores 301 and 302
    ids: [ 301,302 ]
  },// this is where we combine all of the results
  Union(
    Map(
      // here is where we loop over the list of ids
      Var("ids"),Lambda(
        // for each id,run this function's expression
        "id",Select(
          // each Paginate call returns a page of results with its own data
          // field,so we have to select those out
          "data",Paginate(
            Match(
              Index("products_by_store"),// here we compose a reference to a specific store,using the
              // Lambda function's current id
              Ref(Collection("stores"),Var("id"))
            )
          )
        )
      )
    )
  )
)

我使用 Let 模拟将数组传递到 UDF 主体的 Npte。运行此查询时,结果应为:

[
  ["avocados","Conventional Hass,4ct bag",3.99],["cilantro","Organic,1 bunch",1.49],["limes","Conventional,1 ct",0.35],16 oz bag",3.49],["cups","Translucent 9 Oz,100 ct",6.98],["pinata","Giant Taco Pinata",23.99],"Original Classic Donkey Pinata",24.99]
]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。