나는 돌연변이를 만드는 방법에 대한 GraphQL 문서를 주로 따르고 있습니다 . 이것은 GraphQL로 작업하는 첫날입니다. 댓글에 대한 엔드 포인트를 만들고 싶습니다.
Mocha 에서 400 개의 "Bad Request" 를 받고 supertest
있으며 GraphiQL을 실행하려고하면 "Query root type must be provided"라는 오류 배열에 메시지가 표시됩니다. ...하지만 내가 하고 포함 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
내가 확신 사물의 익스프레스 측에 아무런 문제가 없다 해요, 그래서 브라우저에서. (TypeScript는 잘 컴파일됩니다.) "Hello world!"를 반환하는 다른 GraphQL api 엔드 포인트도 있습니다. 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();
});
});
});
그것은 "나쁜 말한다 때문에 요청 "나는 처리하는 방식 query
과를 variables
시험에를 확실히 문제가 될 수 있습니다.
내 문제는 테스트의 쿼리 및 변수입니까 아니면 내 문제 buildSchema
입니까? ... 또는 둘 다입니까? 테스트를 통과하려면 어떻게 다르게 작성해야합니까?
내보내기에서 rootValue를 이렇게 주석 처리 한 후 GraphiQL에서 "쿼리 루트 유형을 제공해야합니다"라고 말 했으므로 차이가 없음을 알았습니다. (가) 것처럼입니다 rootValue
하지가이 경우에도.
export default graphqlHTTP({
graphiql: true,
// rootValue: root,
schema
});
나는이 추가되면 Query
과를 getComment()
언급 뭔가 당 이 Github의 문제 에서 오류 메시지 GraphiQL은 사라입니다. 불행히도 테스트는 여전히 400 개의 잘못된 요청을 제공 하므로 문제가 관련이없는 것처럼 보입니다.
GraphiQL로 엔드 포인트를 테스트했으며 다음과 같이 게시 할 때 예상 / 원하는대로 작동했습니다.
mutation {
createOrUpdateComment(input: {_id: "4", author: "Some Author", text: "some unique text"}) {
_id
author
text
}
}
나는 내 테스트 쿼리를 유사하게 만들기 위해 노력하고 있지만 아날로그가 아닙니다. 이것은 문제가 테스트 자체에 있음을 분명히하고 신체의 응답을 기록 할 때 다음을 얻습니다.
그래서 내 테스트의 쿼리 문자열에 문제가 있습니다.
아래 내 대답을 참조하십시오.
테스트에서 나의 주요 문제점은
이것은 내가 끝낸 테스트 파일입니다.
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();
});
});
});
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다