Skip to content

Query caching broken with some custom scalars and JIT #997

@lsanwick

Description

@lsanwick

I'm building a schema that has a number of custom scalars like DateTime that are automatically validated and parsed into a Luxon DateTime object when provided as arguments.

This works great, except it seems to interact poorly with the default request cache, where subsequent requests will just provide the string representation of the argument.

Here's a toy implementation that shows the issue:

scalar DateTime

type Query {
  testDateTime(time: DateTime!): String!
}
const parseDateTime = (astOrString) => DateTime.fromISO(dateTime, { zone: "utc" })

const resolvers = {
  DateTime: new GraphQLScalarType<LuxonDateTime, string>({
    name: "DateTime",
    description:
      "An ISO8601 formatted date time string (e.g. 2023-02-01T08:30:00Z), parsed into a Luxon DateTime",
    serialize(value) {
      if (value instanceof LuxonDateTime) {
        return value.toISO();
      }
      throw new Error("DateTime must be a Luxon DateTime");
    },
    parseValue(dateTime) {
      return DateTime.fromISO(dateTime, { zone: "utc" })
    },
    parseLiteral(ast) {
      if (ast.kind === Kind.STRING) {
        return DateTime.fromISO(ast.value, { zone: "utc" })  
      }
    },
    Query: {
      dateTimeMessage(_root, { time }) {
        return `Good day! It's ${time.toISODate()}`
      }
    }
})

Example query

query {
  testDateTime(date: "2023-05-16T21:30:00.000Z")
}

First response (correct)

{
  "data": {
    "testDateTime": "Good day! It is 2023-05-16"
  }
}

Second response (incorrect)

{
  "data": null,
  "errors": [
    {
      "message": "time.toISODate is not a function",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "testDateTime"
      ]
    }
  ]
}

Disabling the caching with cache: false on mercurius when registering the plugin fixes the issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions