在 Dgraph 过滤器中查询变量

Query variables in Dgraph filter

我试图在 @filter(ge(...)) 调用中使用一个变量(这是一个标量),但我 运行 出错了

给定以下查询

{
  ua(func: uid(0xfb7f7)) {
    uid
    start_ua {
      sua as index
    }
    recorded_in {
      actions @filter(ge(index, sua)){
        index
      }
    }
  }
}

我收到以下错误

{
  "errors": [
    {
      "code": "ErrorInvalidRequest",
      "message": "Some variables are defined but not used\nDefined:[sua]\nUsed:[]\n"
    }
  ],
  "data": null
}

现在,如果我从查询中删除 sua as ...@filter(...),一切正常。 我的 Dgraph 版本是 v1.0.13.

我尝试用 @filter(ge(index, val(sua))) 替换 @filter(ge(index, sua)) 但我仍然 运行 出错:

{
  "errors": [
    {
      "code": "ErrorInvalidRequest",
      "message": ": No value found for value variable \"sua\""
    }
  ],
  "data": null
}

我做错了什么?

以下是 Dgraph 文档对值变量的描述(强调):https://docs.dgraph.io/query-language/#value-variables

Value variables store scalar values. Value variables are a map from the UIDs of the enclosing block to the corresponding values.

It therefore only makes sense to use the values from a value variable in a context that matches the same UIDs - if used in a block matching different UIDs the value variable is undefined.

start_uarecorded_in 是不同的子图,这意味着在同一查询块中,一个中定义的变量在另一个中未定义。

您可以做的是使用多个查询块。可以跨块访问变量:

{
  block1(func: uid(0xfb7f7)) {
    uid
    start_ua (first: 1) {
      sua as index
    }

  }
  
  block2(func: uid(0xfb7f7)) {
    recorded_in {
      actions @filter(ge(index, val(sua))) {
        index
      }
    }
  }
}

我还在 start_ua 谓词中添加了 (first: 1),这样最多可以获取 1 个节点并将其存储在 sua 变量中。如果您的数据已经按照这种方式构建,那么就不需要了。

val(sua)获取变量sua.

的值