Build and Deploy Instant GraphQL APIs to the Edge

Rate this content
Bookmark

Learn how to generate instant GraphQL APIs using a data source connector (GraphQL and non-GraphQL sources), extend and join them both with custom resolvers and deploy to the edge without leaving the code editor.

Jamie Barton
Jamie Barton
55 min
10 Nov, 2023

Comments

Sign in or register to post your comment.
  • Nikhita Sareen
    Nikhita Sareen
    none
    Is anybody able to join?
  • Lucas Estevão
    Lucas Estevão
    Avenue Code
    It should start at 9 a.m. PST, I guess there was a confusion because of time changes in the US.
  • Nelly Ruehl
    Nelly Ruehl
    JPMorgan Chase
    Is this still happening? Zoom says "waiting for the host to start the meeting".

Video Summary and Transcription

In this Workshop, we explore GraphBase and GraphQL, focusing on using Neon as a connector and Postgres as a database. We learn how to set up Neon and the Postgres connector, automatically generate a GraphQL schema, and create a GraphQL API with Neon. We also delve into edge caching, extending GraphQL APIs, and integrating with MongoDB, Contentful, and other data services. Additionally, we cover adding API rules and security with Click as the auth provider.

1. Introduction to Graphbase and GraphQL

Short description:

In this part, we will explore Graphbase and GraphQL. We'll discuss the challenges of using multiple tools and services in the ecosystem and how GraphQL can provide a unified API. We'll also address the issue of data latency and introduce GraphQL edge caching. Additionally, we'll focus on using a Postgres database with NEON as a connector and demonstrate how to deploy globally using CloudFlare workers.

Hey, everyone. Thanks very much for taking the time today to join me, and we're going to be spending some time today looking at Graphbase and GraphQL. This isn't a workshop people to GraphQL, but I'm more than happy to answer any questions that anyone has there. So, hey, Natasha and everyone else that's in the chat, great to have you all. And please, feel free to interrupt me at any point.

The style that I'm going to go for today actually is going to be me just showing you a few things. And if you have access to GetNation portal, there is other workshops and videos on there from myself about this very topic. But I think today we can go through and show various parts. And I want to talk specifically about one way that you can build GraphQL APIs on top of a Postgres database with a new connector that we have. So that could be interesting. And we'll kind of take a tour of the product and what it enables developers to do. And hopefully, that's enough to, you know, inspire some questions and, you know, invite you to try it out.

I'm just going to flip through some slides to begin with, just to set the scene. And like I said, please ask any questions, should you have them. But I want to talk to you about GraphQL and how you can make your data fast. I'm Jamie. I've been doing GraphQL for about five close years now. I do have a website, GraphQLWTF, where I release weekly videos. That's currently on pause, but I will be resuming weekly videos on that at the moment. There's a huge back catalog if you're interested. You can follow me on Twitter, whatever it's called today. My handle is just Barton Backwards, which is no trab.

The biggest problem that I think we see today, or certainly the problem that I've experienced personally, is we have so many different tools in the ecosystem, and all of these tools allow us to do so many great things. Even my own website has tools for sending notifications via email, via Slack, pulling in content from a CMS, pulling in content from a digital asset management platform to get my images and videos, and certainly from my GraphQL website, content comes from a lot of different places. Which is great. I use these kind of best of breed services, as the industry calls them. But we often spend so much time figuring out how each of these APIs work. We spend so long reading each documentation or SDK reference to really figure out how everything works together, and by the end of it we've got a package json file that's got a dozen or if not multiple gigabytes worth of Node modules just to install SDKs and whatever, and that's okay. But I don't think it's the best experience, and I think GraphQL actually shines here because it allows us to interface with all of these different services as one API. And the other issue that I think we have is, all of these different services belong somewhere, the data is stored somewhere, and it's accessible. If I'm here in the UK and I'm wanting to access data that's on US East one or whatever, I have to make a request, get that data and the latency there can add up, right? And that latency is replicated for each individual service that I use, which isn't great either. Now, a lot of the different service providers that we use do allow you to replicate data or whatever, but you often have to pay a per provider a cost to replicate. So that's not great, you have all of these different costs associated with just one output because you've had to replicate content around the world. So that's not an amazing experience, at Graphbase, we are trying to resolve that by something that we call GraphQL edge caching. So we're able to put a layer in front of your data sources so when someone requests that, we cache that and response times go from looking something like this to the green line. Anything you request comes through a lot quicker. So how do we go about connecting stuff? Well, this is three ways that I want to show you and I'm not going to be focusing on these too much today, the postgres one is the one that about. We have three connectors here. MongoDB and an open API connector. The open API connector is pretty cool because that can take an open API specification and you can use that spec and the great thing about specs is they follow a specification so we're able to determine what fields are and end points are and what arguments they take because these specifications follow a spec and the schema follows a spec. So, we're able to automatically create a GraphQL API based off the open API spec, which is pretty cool. And because it's GraphQL, we can introspect that in the same way. We can introspect GraphQL APIs and we can generate something that you can use. MongoDB, on the other hand, here we have a way to define models and specify fields on those models and kind of add some strictness to our MongoDB, which is traditionally a document database. We can add some kind of rules and behaviors around how documents are stored and we generate all the APIs for that. But the one that I want to focus on today is using a Postgres database and we'll be using NEON, so please go to NEON dot tech and sign up and create your own database there if you want to follow along. But here we're able to kind of plug in NEON as a connector, add it to our schema as a data source, then we're able to add caching to that so when we make a request it fetches data from the cache and not from NEON. Then once we've got all of our connections and data sources lined up all we then need to do to deploy this to the edge globally around the world, over 200 points of presence, is run an npx craftbase deploy and that will deploy around the world which is pretty cool. Everything's built with Rust and it is then packaged up using WebAssembly and deployed into different, not lambdas, but CloudFlare workers that's deployed, like I say, around the world on the CloudFlare Edge network. Everything I'm going to show you and talk about today is open source, please check that out if you're interested.

2. GraphBase, Neon, and Postgres Adapter

Short description:

Once we've deployed everything globally, we need to understand how to query it. Neon provides a dashboard and database that allows us to form GraphQL queries. We can create things with mutations and fetch things with queries, including pagination arguments. pathfinder.dev is a tool to run GraphQL queries. GraphBase aims to solve the problem of consuming data from different services by providing a unified GraphQL API. It also offers automatic deployment of API versions when changes are made to the database. Today, we will focus on Neon and the Postgres adapter. We'll explore the hosted version briefly, but most of our work will be done locally. GraphBase deploys GraphQL endpoints around the world, utilizing edge-caching and features like KVStorage to reduce latency. Each change to the schema is logged, and specific deployments can be accessed for different branches or features.

But once we've got everything kind of deployed around the world we then need to figure out how do we query this, what does that look like? Well, here we can see in the background is a screenshot of Neon. I'm going to dive into this in about two or three minutes time. And we're going to look at that dashboard and database and more tables and data we've got. But that database we're able to form GraphQL queries from. We can see on the screen here we've got a mutation to create things. Then we've got a query to fetch things. And we can see we've got some pagination arguments in here to fetch the first or last or whatever nodes we want from the database. And all of that is generated automatically, which is pretty cool. And yeah, lastly, pathfinder.dev is a way to run these GraphQL queries. This doesn't just work with Graphbase, but it works with any GraphQL API. It's a way to view and perform GraphQL requests similar to GraphiQL, if you've used that before. And that's what we have on the local experience and the deployed versions of Graphbase.

So I think that probably is a good transition to talk about Graphbase very, very quickly. Kind of give me the reasons why we created Graphbase and what we're looking to solve. But just as a high-level overview, GraphQL here, we've got a query to fetch a product by a slug. And the idea, the things that we can make really nice and intuitive for you as a developer is, you could have all of your different data sources, like I mentioned before, spread across different services. And if you want to create one query to fulfill this card requirement here, we can get the image, the title, the price, the variance, whatever, from all the different services that are managing that. So we've been told over the last few years it's important that we have different services to do their own individual things. But actually consuming all of that is actually a problem that we created because of that. And GraphQL is a really cool way to solve that. So, yeah, what GraphBase will do is, we will give you an API to call this GraphQL query. We will deploy that. Whenever you create a pull request on your work and you make any changes to your database, we'll deploy a different version of that as well, which is cool. Cool. So, yeah. Hello, and welcome to anyone that is just joining. It's great to have you all here. And like I said, please let me know if you've got any questions.

So, now we've set the scene. We've talked about the issues and problems that we have and why we create a GraphBase. For anyone just joining, we're looking to solve these two things here. Lots of different data sources with a different learning curve for every single tool that we use and the issue where all of this stuff is deployed around the world and it costs so much because we have to replicate that for each individual service. We think we can bring all of that under one GraphQL API. And today, I want to spend some time talking about Neon and the Postgres adapter that we've just created. So, Graphis, when you sign in, if you go there now and you get started in creating accounts, I'll sign into mine and show you around very briefly what the hosted version looks like. But everything we're going to do today is pretty much going to be something that we work on locally. So here, we've got a few different projects. If I just go to this project here, we can see here, this is our dashboard, we've got our GraphQL endpoints. And these are the endpoints we tell Netlify, Versaille, Fly, wherever you deploy this, this is my GraphQL endpoint. And that endpoint is distributed around the world, deployed to the edge, and has all of that kind of edge-caching stuff built alongside it. We've got things like KVStorage, which you can access all at the edge as well. The idea is to reduce that latency as much as much as we can. So this is a project that gets hammered quite a bit from some kind of tool that compares different edge deployments, but it's kind of good enough to show you around. So here we've got all the deployments that every single time you make a change to the schema, the schema is something I'll show you shortly, but every single change you make, you can go in and see, I added this, I removed this, I added this field, I added this connector. You can see exactly what went on and they build logs for that as well. And you can see exactly who did that. You've got access to the individual endpoints for that specific deployment as well. So if you create a branch to add a new feature or whatever, you get a specific deployment for that GraphQL API. And that's a pain that like setting that stuff up manually. If you're not using something like GraphBase, it's a bit of a pain.