我主要关注有关how to create a mutation的GraphQL文档。这是我使用GraphQL的第一天。我想为评论创建一个端点。
我从摩卡supertest
中回来了 400个“错误请求” ,当我尝试运行GraphiQL时,它在错误数组中显示了一条消息,内容为:必须提供查询根类型。” ...但是我 am 包括rootValue
。
问题可能出在buildSchema
上。
// imports (...working fine)
// Comment Interface for TypeScript is here ...and working
// ...
// gaphql schema (The problem could be here.)
const schema = buildSchema(`
input CommentInput {
_id: String
author: String
text: String
}
type Comment {
_id: String
author: String
text: String
}
type Mutation {
createOrUpdateComment(input: CommentInput): Comment
}
`);
// rootValue
const root = {
createOrUpdateComment: ({input}: { input: IComment }) => {
return {
_id: "id here",
author: input.author,
text: input.text
}
}
};
export default graphqlHTTP({
graphiql: true,
rootValue: root,
schema
});
此文件被导入,然后在快递服务器的顶层使用,就像comments
一样:
app.use("/comments", comments);
工作原理: GraphiQL 确实会在浏览器中的localhost:8080/comments
上弹出,所以我很确定Express方面没有任何问题。 (顺便说一下,TypeScript编译就很好。)我还有一个不同的GraphQL api端点,返回“ Hello world!”。在我正在使用Mocha测试的服务器的最高级别上,它通过了(最初失败后),因此我的测试套件似乎运行良好。没有其他环境变量。这就是您在这里看到的。
我要通过的测试(给我404错误请求)是:
// imports (...working fine)
describe("graphQl comments test", () => {
it("should add a comment", (done) => {
const query = `mutation createOrUpdateComment($input: CommentInput) {
_id
author
text
}`;
const variables = {
input: {
author: "Brian",
text: "This is my favorite video of all time."
}
};
request(app)
.post("/comments")
.send(JSON.stringify({ query, variables})) // (The problem could be here.)
.expect(200)
.end((err, res) => {
if (err) { return done(err); }
// tslint:disable-next-line: no-unused-expression
expect(res.body.data.id).to.exist;
expect(res.body.data.author).to.equal("Brian");
expect(res.body.data.text).to.equal("This is my favorite video of all time.");
done();
});
});
});
因为它说“错误的 request ”,所以我在测试中处理query
和variables
的方式肯定是问题所在。
我的问题是测试中的查询和变量还是我的buildSchema
? ...还是两者?我会怎样写才能使测试通过?
在注释掉导出中的rootValue之后,由于这样说,因为它说“必须提供查询根类型”,因此在 GraphiQL 中发现没有区别。好像rootValue
不在那里,即使它在那里。
export default graphqlHTTP({
graphiql: true,
// rootValue: root,
schema
});
一旦我为this Github issue中提到的内容添加了Query
和getComment()
, GraphiQL 中的错误消息就消失了。不幸的是,该测试仍在发出 400错误请求,因此问题似乎无关。
我用GraphiQL测试了端点,当我发布以下消息时,它按预期/期望的方式工作:
mutation {
createOrUpdateComment(input: {_id: "4", author: "Some Author", text: "some unique text"}) {
_id
author
text
}
}
我正在努力使测试查询相似,但它们不是类似的。这清楚表明问题出在测试本身中,当我记录身体的响应时,我得到以下信息:
所以我的测试的查询字符串有问题。
请在下面查看我的答案。
答案 0 :(得分:1)
GraphQL中有三个操作-查询,变异和订阅。每个操作都有一个与之关联的对象类型。这些称为根类型。默认情况下,这些根类型分别命名为Query
,Mutation
和Subscription
(这些名称无关紧要,可以为您的特定架构进行更改,但这是默认设置)。根类型用作您对该模式其余部分进行任何查询的入口点,即 root 。
规范要求需要查询根类型,而其他两个根类型是可选的,可以省略。
该错误与根值无关。实际上,您可能shouldn't be using buildSchema and rootValue in the first place。如错误所示,您尚未在架构中提供查询根类型。模式中唯一包含的根类型是Mutation
。您需要添加Query
才能解决该错误。
答案 1 :(得分:0)
我在测试中遇到的主要问题是
这是我最终得到的测试文件:
import { expect } from "chai";
import request = require("supertest");
import app from "../src";
describe("graphQl comments test", () => {
it("should add a comment", (done) => {
const query = `mutation {
createOrUpdateComment(input: {_id: "4", author: "Brian", text: "This is my favorite video of all time."}) {
_id
author
text
}
}`;
request(app)
.post("/comments")
.send({query})
.expect(200)
.end((err, res) => {
if (err) { return done(err); }
// tslint:disable-next-line: no-unused-expression
expect(res.body.data.createOrUpdateComment._id).to.exist;
expect(res.body.data.createOrUpdateComment.author).to.equal("Brian");
expect(res.body.data.createOrUpdateComment.text).to.equal("This is my favorite video of all time.");
done();
});
});
});