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

addLinks is not working with Reducers #430

Closed koticmit closed 4 years ago

koticmit commented 4 years ago

I have a reducer that will return an object upon reducing. Now, I am trying to use the result of reducer object in addLinks as a "field" will this work ?? There happening something but not as expected.

Eg: REDUCER

Collection1.addReducers({
    activeversion: {
        body: {
            versions: 1
        },
        reduce(object) {
            const {versions} = object;
        let actVersion = versions.find(ver => ver.versionstatus === "active"); // {versionname: "version1", "executor": "userId"}
            return actVersion;
        }
    }
})

ADDLINKS

Collection1.addLinks({ 'actexecutor': { type: 'one', collection: Meteor.users, field: 'activeversion.executor' } })

QUERY EXECUTION

Collection1.createQuery({ $filters: {_id: "collecId"}, activeversion:{} }).fetchOne()  //This is giving me the right result
Eg: Result: 
{
_id:"collecId",
activeversion:{
versionname:"version1"
executor: "userId"
}
}

//But this is not working as expected but something different (executor field is missing in result)
Collection1.createQuery({ $filters: {_id: "collecId"}, activeversion:{}, actexecutor: { profile: 1 }}).fetchOne();
Eg: Result
{
_id:"collecId",
activeversion:{
versionname:"version1"
}
}

I don't know If I do wrong anywhere or this feature is not yet supported! or Is there any best way to get this work ?

vparpoil commented 4 years ago

I don't think this is supported yet. To use addLinks we use stored fields (not computed fields). A workaround for you would be to store the activeversion field in your Collection1 and then add links on this field. Using logic in hooks or in methods to update the activeversion field, this should allow you to do what you expect

koticmit commented 4 years ago

Okay! Thanks for your suggestion.

theodorDiaconu commented 4 years ago

What @vparpoil said. Thanks!