首页 > 解决方案 > 未知指令“唯一” - Mongoose 和 GraphQL

问题描述

我是 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();
      }
    }

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

标签: mongoosegraphql

解决方案


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

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

推荐阅读