Search code examples
dategraphqlapollo-serverpayloadgql

Error Unknown type "DateTime" on Apollo Payload of gql


I have a lot of problem, but this time my problem is GraphQL and its payload, my schema.gql and resolvers.js files looks like these:

    # schema.gql
    scalar GraphQLDateTime
    scalar GraphQLDate
    scalar GraphQLTime
    
    type Query {
      magicQuery(
        idMagic: Int
        magicDate: GraphQLDateTime
      ): DamnWow
    }
    
    type DamnWow {
      _id: String
      magicDateResult: GraphQLDateTime
    }
    # resolvers.js
    
    const { GraphQLDate, GraphQLTime, GraphQLDateTime } = require('graphql-iso-date');
    
    const resolvers = {
      Query: {
        magicQuery: async (root, { idMagic, magicDate }, context) => {
          // It's never enter here
          console.warn(idMagic, magicDate);
          const result = { _id: 'wow1', magicDate: new Date() };
          return result;
        }
      },
    
      GraphQLDateTime: GraphQLDateTime,
      GraphQLDate: GraphQLDate,
      GraphQLTime: GraphQLTime
    };
    
    module.exports = resolvers;

When I try to use my payload with a query like that:

    # query
    query magicQuery($idMagic: Int, $magicDate: DateTime) {
      magicQuery(idMagic: $idMagic, magicDate: $magicDate) {
        _id
        magicDateResult
      }
    }
    
    # variables
    # {
    #   "idMagic": 1,
    #   "magicDate": "2007-12-03T10:15:30Z"
    # }

it's return me an error:

    {
      "error": {
        "errors": [
          {
            "message": "Unknown type \"DateTime\".",
            "locations": [
              {
                "line": 1,
                "column": 45
              }
            ],
            "extensions": {
              "code": "GRAPHQL_VALIDATION_FAILED",
              "exception": {
                "stacktrace": [
                  "GraphQLError: Unknown type \"DateTime\".",
                  "    at Object.NamedType (/home/alex/workspace/team/node_modules/graphql/validation/rules/KnownTypeNamesRule.js:57:29)",
                  "    at Object.enter (/home/alex/workspace/team/node_modules/graphql/language/visitor.js:323:29)",
                  "    at Object.enter (/home/alex/workspace/team/node_modules/graphql/utilities/TypeInfo.js:370:25)",
                  "    at visit (/home/alex/workspace/team/node_modules/graphql/language/visitor.js:243:26)",
                  "    at Object.validate (/home/alex/workspace/team/node_modules/graphql/validation/validate.js:69:24)",
                  "    at validate (/home/alex/workspace/team/node_modules/apollo-server-core/dist/requestPipeline.js:221:34)",
                  "    at Object.<anonymous> (/home/alex/workspace/team/node_modules/apollo-server-core/dist/requestPipeline.js:118:42)",
                  "    at Generator.next (<anonymous>)",
                  "    at fulfilled (/home/alex/workspace/team/node_modules/apollo-server-core/dist/requestPipeline.js:5:58)"
                ]
              }
            }
          }
        ]
      }
    }

If I use same parameter directy in my code it works fine.

Does someone know the cause of this error?


Solution

  • Your scalar type name in the schema is GraphQLDateTime but in the request you are using DateTime.
    Try changing the request to:

    query magicQuery($idMagic: Int, $magicDate: GraphQLDateTime) {
      magicQuery(idMagic: $idMagic, magicDate: $magicDate) {
        _id
        magicDateResult
      }
    }