text stringlengths 0 2.35k |
|---|
**Mark Sandstrom:** Yeah. And you need a different strategy, because with a SQL database you can select just the fields you want, and document -- at least the document database we use, you get the entire entity from the data store. |
**Break:** \[15:11\] |
**Jon Calhoun:** So it sounds like using GraphQL on the server side is going to complicate things... So presumably it's solving some sort of problems to justify all that added complexity? |
**Mark Sandstrom:** Yeah. |
**Jon Calhoun:** So can you speak a little bit to what problems it's solving and how it was helpful at Khan Academy? |
**Mark Sandstrom:** \[16:16\] Yeah. So Ben mentioned just RUST APIs versus GraphQL APIs, where in a REST API you may be making multiple requests and stitching it together on the client. I think it really does make it easier for the client; we have a couple clients, we have a website, and we also have a mobile client... |
**Ben Kraft:** To me, one of the really nice things is that -- as someone who mostly works on the server, one of the really nice things is that you don't have to know exactly which client is going to want what, where; you don't have to -- if the frontend developers decide they need three more fields in this particular ... |
**Mat Ryer:** Yes. But haven't you really just moved the problem there though? At some point we have to decide what data we're gonna load... And obviously, if there's a real separation between client and server, which if you have a public API, you have customers that are not part of your organization, so that's a signi... |
**Ben Kraft:** In some ways it is. But I think the nice thing about GraphQL over SQL is it's a much simpler language. If you wanted to try to implement a whole SQL server, that includes your business logic... Like if you build a really simple app, it may look like you're kind of just making requests to the database, an... |
**Mat Ryer:** Yeah. |
**Ben Kraft:** And for that, you get a lot of flexibility, and that flexibility -- you know, on the other side it is still tricky for the client, in some cases, to decide "What information do I actually need?" Because you may have some huge React app, and you need to know which of your components is way down the stack;... |
**Mat Ryer:** I'm glad you mentioned that there's better ways to do it than just doing it in SQL, because of course, this is Go Time, not Stored Procedure Time. That's a separate podcast, coming soon, starring Jon Calhoun. |
**Jon Calhoun:** Oh, boy... |
**Mat Ryer:** Jon, you're gonna do an all about SQL and stored procedures podcast. Is that right, or have I just made that up? |
**Jon Calhoun:** I hope nobody's signing me up for it without letting me know... \[laughter\] |
**Mat Ryer:** Oh... You were nearly gonna do it. |
**Jon Calhoun:** \[20:00\] Some of these problems are interesting to me, because it seems like people are approaching them from different angles. One of the more recent React frameworks was Remix, and I believe they're trying to solve some of those similar problems of not knowing what data you need further down in your... |
So I've seen people approach this problem, like you had said, Ben, where you need to kind of know what data the entire page needs, whereas sometimes they're just like, "Well, let's just not know what the entire page needs, and let each thing load its own data." And there's pros and cons to both, I'm sure, but... You kn... |
**Ben Kraft:** I think one of the cool things with GraphQL is that in theory - and I'm not enough of a client developer to know how much this works out in practice... But in theory, you can kind of roll that all up statically, where each of your components may need something different, and you just kind of roll that al... |
**Mat Ryer:** Yeah, it probably comes down to organizational things even... Like, if you've got a little team that are working on something, you don't want them to have a dependency if you can help it. One of the advantages I can see to GraphQL is that it does empower the client. So that means if there is another team ... |
**Mark Sandstrom:** Yeah, so when loading data for a single-page app, you can load the data at the top level and pass it down through the tree, or you could have components own their individual queries and load a subset of the data. But to get to your question, you can construct queries that are very expensive to resol... |
**Mat Ryer:** So with that power that you get also comes great responsibility. That's essentially your message. |
**Mark Sandstrom:** Right. You can see the queries that the client is sending to the server, and exactly the data that they want, so it does provide an opportunity to optimize for returning that data as well. |
**Mat Ryer:** Yeah, that's a good point, because of course, even if it's inefficient, you'd be able to run a report; assuming you were instrumenting this stuff, you'd be able to find out where are the inefficiencies, and then optimize it after, without changing the interface. |
**Mark Sandstrom:** Yeah. And then another consideration is protecting a GraphQL server against denial of service. Because if anyone can construct any query, then that can get very expensive. So one strategy for that is calculating the complexity for query and not allowing queries beyond a certain complexity. |
Another strategy which we use is we have an entire list of all the queries that clients are allowed to send to our server, called the safe list. And if it's not on the safe list, our server will not execute it. |
**Mat Ryer:** Ah, so that's sort of like you've locked that down after. |
**Mark Sandstrom:** That's right. |
**Mat Ryer:** You let the clients free to build and ask for what they need, and then lock it. |
**Mark Sandstrom:** \[23:54\] That's right. Yeah, we statically gather those across the mobile client, across all of the backend services, and so we have a complete list, which allows us to do other very interesting things, since we know exactly what data is being asked for everywhere. We also use GraphQL for communica... |
**Mat Ryer:** Do you really? |
**Mark Sandstrom:** So not just client-client, but service-service, yeah. |
**Mat Ryer:** Is that to have the self-similarity and the familiarity and all the dogfooding benefits? Or is it because you then also get to only select the data that you need? Do you use those features of GraphQL, too? |
**Mark Sandstrom:** It's both. We use federation to make it so the client doesn't need what services, what fields come from; backend services take advantage of that as well. So the services expose one API, and we do have access controls where we'll lock down a field so that it's just allowed to be used by other service... |
**Jon Calhoun:** So when you have a list of "These are the accepted queries", how does that work with a public API where presumably people would think they can kind of generate whatever query they want? Is it just in the docs, like "You can't do certain things"? |
**Mark Sandstrom:** I think in that case you'd wanna go with a complexity approach, and have some sort of perhaps budget when the user is making requests, and if they go through their complexity budget, you start rejecting their requests. |
**Jon Calhoun:** It'd be interesting to see some of the approaches... |
**Mat Ryer:** You're also rewarding the people for keeping the queries simpler, if it's a complexity budget. That's actually quite an interesting idea, when you think about that. |
**Jon Calhoun:** I'd start to wonder if -- like, you'll see some APIs that have rate limits of like this many requests per minute, and if they start to have complexity limits instead of... You know, you can do as many requests as you want, as long as your complexity doesn't reach a certain amount. That could be very we... |
**Mat Ryer:** Yeah, if you're trying to build an app that figures out how many degrees of separation between you and Kevin Bacon... This was a meme a few years ago, which I'm trying to bring back. You know, then that would be expensive, complex, so you maybe can't do it... But maybe it's actually not, as well. Obviousl... |
**Jon Calhoun:** It would be called more than twice, I assume, because for every friend it would have to call that function. |
**Mat Ryer:** Oh yeah, I meant get friends, or the friends function; the one that loads the list of friends... If you then got friends of friends... |
**Mark Sandstrom:** Yeah... |
**Mat Ryer:** Oh yeah, multiple times. You're right. |
**Mark Sandstrom:** Yeah. And exactly how you're fetching the data depends on the structure of your data. So if the friend has the ID of the friends, then if you only select friend and ID, that's gonna be much less expensive than if you select the friend name, so it actually has to fetch the friend from the database to... |
**Mat Ryer:** Right, right. |
**Mark Sandstrom:** I mean, we're talking about the server bit... And we could talk a little bit about the mechanics of what gqlgen is doing, if we wanted. I think something that's interesting to mention about gqlgen is it's a schema-first library. So you write a GraphQL schema, which is defined in the spec, and you po... |
**Mat Ryer:** So there's a really interesting detail there which I think we should probably shine a light on. You say there's a schema, and that schema describes the API. You get pretty good discoverability with that, don't you? |
**Mark Sandstrom:** You do, yeah. |
**Mat Ryer:** \[27:54\] It's an interactive web client that can connect to the end point, and gives you like IntelliSense, so you can actually look at the objects... It's kind of self-documenting, so it does feel very modern in that sense. And that's also great, if someone's consuming an API, having that at your finger... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.