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