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

Fetching link and linkStorage in the same query returns inconsistent results #399

Closed Floriferous closed 4 years ago

Floriferous commented 4 years ago

When you fetch data with a link, and the linkStorage, it doesn't always return the right data based on the order of your fragment:

const A = new Mongo.Collection('a');
const B = new Mongo.Collection('b');

A.addLinks({
  bs: {
    field: 'bLinks',
    collection: B,
    type: 'many',
    unique: true
  }
});
B.addLinks({
  a: {
    collection: A,
    inversedBy: 'bs'
  }
});

describe('fetching links and their storage', () => {
  beforeEach(() => {
    A.remove({});
    B.remove({});
  });

  it('works', () => {
    A.insert({ _id: 'a1' });

    B.insert({ _id: 'b1', name: 'yoo' });
    B.getLink('b1', 'a').add('a1');

    const a1 = A.createQuery({
      $filters: { _id: 'a1' },
      bs: { name: 1 },
      bLinks: 1
    }).fetchOne();
    const a2 = A.createQuery({
      $filters: { _id: 'a12' },
      bLinks: 1,
      bs: { name: 1 }
    }).fetchOne();

    expect(a1).to.deep.equal(a2); // Fails
  });
});

I expect both queries to return the same data.

vparpoil commented 4 years ago

I think this is the same as https://github.com/cult-of-coders/grapher/issues/386. I have tried to fix in a PR but it's not yet finalized because the proposed fix may not work in all cases. I need to sit down and dive into grapher more to produce a better fix

Floriferous commented 4 years ago

You’re right!