GraphQL 和 MongoDB - 从 mongodb 自动插入的数据中获取 "createdAt" 字段

GraphQL and MongoDB - getting the "createdAt" field from the data that was automatically inserted from mongodb

假设我有模型:

const SomeSchema = new Schema(
  {
    name: String,
  },
  {timestamps: true},
);

这有时间戳,当我执行突变时,它会自动将 createdAtupdatedAt 插入 mongoDB。

在我的 GraphQL 类型中:

const SomeType = new GraphQLObjectType({
  name: "SomeType",
  fields: () => ({
    _id: { type: GraphQLID },
    name: { type: GraphQLString },
  })
});

我没有 createdAt 或 updatedAt 字段,因为 mongoDB 帮我处理了。

如果我使用 useQuery,如何获得 createdAt 值?

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 }
  })
});