触发突变时的 GraphQLError 模式验证

GraphQLError Schema validation while triggering a mutation

我正在尝试 GraphQL,我似乎 运行 遇到了一个奇怪的错误。

这是我的突变

    const createNewTask = {
        name: "AddATask",
        description: "A mutation using which you can add a task to the todo list",
        type: taskType,
        args: {
            taskName: {
                type: new gql.GraphQLNonNull(gql.GraphQLString)
            },
            authorId: {
                type: new gql.GraphQLNonNull(gql.GraphQLString)
            }
        },
        async resolve(_, params) {
            try {
                const task = newTask(params.taskName);
                return await task.save();
            } catch (err) {
                throw new Error(err);
            }
        }
    };

任务类型定义如下

const taskType = new gql.GraphQLObjectType({
    name: "task",
    description: "GraphQL type for the Task object",
    fields: () => {
        return {
            id: {
                type: gql.GraphQLNonNull(gql.GraphQLID)
            },
            taskName: {
                type: gql.GraphQLNonNull(gql.GraphQLString)
            },
            taskDone: {
                type: gql.GraphQLNonNull(gql.GraphQLBoolean)
            },
            authorId: {
                type: gql.GraphQLNonNull(gql.GraphQLString)
            }
        }
    }
});

我正在尝试使用 graphiql playground 添​​加任务。

mutation {
  addTask(taskName: "Get something", authorId: "5cb8c2371ada735a84ec8403") {
    id
    taskName
    taskDone
    authorId
  }
}

当我进行此查询时,出现以下错误

"ValidationError: authorId: Path `authorId` is required."

但是,当我从变更代码中删除 authorId 字段并发送其中没有 authorId 的变更时,我收到此错误

"Unknown argument \"authorId\" on field \"addTask\" of type \"Mutation\"."

所以这证明authorId在请求中是可用的。我在 vscode 上进行了相同的调试,可以看到该值。我似乎无法弄清楚哪里出了问题。

我知道错误是什么了。该错误实际上是由我的 mongoose 模式引起的,而不是由 graphql 模式引起的。

const taskSchema = new Schema(
    {
        taskName: {
            type: String,
            required: true
        },
        taskDone: {
            type: Boolean,
            required: true
        },
        authorId: {
            type: mongoose.Types.ObjectId,
            required: true
        }
    },
    {
        collection: "tasks"
    }
);

但奇怪的是,最后的错误消息并没有表明这是 mongoose 模式验证失败。错误指出这是一个 graphql 错误,因此造成了混乱。希望对大家有帮助。