You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Neo4j GraphQL执行create mutation时抛出Invalid input 'on'语法错误

问题描述
  • 搭建了支持CRUD操作的Neo4j GraphQL API,执行mutation操作时触发报错,无法自行定位原因
  • 参照Neo4j官方GraphQL入门文档的示例实现mutation可正常运行,替换为自定义示例后就会触发错误
完整报错信息
{
  "errors": [
    {
      "message": "Invalid input 'on': expected\n  \"*\"\n  \"]\"\n  \"{\"\n  \"|\"\n  a parameter (line 11, column 26 (offset: 360))\n\"MERGE (this0)-[:Activity on]->(this0_activityonDisease0_node)\"\n                          ^",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "createUsers"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "code": "Neo.ClientError.Statement.SyntaxError",
          "name": "Neo4jError",
          "stacktrace": [
            "Neo4jError: Invalid input 'on': expected",
            "  \"*\"",
            "  \"]\"",
            "  \"{\"",
            "  \"|\"",
            "  a parameter (line 11, column 26 (offset: 360))",
            "\"MERGE (this0)-[:Activity on]->(this0_activityonDisease0_node)\"",
            "                          ^",
            "",
            "    at captureStacktrace (C:\\Users\\DELL\\Documents\\GitHub\\graphql-testing\\node_modules\\neo4j-driver-core\\lib\\result.js:239:17)",
            "    at new Result (C:\\Users\\DELL\\Documents\\GitHub\\graphql-testing\\node_modules\\neo4j-driver-core\\lib\\result.js:59:23)",
            "    at newCompletedResult (C:\\Users\\DELL\\Documents\\GitHub\\graphql-testing\\node_modules\\neo4j-driver-core\\lib\\transaction.js:433:12)",
            "    at Object.run (C:\\Users\\DELL\\Documents\\GitHub\\graphql-testing\\node_modules\\neo4j-driver-core\\lib\\transaction.js:287:20)",
            "    at Transaction.run (C:\\Users\\DELL\\Documents\\GitHub\\graphql-testing\\node_modules\\neo4j-driver-core\\lib\\transaction.js:137:34)",
            "    at execute (C:\\Users\\DELL\\Documents\\GitHub\\graphql-testing\\node_modules\\@neo4j\\graphql\\dist\\utils\\execute.js:87:51)",
            "    at resolve (C:\\Users\\DELL\\Documents\\GitHub\\graphql-testing\\node_modules\\@neo4j\\graphql\\dist\\schema\\resolvers\\mutation\\create.js:34:57)",
            "    at processTicksAndRejections (internal/process/task_queues.js:95:5)"
          ]
        }
      }
    }
  ],
  "data": null
}
问题相关代码

GraphQL类型定义

const typeDefs = gql`
type Disease {
    name: String
    medication: String
    period: String
    userActivityon: User @relationship(type: "Activity on", direction: IN)
  }
  
  type User {
    name: String
    age: Int
    sex: String
    weight: Int
    smoking: Boolean
    drinking: Boolean
    nationality: String
    birth_type: String
    activityonDisease: Disease @relationship(type: "Activity on", direction: OUT)
  }
`;

执行的Mutation语句

mutation{
  createUsers(input: {
    name:"harsha"
    age:22
    sex:"male"
    activityonDisease:{create:{node:{
      name:"cold"
      medication:"months"
      }}}
  }) {
    users {
      name
      age
      activityonDisease {
        name
        medication
      }
    }
  }
}
问题根因

报错为Neo4j Cypher语法错误,从报错指向的自动生成Cypher语句MERGE (this0)-[:Activity on]->(this0_activityonDisease0_node)可直接定位问题:

  • Cypher语法规定,关系类型、节点标签名称如果包含空格、特殊字符,必须使用反引号(`)包裹才能被正确解析
  • 代码中@relationship注解定义的关系类型为Activity on,名称包含空格,当前使用的@neo4j/graphql版本生成Cypher时未自动给带空格的关系名添加反引号转义,导致解析器读到空格后的on时判定为非法输入,抛出语法错误。
修复方案

优先选择第一种方案,从根源避免转义问题:

  1. 将关系类型修改为不带空格的命名格式,比如ACTIVITY_ON、ActivityOn,同步修改两个类型中@relationship注解的type参数值即可。
  2. 如果必须保留带空格的关系名,升级@neo4j/graphql到支持自动转义特殊字符关系名的版本,或手动在关系名两侧加反引号,即写为type: "`Activity on`"。

内容的提问来源于stack exchange,提问作者daylightisminetocommand

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 22:06:23