简体   繁体   English

GraphQL 查询响应返回空对象而不是对象数组

[英]GraphQL query response coming back a null object instead of an array of objects

I read through and followed Why does a GraphQL query return null?我通读并遵循了为什么 GraphQL 查询返回 null? , but I'm still getting an object of null fields where I should be getting an array of objects. ,但我仍然得到一个null字段的对象,我应该在那里得到一个对象数组。

This is my resolver.这是我的解析器。 My tests pass if I'm looking up a single _id .如果我查找单个_id我的测试就会通过。 If there is no _id I want it to return everything.如果没有_id我希望它返回所有内容。 The query and mongoDB are working good.查询和 mongoDB 运行良好。 I can console.log the response and it's exactly what I'm looking for, but once I return the response for GraphQL, GraphQL messes it up.我可以console.log response ,这正是我正在寻找的,但是一旦我返回 GraphQL 的response ,GraphQL 就会把它搞砸。

comment: ({_id}: {_id: string}) => {
        if (_id) {
            return Comment.findOne({_id}).exec();
        } else {
            return Comment.find({}).exec().then((response) => {
                console.log("response inside resolver: ", response); // <-- THIS HAS THE ARRAY OF OBJECTS!
                return response;
            });
        }
    }

So when it gets to my test, it returns a data object instead of an array and all the fields are null.因此,当它进入我的测试时,它返回一个数据对象而不是一个数组,并且所有字段都为空。 Here is my test:这是我的测试:

 it("should retrieve all records from database", (done) => {

        const query: string = `query {
            comment { _id author text }
          }`;

        request(app)
            .post("/comments")
            .send({query})
            .expect(200)
            .end((err, res) => {
                console.log("response inside test: ", res.body.data.comment); // <-- OBJECT WITH NULL FIELDS!
                if (err) { return done(err); }
                expect(res.body.data.comment).to.have.lengthOf(arrayOfNewElements.length);
                res.body.data.comment.sort((a: IComment, b: IComment) => parseInt(a._id, 10) - parseInt(b._id, 10));
                expect(res.body.data.comment).to.deep.equal(arrayOfNewElements);
                done();
            });
    });

The console.log output: console.log 输出:

在此处输入图片说明

What am I doing to mess GraphQL up between the returned promise in the resolver and my test?我在做什么来混淆解析器中返回的承诺和我的测试之间的 GraphQL?

NOTE: This is in TypeScript.注意:这是在 TypeScript 中。

Update: Solved更新:已解决

I put my answer below.我把我的答案放在下面。 I hope it helps somebody.我希望它可以帮助某人。

As @DanielRearden said in the comments, you can't return either an object or a list.正如@DanielRearden 在评论中所说,您不能返回对象或列表。 I changed my query to return an array:我改变了我的查询以返回一个数组:

    type Query {
        comment(_id: String): [Comment]
    }

And updated my resolver like so to get the test to pass:并像这样更新我的解析器以使测试通过:

    comment: ({_id}: {_id: string}) => {
        if (_id) {
            return Comment.findOne({_id}).exec().then((response) => {
                return [response];
            });
        } else {
            return Comment.find({}).exec().then((response) => {
                return response;
            });
        }
    }

Of course, I had to update a previous test to expect an array instead of a single object.当然,我必须更新之前的测试以期望使用数组而不是单个对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM