MongoDB,Express + GraphQL 服务器:使用新字段更新数据模型,但查询不获取新字段
MongoDB, Express + GraphQL server: Updated data model with new fields, but queries do not pick up new fields
我结合 Express + MongoDB 创建了一个 GraphQL 服务器。我从以下数据模型开始:
const AuthorSchema = new Schema({
name: { type: String, required: true },
age: { type: Number, required: true },
});
查询 + 变更有效,但我决定向数据模型添加更多字段:
const AuthorSchema = new Schema({
name: { type: String, required: true },
age: { type: Number, required: true },
bio: { type: String, required: true },
picture: { type: String, required: true }
});
我可以通过更改新字段来添加新作者,但由于某些原因,查询不会 return 新字段。
{
"errors": [
{
"message": "Cannot query field \"bio\" on type \"Author\".",
"locations": [
{
"line": 3,
"column": 5
}
]
}
]
}```
您的 GraphQL 类型与您的 Mongoose 模式不同。如果您向 AuthorSchema
模式添加一个字段,并且还想将其公开为您的 Author
类型的一个字段,那么您需要在您的 GraphQL 模式中显式定义该字段。
我结合 Express + MongoDB 创建了一个 GraphQL 服务器。我从以下数据模型开始:
const AuthorSchema = new Schema({
name: { type: String, required: true },
age: { type: Number, required: true },
});
查询 + 变更有效,但我决定向数据模型添加更多字段:
const AuthorSchema = new Schema({
name: { type: String, required: true },
age: { type: Number, required: true },
bio: { type: String, required: true },
picture: { type: String, required: true }
});
我可以通过更改新字段来添加新作者,但由于某些原因,查询不会 return 新字段。
{
"errors": [
{
"message": "Cannot query field \"bio\" on type \"Author\".",
"locations": [
{
"line": 3,
"column": 5
}
]
}
]
}```
您的 GraphQL 类型与您的 Mongoose 模式不同。如果您向 AuthorSchema
模式添加一个字段,并且还想将其公开为您的 Author
类型的一个字段,那么您需要在您的 GraphQL 模式中显式定义该字段。