gql 的 Apollo Payload 上的错误未知类型 "DateTime"

Error Unknown type "DateTime" on Apollo Payload of gql

我有很多问题,但这次我的问题是 GraphQL 及其有效负载,我的 schema.gqlresolvers.js 文件如下所示:

    # 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;

当我尝试将我的负载用于这样的查询时:

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

这是return我的一个错误:

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

如果我在我的代码中直接使用相同的参数,它工作正常。

有人知道这个错误的原因吗?

您在架构中的标量类型名称是 GraphQLDateTime,但在请求中您使用的是 DateTime
尝试将请求更改为:

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