【问题标题】:MongoDB, Express + GraphQL server: Updated data model with new fields, but queries do not pick up new fieldsMongoDB、Express + GraphQL 服务器:使用新字段更新数据模型,但查询不会获取新字段
【发布时间】:2020-06-21 13:24:39
【问题描述】:

我结合 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 }
});

我可以通过新字段的突变添加新作者,但由于某种原因,查询不会返回新字段。

{
  "errors": [
    {
      "message": "Cannot query field \"bio\" on type \"Author\".",
      "locations": [
        {
          "line": 3,
          "column": 5
        }
      ]
    }
  ]
}```

【问题讨论】:

    标签: mongodb express graphql mongoose-schema express-graphql


    【解决方案1】:

    您的 GraphQL 类型与您的 Mongoose 架构不同。如果您将一个字段添加到 AuthorSchema 架构并希望将此字段公开为您的 Author 类型上的字段,那么您需要在 GraphQL 架构中显式定义该字段。

    【讨论】:

      猜你喜欢
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-20
      • 2019-09-25
      • 1970-01-01
      • 2019-10-30
      相关资源
      最近更新 更多