【发布时间】:2020-07-05 11:49:30
【问题描述】:
我创建了一个由 Sequelize 支持的 Apollo 服务器。
我也在用 TypeScript 编写我的应用程序。
这在 Sequelize 的主键(Int)概念与 GraphQL 的 ID(特殊字符串)概念之间产生了冲突。
具体来说,我有一个如下所示的 GQL 类型:
type Book {
id: ID!
Title: String!
}
还有一个 Typescript 界面,大致如下:
export interface Book extends Model {
id: number;
name: string;
}
在我的解析器中,我有以下内容:
book: async (parent, args, context) => {
const { models } = context;
const { id } = args;
const carrier = await models.Book.findByPk(id);
return carrier;
},
这会返回错误:
Types of property 'id' are incompatible.
Type 'number' is not assignable to type 'string'.
这让我想到了我的问题
有没有办法强制 ID 解析为 int 而不是字符串?我想避免重新映射 Book 中的所有字段只是为了将 ID 转换为字符串
【问题讨论】:
-
在graphql中使用Int类型而不是ID?
-
我认为这会导致信息丢失。
ID类型可以有程序化应用程序(比如在 Apollo 客户端缓存中),我不想丢失。
标签: typescript graphql sequelize.js