Date Error with custom resolvers within @cypher directive - GRANDStack

Hey, I need some help. I have some trouble with the _Neo4JDateInput field.
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
      """
    )
}

mutation in graphql playground:

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

resolver:

CreateRegistration: (parent, args, context, info) => {
      // Fill in the custom logic
      return neo4jgraphql(parent, args, context, info)
    },

the autogenerated query is:

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

The problem is, as soon as I create the custom mutation, I got an error because of the Date type.
The error:

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

I tried to just use the autogenerated mutation CreateRegistration which worked perfectly. As soon as I add the @cypher directive, the Date Input stops working.

The problem is, I have to use custom Resolvers where I need to add a Date, example for User Registration.

Thx for your help.

You might look and see if Apoc has a specific way that it want's you to add a date. Maybe you need to cast it to a date like you'll find here. Neo4j: Convert string to date - Knowledge Base

1 Like

hi @shek I just ran into this problem myself . the issue is with the @cypher directive understanding graphql datetime
if you change your mutation to

   CREATE (registration: Registration {
                          registrationId: apoc.create.uuid(),
                          startDate: DateTime($startDate),
                          endDate: DateTime($endDate)
                        })
  RETURN registration

it should work
1 Like

Thank you for your suggestion.
Unfortunately it didn't work. When I change it to DateTime($startDate) or to Date($startDate) I get a new Error:

 "errors": [
    {
      "message": "Failed to invoke procedure `apoc.cypher.doIt`: 
Caused by: java.lang.IllegalArgumentException: 
year must be an integer value, but was a DoubleValue",

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 Like