【问题标题】:graphql prisma nodejs postgresql how to add date created/update fild to graphql type?graphql prisma node js postgresql如何将创建日期/更新字段添加到graphql类型?
【发布时间】:2019-03-05 09:16:15
【问题描述】:
我正在使用 graphql + prisma(在本地 docker 中)、nodejs 和 postgresql。
如何让它添加一些字段,如created/edited date?
例如。我有这种类型:
type Post {
id: ID! @unique
title: String!
content: String!
published: Boolean! @default(value: "false")
author: User!
}
如何添加日期等字段。使其等于元素创建/更新的日期?
【问题讨论】:
标签:
node.js
postgresql
graphql
prisma
【解决方案1】:
在 datamodel.prisma 中进行此更改:
type Post {
id: ID! @unique
title: String!
content: String!
published: Boolean! @default(value: "false")
author: User!
updatedAt: DateTime!
createdAt: DateTime!
}
在 schema.graphql 中进行此更改:
updatedAt: String!
createdAt: String!
【解决方案2】:
默认情况下,您在 Prisma 中创建的每种类型都隐藏了 2 个字段,但它们始终在后台创建并保持最新:createdAt: DateTime! 和 updatedAt: DateTime!。要公开它们,只需将它们添加到您的类型并尝试查询它们,您应该会看到已经有数据。
另请注意,它们不能被删除,通过从您的架构中删除它们可以再次隐藏它们。
希望这会有所帮助。