【问题标题】:Adding a relationship with properties breaks other queries添加与属性的关系会破坏其他查询
【发布时间】:2019-03-07 06:07:06
【问题描述】:

我使用的是 GRAND 堆栈启动器,最近升级了我的软件包和 Neo4j 数据库,以匹配 GRAND 启动器工具包中确定的当前版本。

这似乎打破了我最基本的查询。我正在为包含食谱的探索创建一个相当简单的演示数据库。我有一个带有配方的节点,其中包括名称、说明和时间。

这个节点也有关系,表明它是什么类型的食物以及它有多难。您可以在下面看到:

这与我在前端或 Graphql Playground 中的查询返回得很好,并带有以下查询:

query($searchQuery: String = "baked") {
  RecipesBySubstring(searchQuery: $searchQuery) {
    name
    time
    instructions
    ingredients {
      name
      quantity
    }
    mealtype {
      type
    }
    difficulty {
      value
    }
  }
}

我的 graphql-schema.js 文件中的实际定义如下所示:

  RecipesBySubstring(searchQuery: String): [Recipe] @cypher(statement:
    "MATCH (r:Recipe) WHERE toLower(r.name) CONTAINS toLower($searchQuery) OR toLower(r.time) CONTAINS toLower($searchQuery) RETURN r ORDER BY r.name ASC")

但是,一旦我尝试创建具有属性的关系,它就无法使用这些确切的查询返回任何结果。我用这个查询创建了一个关系:

MATCH (r:Recipe{name:"baked spaghetti"}),(i:Ingredient{name:"beef"})
CREATE (r)-[c:Contains{quantity:"1 pound"}]->(i)
RETURN r,i,c

它把它添加到数据库中就好了。

我还在我的 graphql-schema.js 中定义了我的成分如下:

type Ingredient {
  name: String!
  quantity: String @cypher(statement:"MATCH (:Recipe)-[c:Contains]-(this) RETURN q.quantity")
  recipe: Recipe
}

在我升级之前,这些项目都运行良好。我有多个食谱共享我可以查询的成分,查询食谱将根据需要返回所有成分和数量。您可以在下图中看到数据库的上一次迭代,我可以确认这确实在 graphql 操场和我的前端返回:

所以我有点难过可能发生的事情,或者如果有一种新的方式我应该做这些事情。我可以回滚,但由于这是一个学习过程,我希望让我的包和数据库保持最新,以应对这些挑战。

尝试查询与属性有关系的节点时,来自 GraphQL Playground 的完整错误如下:

{
  "data": {
    "RecipesBySubstring": null
  },
  "errors": [
    {
      "message": "Failed to invoke function `apoc.cypher.runFirstColumn`: Caused by: org.neo4j.cypher.internal.util.v3_4.SyntaxException: Variable `q` not defined (line 1, column 93 (offset: 92))",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "RecipesBySubstring"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "code": "Neo.ClientError.Procedure.ProcedureCallFailed",
          "name": "Neo4jError",
          "stacktrace": [
            "Neo4jError: Failed to invoke function `apoc.cypher.runFirstColumn`: Caused by: org.neo4j.cypher.internal.util.v3_4.SyntaxException: Variable `q` not defined (line 1, column 93 (offset: 92))",
            "",
            "    at captureStacktrace (E:\\Recipe Snap\\grand-stack-starter\\api\\node_modules\\neo4j-driver\\lib\\v1\\result.js:200:15)",
            "    at new Result (E:\\Recipe Snap\\grand-stack-starter\\api\\node_modules\\neo4j-driver\\lib\\v1\\result.js:73:19)",
            "    at Session._run (E:\\Recipe Snap\\grand-stack-starter\\api\\node_modules\\neo4j-driver\\lib\\v1\\session.js:116:14)",
            "    at Session.run (E:\\Recipe Snap\\grand-stack-starter\\api\\node_modules\\neo4j-driver\\lib\\v1\\session.js:95:19)",
            "    at _callee$ (E:\\Recipe Snap\\grand-stack-starter\\api\\node_modules\\neo4j-graphql-js\\dist\\index.js:80:28)",
            "    at tryCatch (E:\\Recipe Snap\\grand-stack-starter\\api\\node_modules\\regenerator-runtime\\runtime.js:62:40)",
            "    at Generator.invoke [as _invoke] (E:\\Recipe Snap\\grand-stack-starter\\api\\node_modules\\regenerator-runtime\\runtime.js:296:22)",
            "    at Generator.prototype.(anonymous function) [as next] (E:\\Recipe Snap\\grand-stack-starter\\api\\node_modules\\regenerator-runtime\\runtime.js:114:21)",
            "    at step (E:\\Recipe Snap\\grand-stack-starter\\api\\node_modules\\babel-runtime\\helpers\\asyncToGenerator.js:17:30)",
            "    at E:\\Recipe Snap\\grand-stack-starter\\api\\node_modules\\babel-runtime\\helpers\\asyncToGenerator.js:35:14"
          ]
        }
      }
    }
  ]
}

【问题讨论】:

  • 您在 grandstack 或 neo4j 日志中是否有任何具体的错误/错误消息?
  • 当我尝试使用前端创建关系时,我得到“无法调用函数apoc.cypher.runFirstColumn”让我再次启动数据库,看看我是否还有其他要分享的内容。
  • 我添加了有关我看到的完整错误堆栈的附加信息。
  • MATCH (:Recipe)-[c:Contains]-(this) RETURN q.quantity 我认为这是一个错字 - 应该有一个 c 而不是 q - 以及关于它的消息 - Variable "q" not defined...
  • 好吧,我的名为 Contains 的关系有一个名为数量的属性,这是我在这种情况下真正想要返回的。在更新之前,查询一个菜谱确实会返回一个配料以及与菜谱关系上的数量。但是当我今晚回到家时,我会将那个 graphql 查询修改为更简单的东西,看看这是否有问题。虽然最终我仍然需要获取关系属性值。

标签: graph neo4j cypher graphql grandstack


【解决方案1】:

错误是 Cypher 语法错误。在 GraphQL 模式定义中 Ingredient.quantity 上的 @cypher 指令中使用的查询不正确,它指的是尚未定义的变量 q。由于quantityContains 关系上的一个属性,并且您的查询将c 绑定到该关系,因此您的@cypher 指令查询应该是

quantity: String @cypher(statement:"MATCH (:Recipe)-[c:Contains]-(this) RETURN c.quantity")

此外,neo4j-graphql-js 的 v1.0.1 引入了更好的关系类型支持,因此您不必使用 @cypher 指令来访问关系属性,而是只需定义一个关系类型:https://grandstack.io/docs/neo4j-graphql-js.html#relationships-with-properties

【讨论】:

  • 这绝对符合 stdob 所说的。奇怪的是,我直到两天前才让它工作,但你和他都指出了这一点,这是有道理的。今晚我将重做这个并尝试一下。您关于 neo4j-graphql-js 库 1.0.1 更改的链接也很棒。从最初使用 GRAND 启动器开始,我一直在运行一个更旧的版本。感谢您提供信息。
猜你喜欢
  • 2021-08-05
  • 1970-01-01
  • 1970-01-01
  • 2019-01-28
  • 2016-02-18
  • 2013-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多