grafoojs/grafoo

Paths and objects

adjourn opened this issue · 3 comments

Could you tell me what's your reasoning behind paths map and (especially) objects map? Do you have some kind of vision and is it work in progress?

Why I ask? Because I doubt it's in its final state right now, I think I could literally remove all objectsMap logic from core and no value would be lost:

(note: Im not suggesting it should be removed, just theorizing and I might miss the point)

  • listeners are global anyway
  • path object already contains all the data
  • build-query-tree.js does nothing?

Objects are fed into listeners when write happens and also included to read result but you could just as well replace those with path.data(s) because all data is there.

objectsMap is very important! It's not a WIP at all, but a key piece on how the cache works. : )

It stores every query node gathered from an execution result by ID. So if two queries share a particular node with a given ID and a mutation is run affecting this node, both queries will be updated automatically.

Objects are fed into listeners when write happens...

Take a look at this snippet from @grafoo/bindings:

unbind = client.listen(nextObjects => {
  if (lockListenUpdate) return (lockListenUpdate = 0);

  objects = objects || {};

  for (let i in nextObjects) {
    // object has been inserted
    if (!(i in objects)) return performUpdate();

    for (let j in nextObjects[i]) {
      // object has been updated
      if (nextObjects[i][j] !== objects[i][j]) return performUpdate();
    }
  }

  for (let i in objects) {
    // object has been removed
    if (!(i in nextObjects)) return performUpdate();
  }
});

The objects variable is a fragment from objectsMap containing all nodes present in a given query tree. Comparing both we are able to determine whether we should perform an update or not.

Ah yes, didn't look at view binding packages. Thanks for clearing this up.

Will buildQueryTree have a purpose in the future?

buildQueryTree is also important. It's responsibility is to build query trees based on objectsMap.

Given a query like the one below:

{
  parent {
    id
    name
    children {
      id
      name
      parent {
        id
        name
      }
    }
  }
}

resulting in a query tree like so:

let queryTree = {
  parent: {
    id: 1,
    name: "John Doe",
    children: [
      {
        id: 2,
        name: "Susie Q.",
        parent: {
          id: 1,
          name: "John Doe"
        }
      }
    ]
  }
};

assuming objectsMap has the following shape

let objectsMap = {
  1: {
    id: 1,
    name: "Totally not John Doe"
  },
  2: {
    id: 2,
    name: "Susie Q."
  }
}

buildQueryTree traverses the query tree checking in every branch if it's ID matches with an objectsMap key. If so it's replaced.