apollo-datasource-firestore icon indicating copy to clipboard operation
apollo-datasource-firestore copied to clipboard

`orderDocs` removes documents from result if the document has an `id` key

Open guneemwelloeux opened this issue 1 year ago • 1 comments

It looks like the orderDocs function in cache.ts is removing (or not returning) documents when they contain the id property in the document itself. I haven't dug deeper into this, but it shouldn't be too difficult to find why this is happening.

Consider:

  • a dummy Firestore collection, and 2 documents inside: A: {id: "123", value: "hidden"} and B: {value: "notHidden"}.
  • a FirestoreDataSource for this collection: dataSource

Expected result: When calling findOneById, with either A or B, I expect A or B to be returned respectively.

Observed result: findOneById(A) is returning null, whereas findOneById(B) is returning the B document properly.

This can be tested by adding the following test:

  describe('findOneById', () => {
    it('Should find a doc by ID', async () => {
      // make sure we have extra users in the DB
      // also make sure they are created outside of the DataSource
      // as things will be cached on creation
      const dRefOne = await usersCollection.add({
       id: "something",
        email: '[email protected]'
      })
      const dSnapOne = await dRefOne.get()

      const foundOne = await userSource.findOneById(dRefOne.id)

      assert.deepStrictEqual(foundOne, { ...dSnapOne.data(), id: dRefOne.id, collection: dRefOne.parent.id, createdAt: dSnapOne.createTime, updatedAt: dSnapOne.updateTime })
    })

guneemwelloeux avatar Aug 11 '23 16:08 guneemwelloeux

This might not be actually accurate. I was using version 5.2.0, which did itself rely on dataloader 2.0.0, and this is what I observed.

However, after upgrading to the latest version (6.0.1 at this point), which upgraded to dataloader 2.2.2, it seems that the issue is not occurring anymore.

guneemwelloeux avatar Aug 11 '23 16:08 guneemwelloeux