We want to run the equivalent of the following, getting name, age and friends' name(s):
GET https://example.org/users/1
With GraphQL, we can use user.graphql
to get the following:
{
id: 'https://example.org/users/1',
name: 'Jane Doe',
friends: [
{ name: 'Sandy Mienza' }
]
}
With Falcor, I think it would work like this:
var model = new falcor.Model({
cache: {
peopleById: {
'https://example.org/users/1': {
id: 'https://example.org/users/1',
name: 'Jane Doe',
friends: [
{ $type: 'ref', value: ['peopleById', 'https://example.org/users/2'] }
]
},
'https://example.org/users/2': {
id: 'https://example.org/users/2',
name: 'Sandy Mienza',
age: 40,
friends: [
{ $type: 'ref', value: ['peopleById', 'https://example.org/users/1'] },
{ $type: 'ref', value: ['peopleById', 'https://example.org/users/3'] }
]
},
'https://example.org/users/3': {
id: 'https://example.org/users/3',
name: 'Mike Lee',
age: 40,
friends: [
{ $type: 'ref', value: ['peopleById', 'https://example.org/users/2'] }
]
}
},
users: [
{ $type: 'ref', value: ['peopleById', 'https://example.org/users/1'] },
{ $type: 'ref', value: ['peopleById', 'https://example.org/users/2'] },
{ $type: 'ref', value: ['peopleById', 'https://example.org/users/3'] }
]
}
});
// prints the following:
// {"json":{"users":{"0":{"name":"Jane Doe"}}}}
model.
get(['users', 0, ['name', 'age']]).
subscribe(pathValue => console.log(JSON.stringify(result)));
// prints the following:
// { path: [['users', 0, 'friends'], { from: 0, to: 10 }, 'name'], value: ['Sandy Mienza'] }
model.
get(['users', 0, 'friends'], { from: 0, to: 10 }, 'name').
toPathValues().
subscribe(pathValue => console.log(JSON.stringify(pathValue)));