Apollo Client is a state library that integrates closely with GraphQL requests. I just have a quick code sample here. If you want to create a new Apollo Client, you give it a URI, and here I've also given it an in-memory cache for its requests. It will then be able to client.query, send queries to that GraphQL URI, and store the results in the cache to cache and de-duplicate them nicely. Now I'll note that it requires queries passed through this GQL string formatter, which does some behind the scenes niceties, but also means that VS Code extensions and other IDE versions know to give you nice syntax highlighting. So these, what, 4, 1, 2, 3, 4, 5, like dozen lines of code, this is a quick way that you can send a query to a GraphQL endpoint to get the results and have it be nicely cached in memory. But the real magic of Apollo Client, comes in when you start using its tools for other frameworks such as Apollo Provider, which sets up a React context for Apollo Client to become available to queries within. Now to start, you can take in this useApolloClient hook in your components, and I am starting to do that here. So let's take a look at my components, Apollo, component, Apollo, wrong one. Here we go. Here in my GetServerSideProps, I haven't even used the provider yet, I'm just creating a new Apollo client, and I am awaiting client.query. Here to get the results in my component as jokes, here we go, return props jokes, which is either failed or the data, and then that's back to the components, and here we have my nice little GetRandomJoke GQL. But there's some really nice Apollo, oops, Apollo component. There's some really nice tools that get generated for you. For example, if you wanted to write a query, what you can do is you can say, const result equals Apollo.UseQuery, and then pass in your GQL of query. Or you can use the Apollo Code.Gen to generate the Apollo functions, the Apollo hooks for you that use your queries from your GraphQL. So here I have the generated code from Apollo Client Code.Gen, which I will show you the schema for. It's really beautifully small. This TypeScript React Apollo plug-in for your Code.Gen, which then spits out these hooks that use the Apollo Client, which is then available within components wrapped by Apollo Provider. And now I have a component, which in a single line of code, runs this random query, which is defined in my auto-generated code from my .graphql file. So in a nutshell, putting it all together, I have defined in my .graphql file a random query here, which then gets auto-generated into a constant variable here, and a hook here that then returns loading error or data, one of which will be populated at any given time. And if I go to source pages with Apollo query, therefore, look host 2000, with, or...what is it? With Apollo queries slash components, wait for it to build the page, d-d-doo! Voila! Now on my page, I get a random cat, pun, sad little joke every time I load the page. And just looking at the network tools to see how that looks, boop-ba-boop! Here we go, my GraphQL fetch which has a payload with the query and then the response data sent back to me. Amazing. So using this Apollo provider, we can then create these Apollo query hooks that return this tri-state data or error or loading and generate from our darn .graphql file a whole list of things we can use, unfortunately I can only show you these quick little examples, use query, use lazy query, use mutation, use subscription, there's all sorts of stuff and just use query gets shown, but trust me there's a lot of great stuff and I have docs in the slides for more data if you're interested. GraphQL code gen can generate Apollo components, hooks, etc. from GraphQL files and that is I think an incredible way to live your life. You take from your GraphQL queries this entire area of code generated for you so that you don't have to keep writing boilerplates and I highly recommend trying these out if you have the ability to. Mind freaking blown. So let's talk a little bit about where you can go to see more information. I have all these things and a little bit more in the companion repositories, GraphQL galaxy, a lovely trio and a lovely GraphQL server. The core technical resources, nextjs.org, GraphQL code generator.com and Apollo GraphQL.com are all excellent. And if you want to see some cool additions, Blitz.js is a framework that builds on top of Next.js and RemixRun is a somewhat of a competitor to Next.js that came out open source recently. And lastly, my favorite TypeScriptBlind.org, just a great language to write your stuff in. Thank you to everyone, in particular, the GraphQL galaxy organizers. I'm Josh Goldberg, you can find me on Twitter at JoshuaKGoldberg and on the web at joshuakgoldberg.com.
Comments