GraphQL和MongoDB-获取;“创建数据”;从mongodb自动插入的数据中的字段

GraphQL和MongoDB-获取;“创建数据”;从mongodb自动插入的数据中的字段,mongodb,graphql,react-apollo,apollo-client,Mongodb,Graphql,React Apollo,Apollo Client,假设我有一个模型: const SomeSchema = new Schema( { name: String, }, {timestamps: true}, ); 这有时间戳,当我执行变异时,它会自动将createdAt和updatedAt插入mongoDB 在我的GraphQL类型中: const SomeType = new GraphQLObjectType({ name: "SomeType", fields: () => ({ _id: {

假设我有一个模型:

const SomeSchema = new Schema(
  {
    name: String,
  },
  {timestamps: true},
);
这有时间戳,当我执行变异时,它会自动将
createdAt
updatedAt
插入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 }
  })
});

您可以使用
CreatedAt
作为
GraphQLString
类型,并将其插入到
SomeType
字段中

试试这个:

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