mongodb - GraphQL 和 MongoDB - 从 mongodb 自动插入的数据中获取“createdAt”字段
问题描述
假设我有模型:
const SomeSchema = new Schema(
{
name: String,
},
{timestamps: true},
);
这有时间戳,当我执行突变时,它会自动插入到 mongoDB 中createdAt
。updatedAt
在我的 GraphQL 类型中:
const SomeType = new GraphQLObjectType({
name: "SomeType",
fields: () => ({
_id: { type: GraphQLID },
name: { type: GraphQLString },
})
});
我没有 createdAt 或 updatedAt 字段,因为 mongoDB 为我处理了它。
createdAt
如果我正在使用,我将如何获得价值useQuery
?
const GET_SOME = gql`
query($name: String) {
some(name: $name) {
_id
createdAt // how would I need to call this?
}
}
`;
我需要插入createdAt
字段SomeType
吗?
解决方案
您可以CreatedAt
用作GraphQLString
类型,并插入到SomeType
字段中。
尝试这个:
const SomeType = new GraphQLObjectType({
name: "SomeType",
fields: () => ({
_id: { type: GraphQLID },
name: { type: GraphQLString },
createdAt : { type : GraphQLString }
})
});
推荐阅读
- go - 将具有 uint8_t *num 的 C 结构转换为 Go
- python - SQLAlchemy GUID 自定义类型或 SQLAlchemy-Utils UUID 类型
- android - 尝试使用 repo-sync 同步沿袭 OS 存储库,获取错误,尝试强制同步
- c - 如果输入“q”,则在 C 中创建一个退出的函数
- java - 使用 AWS 签名的 RestAssured 身份验证
- javascript - 过渡密码散列算法
- azure - 使用其 Rest API 时,Microsoft Azure 媒体服务通道 ID 应如何格式化?
- android - 在关闭 android 应用程序之前放置执行代码的可靠位置是什么?
- ruby-on-rails - jquery_ujs 和/或 turbolinks 阻止 js 事件在表单提交按钮上发生
- python - Python中if语句中的三元运算符