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

420 views Asked by At

I`m trying to add custom resolvers to my grand stack app. There I get an error while passing DateInput to my mutation.

This is my schema:

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
      """
    )
}

This is my mutation, I use in the GraphQL playground:

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

this is the automated generated mutation by neo4j-graphql package:

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 | }

this is the errorresponse I get back:

{
  "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",

When I just use the autogenerated Resolver without @cypher, it works perfectly.

It looks like it is a problem with the input value for my date object. When I remove the date completely, it also works.

Does anybody have a suggestion, what I am doing wrong?

THX

2

There are 2 answers

0
cloudyguy On BEST ANSWER

It works when I use the "formatted" version:

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
      """
    )
}

where the mutation must be:

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

The problem is that you are passing objects as values for the startDate and endDate properties which is not supported by neo4j.

So for example you can use the date function to transform the object into a suitable type:

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
      """
    )
}