添加与属性的关系会破坏其他查询
Adding a relationship with properties breaks other queries
我使用的是 GRAND stack starter,最近升级了我的包和 Neo4j 数据库,以匹配 GRAND 入门工具包中标识的当前版本。
这似乎打破了我最基本的查询。我正在创建一个相当简单的演示数据库,用于探索包含食谱。我有一个带有食谱的节点,其中包括名称、说明和时间。
这个节点也有关系,表明这是什么饭菜,有多难吃。您可以在下面看到:
这 return 适合我在前端或 Graphql 游乐场中使用以下查询进行的查询:
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")
然而,一旦我尝试创建具有属性的关系,它就无法使用这些确切的查询 return 任何结果。我与此查询建立关系:
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
}
在我升级之前,这些项目都运行良好。我有多个食谱共享我可以查询的成分,查询一个食谱会 return 根据需要的所有成分和数量。您可以在下图中看到数据库的先前迭代,我可以在 graphql 游乐场和我的前端确认 return:
所以我对可能发生的事情感到困惑,或者如果有一种新的方法我应该可以做这些事情。我可以回滚,但由于这是一个学习过程,我想使我的包和数据库保持最新状态以应对这些挑战。
在尝试查询与属性有关系的节点时,来自 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"
]
}
}
}
]
}
该错误是 Cypher 语法错误。在您的 GraphQL 架构定义中 Ingredient.quantity
上的 @cypher
指令中使用的查询不正确,它指的是尚未定义的变量 q
。由于 quantity
是 Contains
关系上的 属性,并且您的查询绑定 c
到该关系,因此您的 @cypher
指令查询应该是
quantity: String @cypher(statement:"MATCH (:Recipe)-[c:Contains]-(this) RETURN c.quantity")
此外,neo4j-graphql-js introduces better relationship type support so you don't have to use @cypher
directives to access relationship properties, instead you can just define a relationship type: https://grandstack.io/docs/neo4j-graphql-js.html#relationships-with-properties
的 v1.0.1
我使用的是 GRAND stack starter,最近升级了我的包和 Neo4j 数据库,以匹配 GRAND 入门工具包中标识的当前版本。
这似乎打破了我最基本的查询。我正在创建一个相当简单的演示数据库,用于探索包含食谱。我有一个带有食谱的节点,其中包括名称、说明和时间。
这个节点也有关系,表明这是什么饭菜,有多难吃。您可以在下面看到:
这 return 适合我在前端或 Graphql 游乐场中使用以下查询进行的查询:
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")
然而,一旦我尝试创建具有属性的关系,它就无法使用这些确切的查询 return 任何结果。我与此查询建立关系:
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
}
在我升级之前,这些项目都运行良好。我有多个食谱共享我可以查询的成分,查询一个食谱会 return 根据需要的所有成分和数量。您可以在下图中看到数据库的先前迭代,我可以在 graphql 游乐场和我的前端确认 return:
所以我对可能发生的事情感到困惑,或者如果有一种新的方法我应该可以做这些事情。我可以回滚,但由于这是一个学习过程,我想使我的包和数据库保持最新状态以应对这些挑战。
在尝试查询与属性有关系的节点时,来自 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"
]
}
}
}
]
}
该错误是 Cypher 语法错误。在您的 GraphQL 架构定义中 Ingredient.quantity
上的 @cypher
指令中使用的查询不正确,它指的是尚未定义的变量 q
。由于 quantity
是 Contains
关系上的 属性,并且您的查询绑定 c
到该关系,因此您的 @cypher
指令查询应该是
quantity: String @cypher(statement:"MATCH (:Recipe)-[c:Contains]-(this) RETURN c.quantity")
此外,neo4j-graphql-js introduces better relationship type support so you don't have to use @cypher
directives to access relationship properties, instead you can just define a relationship type: https://grandstack.io/docs/neo4j-graphql-js.html#relationships-with-properties