cult-of-coders / grapher

Grapher: Meteor Collection Joins + Reactive GraphQL like queries
https://atmospherejs.com/cultofcoders/grapher
MIT License
275 stars 53 forks source link

Client-side data-fetching inconsistency when link field is inside a nested field #460

Open vparpoil opened 2 years ago

vparpoil commented 2 years ago

Hello, I just found what seems like a bug : on the client, data returned by two queries can differ depending in which order the queries are called. I just build a reproduction here : https://github.com/vparpoil/grapher-bug-client-data-fetching

I have two collections with links in between them defined as such :

Link.addLinks({
    reference: {
        type: "one",
        collection: Reference,
        field: "meta.refId",
        index: true
    }
})

Reference.addLinks({
    links: {
        type: "many",
        collection: Link,
        inversedBy: 'reference'
    }
})

Then these two queries :


firstQuery = createQuery('firstQuery', {
    Reference: {
        name: 1,
        links: {
            _id: 1,
            type: 1,
        }
    }
})

secondQuery = createQuery('secondQuery', {
    Reference: {
        name: 1,
        links: {
            meta: 1
        }
    }
})

And the following data :

let refId = Reference.insert({name: "ref1"})
Link.insert({meta: {refId: refId, name: "link1"}, type: "nested"})
Link.insert({meta: {refId: refId, name: "link2"}, type: "nested"})

When querying query1 then query2, here are the results, we expect to retrieve the meta.name field of Link items from query2, but we don't :

query1 [{"_id":"HLty9SuyvHKL8eT9x","meta":{},"type":"nested"},{"_id":"mHT9aCuyb9H2r4b2h","meta":{},"type":"nested"}]
query2 [{"meta":{"refId":"Q7p2gMFbm2vbteKY7"},"_id":"HLty9SuyvHKL8eT9x"},{"meta":{"refId":"Q7p2gMFbm2vbteKY7"},"_id":"mHT9aCuyb9H2r4b2h"}]

When querying query2 only, the name is there !

query2 [{"meta":{"refId":"Q7p2gMFbm2vbteKY7","name":"link1"},"_id":"HLty9SuyvHKL8eT9x"},{"meta":{"refId":"Q7p2gMFbm2vbteKY7","name":"link2"},"_id":"mHT9aCuyb9H2r4b2h"}]

This bug is creating inconsistent behavior when developpers use the package so I believe it should be fixed. I can work on a fix for this, but I would love some feedback first.

--

After more research, it seems like this bug is present only for nested data in a mongo Document, when two queries ask for separate fields of this nested object and these two queries are subscribed using .subscribe()