Search code examples
falcorfalcor-router

Falcor - 'get' no longer emits references as leaf values


I've recently upgraded from Falcor 0.x to 1.1.0 (2.x will be the next step)

According to the Falcor migration documentation, when calling model.get references are not emitted as json anymore.

I'm however wondering what would be the best practice in order to manage references in a model.get.

Here is an example.

Having the following json graph:

{
    jsonGraph: {
        comment: {
            123: {
                owner: { $type: "ref", value: ["user", "abc"] },
                message: "Foo"
            }
        },
        user: {
            abc: {
                name: "John Doe"
                initials: "JD"
            }
        }
    }
}

Calling model.get will result to:

const json = await model.get(["comment", "123", ["owner", "message"]);

{
    owner: undefined, // 0.x was returning `["user", "abc"]`
    message: "John Doe"
}

However it's possible to get the owner only:

const json = await model.get(["comment", "123", "owner", ["name", "initials"]);

{
    name: "John Doe",
    initials: "JD"
}

What is the recommendation for handling references in model.get?

Should I manually get the owner (like the last example?) or should I have an ownerId instead of an owner reference in comment model?


Solution

  • model.get can take any number of pathSets (docs). So, break your first pathSet into two and pass as separate arguments:

    await model.get(
      ["comment", "123", "message"],
      ["comment", "123", "owner", ["name", "initials"]]
    );
    

    which should return

    {
      message: "John Doe"
      owner: {
        name: "John Doe",
        initials: "JD"
      }
    }
    

    The underlying constraint is that a single pathSet can only include multiple paths of the same depth. So multiple paths of different depth can only be represented by multiple pathSets.