带有@cypher 模式指令的自定义解析器不接受日期作为输入 - GRANDStack

Custom resolver with @cypher schema directive do not accept Date as an Input - GRANDStack

我正在尝试将自定义解析器添加到我的 grand stack 应用程序中。将 DateInput 传递给我的突变时出现错误。

这是我的架构:

type Registration @hasRole(roles: [admin]) {
  registrationId: ID!
  startDate: Date!
  endDate: Date
}
type Mutation {
CreateRegistration(startDate: Date!, endDate: Date): Registration
    @cypher(
      statement: """
      CREATE (registration: Registration {
                              registrationId: apoc.create.uuid(),
                              startDate: $startDate,
                              endDate: $endDate
                            })
      RETURN registration
      """
    )
}

这是我的变异,我在 GraphQL 操场上使用:

mutation CreateRegistration {
  CreateRegistration(
    startDate: { year: 2020, month: 3, day: 22 }
    endDate: { year: 2020, month: 4, day: 12 }
  ) {
    registrationId
    startDate {
      formatted
    }
  }
}

这是neo4j-graphql包自动生成的突变:

20:49:51 api | 2020-11-29T19:49:51.949Z neo4j-graphql-js CALL apoc.cypher.doIt("CREATE (registration: Registration {registrationId: apoc.create.uuid(), startDate: $startDate, endDate: $endDate})
20:49:51 api | RETURN registration", {startDate:$startDate, endDate:$endDate, first:$first, offset:$offset}) YIELD value
20:49:51 api |     WITH apoc.map.values(value, [keys(value)[0]])[0] AS `registration`
20:49:51 api |     RETURN `registration` { .registrationId ,startDate: { formatted: toString(`registration`.startDate) }} AS `registration`
20:49:51 api | 2020-11-29T19:49:51.949Z neo4j-graphql-js {
20:49:51 api |   "startDate": {
20:49:51 api |     "year": 2020,
20:49:51 api |     "month": 3,
20:49:51 api |     "day": 22
20:49:51 api |   },
20:49:51 api |   "endDate": {
20:49:51 api |     "year": 2020,
20:49:51 api |     "month": 4,
20:49:51 api |     "day": 12
20:49:51 api |   },
20:49:51 api |   "first": -1,
20:49:51 api |   "offset": 0
20:49:51 api | }

这是我返回的错误响应:

{
  "errors": [
    {
      "message": "Failed to invoke procedure `apoc.cypher.doIt`: Caused by: org.neo4j.exceptions.CypherTypeException: Property values can only be of primitive types or arrays thereof",

当我只使用不带@cypher 的自动生成的解析器时,它工作得很好。

我的日期对象的输入值似乎有问题。当我完全删除日期时,它也有效。

有没有人有什么建议,我做错了什么?

谢谢

问题是您将对象作为 startDateendDate 属性的值传递,这不受 neo4j 支持。

例如,您可以使用 date 函数将对象转换为合适的类型:

type Mutation {
CreateRegistration(startDate: Date!, endDate: Date): Registration
    @cypher(
      statement: """
      CREATE (registration: Registration {
                              registrationId: apoc.create.uuid(),
                              startDate: date($startDate),
                              endDate: date($endDate)
                            })
      RETURN registration
      """
    )
}

当我使用“格式化”版本时有效:

type Mutation {
CreateRegistration(startDate: Date!, endDate: Date): Registration
    @cypher(
      statement: """
      CREATE (registration: Registration {
                              registrationId: apoc.create.uuid(),
                              startDate: date($startDate.formatted),
                              endDate: date($endDate.formatted)
                            })
      RETURN registration
      """
    )
}

突变必须是:

mutation CreateRegistration {
  CreateRegistration(
    startDate: { formatted: "2020-3-22" }
    endDate: { formatted: "2020-6-22" }
  ) {
    registrationId
    startDate {
      formatted
    }
  }
}