Apollo Server 1.0 – GraphQL Server for Node.js Frameworks
dev-blog.apollodata.com
dev-blog.apollodata.com
I'll have to dive into the code myself when I have some free time but can anyone explain to me what value this adds specifically?
I am assuming it does add value and I'm just not seeing it.
Comparison with express-graphql
- express-graphql works with Express and Connect, Apollo Server supports Express, Connect, Hapi, Koa and Restify.
- Compared to express-graphql, Apollo Server has a simpler interface and supports exactly one way of passing queries.
- Apollo Server separates serving GraphiQL (an in-browser IDE for exploring GraphQL) from responding to GraphQL requests.
- express-graphql contains code for parsing HTTP request bodies, Apollo Server leaves that to standard packages like body-parser.
- Apollo Server includes an OperationStore to easily manage whitelisting.
- Apollo Server is built with TypeScript.
The Apollo team member himself even said the wrapper is "thin."
The intention of this library isn't to change a lot about how GraphQL servers are written, it's just a stable, community focused library to bind your GraphQL server to a schema.
I'm not sure that answers my question though. I'd like to know which things it makes easier. Maybe a side by side with the "traditional way" vs "the Appolo way"
Looking at the website, it looks like it makes the benefit of Apollo client relatively clear, I'm still not sure about the server piece.
Rails/PHP/Django embody the MVC paradigm, where in reality it's just model and controller, and very little view.
React+Redux is bringing view to another level, and node is there just so you can render server-side the same things you render in the browser. When I saying bringing to another level, I don't just mean it's more feature-rich, I mean it's more maintainable, testable, and it's a paradigm designed to scale the team working on it by reducing complexity.
Back to the MVC comparison, Apollo+GraphQL bring the same concept of evolution to the API (controller maybe?). In the MVC world you learn how to build one API, not how to keep it working, maintain and upgrade it for years. GraphQL is a solution to that, Apollo (as said in other comments) is just a tiny layer.
Finally, the model in the MVC to me is just an academic abstraction, I've seen 0 large-scale deployments using joins and integrity constrains.
So yes, before it was MVC, with different implementations in different languages, now people are looking into JS because it's supported in the browser and moving towards more scalable architectures, both in terms of infrastructure and team.
Your comment is based on a grossly misleading presentation of the comparison though. React is a powerful view layer with an ecosystem around it. Rails is a full framework. You've misrepresented rails as simple and easy to learn by using only one term to describe it, and misrepresented the alternative to rails by separating it's components into named parts to scare us into thinking "Wow, 7 things? That's way more complicated than 2 things!" The reverse misrepresentation would be "So we now have react + postgre, but you're still using rails router + rails templates + rails graphql + rails rest + rails ORM + rails http client + jQuery + jQuery UI + gem + Ruby + macbookpro. How can you manage to train anyone on so many complicated tools?"
Why is this progress? Rails has it's limits in terms of full potential (complicsted backend + complicated UI + fast path to cross platform apps). You can add react to rails and reach the full potential. You can also do less than the full potential without react. React has some alternatives growing to also give you full potential, some not even in JS. It's 100% progress all across the board.
A large portion of apps aren't built with Node or React, they're built with, say, Rails, Java/Spring or .NET backends and a front end framework like Angular.
I get the theory behind React, the reality is that it is often a complete mess.
I've actually built a solution to this problem called Statebus: https://stateb.us
It takes a unique approach to the problem — instead of building more layers in the web stack, it builds support for change directly into HTTP itself! It turns out that if HTTP supports changing pages, and keeping things synchronized, you don't need web frameworks at all anymore.
Hard for me to see that as a problem with the tools rather than the developer.
There are plenty of 100% server-side jobs. Maybe you should be pointing those out to beginners instead of suggesting that they need to learn even niche tech like GraphQL before having a shot in the job market.
if it helps, Apollo has Redux built into it so you don't need a store. With this I reduced the size of my small app quite significantly by removing reducers, actions and the store files.
I've been using React + Apollo + GraphQL on the frontend and Graph.cool as a BaaS which has been great so far.... admittedly I'm a beginner (starting from my experience as a Java developer 7 years ago)
0) Do nothing
If you are just prototyping, you should do the simplest thing that could possibly work. (But this is not a real solution, so I won't count it)
2) Request Batching
This approach is popularised by Facebooks Dataloader library https://github.com/facebook/dataloader Instead of directly querying the database in the resolver, you return a specification of what data you are interested in. When all resolvers on the same level in the query has returned this specification, the Dataloader library figures out how to batch queries together as much as possible
3) Big Join
You can inspect the GraphQL query in the top level resolver and basically perform one giant join up front. You will then pass the entire value down your resolve hierarchy, and all they have to do is pick out the correct data from the complete result set. Take a look at join-monster if you are interested in this approach.
The Graphcool hosted backend is serving millions of requests an hour using the Dataloader approach, and we have found this to be more flexible and more performant at scale than generating a big complex join up front.
For most queries we see single digit millisecond response time for a batched query, and the time complexity scales linearly with the depth of the query, which is not the case for joins.
Primer on how to use it: https://blog.kensho.com/compiled-graphql-as-a-database-query...
Full disclosure: I wrote it, and open-sourced it just yesterday.
3) here's the code:
const EagerQL = (info, allowed) => { let fields = info.operation.selectionSet.selections[0].selectionSet.selections; return fields .map(field => field.name.value) .filter(field => allowed.includes(field)); };
// in a resolver user(_, args, context, info) { let eagers = EagerQL(info); console.log(eagers); // ['posts'] }
This will give you fields that were queried for so you can do a big join. Hope this helps someone :P
join-monster.readthedocs.io
Primer on how to use it: https://blog.kensho.com/compiled-graphql-as-a-database-query...
I work at Kensho and we use it with queries that are 10+ levels of nesting with no problems.
What you describe isn't a limitation at all-- some of our queries run 10 levels deep and work just fine. In fact, this method is so powerful that most of our queries start from the `user` object and just JOIN off of it.
I’m sure they’d welcome further feedback or pull requests...
If you use PostgreSQL however, i solve that problem :) https://subzero.cloud (GraphQL & REST api for your database) It's still in private beta but you can get a sense of how things are put together by looking a this OS project (just REST) https://github.com/subzerocloud/postgrest-starter-kit
Separate branches of the the graphql query are handed to independent goroutines. When batchable rpcs (to the database, or other services) occur, we delay execution by ~1ms and wait for any other rpcs of the same type (and keep delaying for up to ~20ms). This works pretty well for our more expensive field resolvers. We've also thought about ways to examine and track the execution of the goroutines (similar to the facebook/dataloader approach), but the time-based batches have worked pretty well for us so far.
We also aggressively cache queries and rpcs so that the same data is not expensive to fetch across different branches of a query.
[0] You can see the batching implementation here: https://github.com/samsarahq/thunder/blob/master/batch/batch...
Old timers that remember Enterprise JavaBeans 1.x entity beans are probably rolling in laughter currently. It had a very similar problem in 1999-2000 that doomed many systems to crappy performance and tarred EJB forever.
History never fails to repeat itself: beware subsystems that propose to wrap and abstract your database for a new paradigm "automatically". They're leaky and difficult and take a decade to get right. See: ActiveRecord, EJB followed by JPA, .NET's various DB frameworks, Django, etc.
[1] https://github.com/mwilliamson/node-graphjoiner
However, it's rather verbose with repeated boilerplate, so to make the approach work I think you'd want to put a higher-level layer on top -- that's what I've done with a Python implementation [2]. I think the general approach is the valuable part, rather than the specific libraries, and seems more in keeping with how I'd normally write queries than the usual DataLoader approach. In particular, I've found it tends to improve performance when fetching large data sets since you don't need to call a resolution function on every single field that gets returned. (DataLoader implementations batch queries together to avoid the n+1 problem, but they mean that you need to implement a batching layer, and probably make your resolution functions asynchronous, which is especially expensive for large data sets).
In fact you simply have functions called resolvers where you write your logic to get data from wherever you want. SQL, NoSQL, API, filesystem, etc.
GraphQL should just be proxy layer between actual client and the actual, multiple, APIs.
I've been using it while following along the backend JS tutorial at howtographql.com and was wondering if it's now outdated and what the migration would look like.