graphql-tools 的 makeExecutableSchema 因模式中的 AWSDateTime 而失败

makeExecutableSchema of graphql-tools failes with AWSDateTime in the schema

我正在使用 graphql-tools 来模拟来自 Appsync 的响应,但对于将 AWSDateTime 作为某些字段的数据类型的模式,它失败了。以下是我收到的错误:

Uncaught Error: Unknown type "AWSDateTime".

Unknown type "AWSDateTime".

Unknown type "AWSDateTime".

这是失败的代码:

import { SchemaLink } from "apollo-link-schema";
import { makeExecutableSchema, addMockFunctionsToSchema } from "graphql-tools";

const typeDefs = `
type Dates {
    createdAt: AWSDateTime
    updatedAt: AWSDateTime
}

type Query {
    getDates(id: ID!): Dates
}`;
const schema = makeExecutableSchema({ typeDefs });

知道如何解决这个问题吗?我知道 AWSDateTime 是专门为 appsync 定义的标量类型,所以它可能不起作用。但是有没有解决方法。使用 ApolloLink 客户端,它工作得很好。

您使用的每个标量(5 个内置标量除外)都必须在您的模式中明确定义。这是一个两步过程:

首先,添加类型定义:

scalar AWSDateTime

其次,提供一个GraphQLScalarType对象,它封装了标量的解析和序列化逻辑。对于 makeExecutableSchema,这是通过解析器映射提供的。

const resolvers = {
  ...
  AWSDateTime: new GraphQLScalarType({ ... }),
}

有关更多详细信息,请参阅 docs。如果序列化和解析逻辑并不重要,因为这只是为了模拟,那么您可以使用现有标量的方法,例如 String.

const resolvers = {
  ...
  AWSDateTime: new GraphQLScalarType({
    name: 'AWSDateTime',
    parseValue: GraphQLString.parseValue,
    parseLiteral: GraphQLString.parseLiteral,
    serialize: GraphQLString.serialize,
  }),
}