const ProfileImage = ({ user }) => <img src={user.avatarUrl} alt={user.name} />;
const query = gql`{
user(id: 3) {
name
avatarUrl
}
}`
export const ProfileImageWithData = graphql(query)(ProfileImage);
You can ask for data in the structure the UI expects and only get exactly that data, letting the GQL client handle batching network requests for needs distributed across the whole app, caching, UI updating, etc under the hood. Also does a great job of decoupling product and data management code.False, even if there is graph in its name it's not restricted to graph, and that what is great about it compared to things like RDF and SPARQL. Backend data can be stored in key/value store, document, files, RDBMS or whatever. And you will still be able to make it work. AFAIK you write a "translation" layer that interprets a pseudo-JSON file into your data.
AFAIK GraphQL is not good at querying recursive datastructure, it's better at "neighborhood" kind of query, so you can traverse Foreign Keys, but not "indefinitely".
> graphql allowed you to run complex operations in the backend without fetching each individual node on the client.
Yes.
> I clearly missed something?
IDK. For me, GraphQL basically it's an RPC interface particularly suited at "reading" data.
> And if you can also elaborate on why SPARQL and RDF were a major failure, and how GraphQL is different/better?
GraphQL is not tied to particular data layout. I don't know much of SPARQL and RDF actually and don't know why they failed. I am just guessing.
That said, GraphQL it "just" another query language, it's "just" another DSL targeted at querying datastructures. As something that was thought a long time, focused to solve a particular issue.
That isn't true. You can request data in any shape you want and could theoretically traverse through child nodes indefinitely.
We have many queries which go 5-6 levels deep.
Forgive me if this sounds hyperbolic, but unless you have unusual or strict requirements, building a new app in 2017 REST-first is most likely a terrible mistake.
Let me clear up some misconceptions I see in almost every HN comment thread on GraphQL:
1. GraphQL isn't more suited to Graph databases. Your data sources can be a mix of relational DB, NoSQL, key value, a REST API, or anything else.
2. n+1 has always been a solved problem in GraphQL thanks to DataLoader[1], a query batching utility which coalesces calls to your data sources from different parts of your app, specifically to avoid n+1.
3. It's unquestionably production-ready, battle-tested, has a real spec and official reference implementation, and it's probably the safest bet you can make at this moment in an industry as fickle as this.
With GraphQL you simply write your schema, define types and relationships, and you’re then able to request data in almost any shape you wish, with very little extra work. This is invaluable during development.
If you have a list of recent comments with author names, and later decide to show an avatar alongside the name, you don’t need to write any extra code on the server. You add an extra line to your query (or component’s fragment) on the client.
The same goes for any field, any relationship, no matter how complex the resulting shape. If you wanted a comment author’s follower’s comments’ likeCounts, you still don’t need to write another line of server code.
This makes it stupidly simple to rapidly prototype new features, try out new layouts, and means you can share a single API endpoint between mobile apps and desktop site without sending useless data to one or both.
There have been many occasions when we simply wouldn't have had the time to implement a feature correctly with REST, particularly when we might not even know what data we'll want until we begin developing the feature and get a feel for how it works.
It doesn't just save server dev time either. On the client side there are libraries like Apollo[2] and Relay which take care of fetching data, caching, normalization, and you should almost certainly use one (I recommend Apollo) unless you have a good reason not to. Writing fetch calls and managing your store manually is just going to be a huge waste of time.
And the spec is more than queries and mutations. It's subscriptions, live queries, and more [3]. Real-time data is a first-class citizen of GraphQL, and the two most popular front-end libraries have official implementations of subscriptions (with live queries in progress).
GraphQL is elegant, has a well-designed official spec, great DX and just plain makes sense. But it's really something you need to try out for yourself (preferably on a real project) to see just how great it is.
If you’re planning to build something new with REST, seriously, reconsider. There's a slightly higher upfront cost to using GraphQL (particularly if you're new to it), but once you settle into it you'll be glad you did.
Useful tools and resources:
- GraphiQL[4] - an incredibly useful tool for running queries on your GraphQL API
- Graph.cool[5] - BaaS for quickly prototyping a GraphQL API
- Apollo Launchpad[6] - Try out GraphQL server code in your browser
[1] https://github.com/facebook/dataloader
[2] https://github.com/apollographql/apollo-client
[3] https://dev-blog.apollodata.com/new-features-in-graphql-batc...
You see many "GraphQL is similar to x, and x failed" comments on here, but I'm not sure x ever came close to where GraphQL is at the moment.
Good ideas fail sometimes. E4X failed, but JSX and its variations are game-changers.
Forming data requests on the client/consumer side.
> JSX and its variations are game-changers.
don't be so sure
If there were GraphQL for Spotify's API and you wanted an artist's discography, you don't have to make multiple calls to endpoints and work through huge objects where 90-95% of the data is not needed.
For the front-end, it's a no-brainer. I've read that it is a bit more involved on the server-side but Apollo seems to be making great tooling for it.