【问题标题】:Unknown Directive "unique" - Mongoose and GraphQL未知指令“唯一” - Mongoose 和 GraphQL
【发布时间】:2019-07-24 18:55:12
【问题描述】:

我是 graphql 的新手,并试图在 graphql 中添加一个独特的模式,但得到了这样的错误。
这是架构:

const mongoose = require('mongoose');
const { Schema } = mongoose;

const incidentSchema = new Schema({
  incidentNumber: {type: String, required: true, unique: true},
  releaseDate: {type: String, required: true}
});
module.exports = mongoose.model('Incident', incidentSchema);

在graphql突变中:

addIncident: {
      type: IncidentType,
      args: {
        incidentNumber: { type: GraphQLString },
        releaseDate: { type: GraphQLString }
      },
      resolve(parent, args) {
        let incident = new Incident({
          incidentNumber: args.incidentNumber,
          releaseDate: args.releaseDate
        });
        return incident.save();
      }
    }

不知道这个问题需要做什么

【问题讨论】:

  • 此错误似乎特定于 GraphQL,而不是 mongoose 或 MongoDB。你在哪里看到这个错误?它是否作为 GraphQL 响应中的错误数组的一部分返回?如果是,您发送的查询是什么?
  • 如果我从架构中删除唯一约束,那么一切正常,但我希望该字段是唯一的
  • 奇数。你在哪里看到错误?是否有堆栈跟踪?
  • 谢谢我已经解决了查询中有@unique的问题。但是你能帮我解决这个错误吗:
  • 消息:E11000 重复密钥错误收集:

标签: mongoose graphql


【解决方案1】:

当使用服务器不支持的指令进行查询时,通常会返回此错误消息。听起来您的查询包含 @unique 指令,但没有在服务器端定义这样的指令。您可以查看 API 的文档以了解支持哪些指令。也可以运行自省查询来获取该信息:

query GetDirectives {
  __schema {
    directives {
      name
      description
      locations
      args {
        name
        description
        defaultValue
        type {
          name
        }
      }
    }
  }
}

【讨论】:

    猜你喜欢
    • 2019-03-19
    • 2021-02-07
    • 2022-01-02
    • 2020-08-14
    • 2021-12-05
    • 1970-01-01
    • 2020-03-05
    • 2018-11-23
    • 1970-01-01
    相关资源
    最近更新 更多