Example: To render a UI view, I need to fetch a list of friends, a list of posts, and a list of notifications. All 3 of these data types (friends, posts, notifications) have users associated with them. Instead of fetching users for the 3 data types 3 separate times, I need to flatten that into a single call. I end up with something like this:
var [posts, friends, notifs] = yield [
getPosts(),
getFriends(),
getNotifs()
]; // executes in parallel
var userIDs = new Set();
userIDs.addAll(posts.map(p => p.userID));
userIDs.addAll(friends.map(f => f.userID));
userIDs.addAll(notifs.map(n => n.userID));
var users = yield getUsers(userIDs.toArray());
return {posts, friends, notifs, users};
You can see where this gets cumbersome. I have to compose one of these for every kind UI view at the root level. On the one hand it's very explicit and the flow of data is very clear, but it also means relationships between data are dupicated in more than one place (often many places).Could GraphQL help with that scenario?
If I understand your post correctly, I think it could. GraphQL prefers to expose full objects, rather than IDs, which lets you query for data inside objects at the same time. So for your example above, you would only need one GraphQL query:
viewer() {
posts {
node {
author { id, name, favorite_color },
// any other post data you want
}
},
friends {
node {
id,
name,
favorite_color,
}
},
notifications {
node {
source { id, name, favorite_color },
// any other notification fields you want
}
},
}
The "id, name, favorite_color" is just a sample set of fields, you could replace it with whichever your components needed. Relay also has functionality to avoid duplicating the set of fields, which is especially useful when you want to add or remove a field from what a reusable component needs.If you like that kind of control flow, check out the library we wrote to do it: https://github.com/storehouse/engen
My concern is regarding the server-side implementation of the GraphQL endpoint. My understanding is that GraphQL endpoints couple tightly to a graph-oriented backend. Facebook already has TAO so it's a no-brainer, but how feasible do you think a normal SQL-based backend can adapt to efficiently process arbitrary GraphQL queries? Or would it be easier to switch to a graph-oriented database (e.g. neo4j) instead? The former option seems to be quite an engineering endeavor, while the latter is just too risky right now.
Having experimented with neo4j a year ago, I would concur that it hasn't been battle-tested for use as a high-availability database for web apps; it was used much more for offline analytics AFAIK. Thingdom did some REALLY cool things using neo4j from JS, and their ideas for modeling news feeds were really mindblowing, but I'm hesitant to put anything mission-critical on it just yet from both a technical-debt perspective and a speed/scalability perspective.
In our framework, it's mostly event based, so if some data changes, events are fired and then things happen based on subscriptions. Just wondering what advantages are offered here over that setup.
> This means that only the fields of an object that a component explicitly asks for will be accessible to that component, even if other fields are known and cached in the store (because another component requested them). We call this masking, and it makes it impossible for implicit data dependency bugs to exist latently in the system.
We haven't yet finalized what API we will provide when we open source Relay. You'll certainly get one object, called Relay, however ;)
Here's a photo from the presentation that shows an example: https://twitter.com/devonbl/status/560532680513556481.