text stringlengths 0 2.35k |
|---|
**Mat Ryer:** Yeah. Yeah, yeah. |
**Jon Calhoun:** Come on, now... Alright, sorry everybody. |
**Mat Ryer:** Treat me like a guest. |
**Jon Calhoun:** We're also joined by our other host, Mat Ryer. |
**Mat Ryer:** Hello. |
**Jon Calhoun:** Mat, how are you? |
**Mat Ryer:** Thank you for remembering me. I'm doing fine, thanks. Very glad to be back, it's been a while, but... |
**Jon Calhoun:** It's been a while for me, if you can't tell, based on my terrible introduction for you. |
**Mat Ryer:** Nah, it's alright, really. I know sometimes when I'm not the host I tend to mess around and be silly, and stuff, but Jon, out of respect -- well, I'm gonna obviously carry on. I'll just carry on actually doing that, probably... |
**Jon Calhoun:** Alright. We're gonna first start off by just talking about what is GraphQL. So Ben or Mark, do either of you wanna take that? |
**Mark Sandstrom:** Yeah, I'll take this. So GraphQL is a specification for building APIs. It consists of a query language for fetching data, and also a spec for implementing GraphQL servers. It can run over any transport, but it's usually run over HTTP. You send a query to the server, usually as a post request. If you... |
\[04:05\] So the structure of the data that you're requesting is specified in a schema that you write... So your schema might have a type called user, and the user would have an ID, and a name, and perhaps friends... And friends could be an array of users... And if you follow that through, those friends could have frie... |
**Ben Kraft:** I guess the way I like to think about GraphQL is as a solution to some of the classic difficulties of a REST API. So say you have an API and you wanna get the current user, but different pages wanna get different pieces of information about the current user... So one page might just want their ID and nam... |
GraphQL says "We're just gonna have this whole schema", and then the client says "Here's what I want", and the server just sends it back, and it's all in one API call, and it makes things super-easy. |
**Jon Calhoun:** Yeah, I know when I first looked to GraphQL, it kind of struck me as something that somebody on the client side would have dreamed up... Whereas when you look at a REST API, it looks like somebody on the server side dreamed it up, because it literally just tends to mimic exactly what's on the server si... |
**Ben Kraft:** Yeah, that's right. It puts a lot more work on the server, to say "Here's what the client wanted, and now I have to do all the glue to put it together." But when you're actually writing a GraphQL server, most of that is gonna be in your GraphQL library, so you still just write the application code. |
**Jon Calhoun:** Ben, you mentioned REST APIs... I know there are some out there that have been trying to sort of solve this problem. Stripe is one that comes to mind, where -- I don't know if you're familiar with their API at all, but they have a way, when you're making REST requests, that you can include... I think t... |
**Mark Sandstrom:** Yeah, that's right. It's considered an anti-pattern in GraphQL to have fields that are just IDs. So when returning an array of friend IDs, instead you can select into that object, and that prevents you from having to stitch together the data on the client. |
**Ben Kraft:** But yeah, I really think of GraphQL as sort of - you choose which fields you want in a REST API, and you really supercharge that and you make it the whole thing. You can nest it, you can -- that's just the whole API is structured, and that's GraphQL. |
**Mark Sandstrom:** You can also tailor fields a little bit more to the client. You have to normalize data, for example if you want, because it's okay; the client doesn't have to select a field if they don't want, so you can really include additional things that are just especially handy for the client to select direct... |
**Ben Kraft:** Yeah. I think at Khan Academy our user had like a hundred fields, because it's anything anyone needs anywhere; you just put it somewhere useful, and then most clients are only gonna select like five fields. And that's fine, they don't pay for the others. |
**Jon Calhoun:** \[07:51\] I think that's something that me personally, I wouldn't have thought about at first, because I have a lot more experience just making JSON RESTful APIs that send that data back... And if your objects aren't too big, or there's not too much there, it doesn't make much of a difference. But like... |
So GraphQL - Mark, you mentioned that it kind of looks like JSON when you're writing these queries, and then the data you get back actually is JSON. Is that correct? |
**Mark Sandstrom:** Yeah, that's right. |
**Jon Calhoun:** Okay. So I'm assuming that means that there's both a server and a client part... So can you elaborate I guess a little bit more on that part? |
**Mark Sandstrom:** Yeah. So a GraphQL client is pretty simple. You package an operation in a JSON payload, and if you can make an HTTP request, you get a JSON response back, and clients - that's the basic thing that a client needs to do. And client libraries can provide additional facilities to make certain use cases ... |
You can't actually write a GraphQL query that's infinitely deep, so therefore you wouldn't end up in an infinite loop, and your server trying to resolve friends of friends of friends. Fields can also take arguments, and so really, each field is like a function. Some are really simple, and some are more complex. Not eve... |
In Go, one of the nice things - each separate resolver function is resolved (in gqlgen) in a separate goroutine. So they're resolved in parallel. |
**Jon Calhoun:** So you mentioned gqlgen, and Ben had created genclient, just so people know, I guess, when we get into this... Gqlgen is a library that helps generate the server-side stuff, and genclient is something for the client-side, correct? |
**Mark Sandstrom:** Yeah, that's right. And we've talked about JSON, we've mentioned JSON a few times... So one way to handle JSON in Go, or the way that JSON is handled in a JSON package is via struct tags. So there are some libraries that use struct tags to help you generate queries and interact with the GraphQL serv... |
**Jon Calhoun:** Mat, you had said before we started the show that you've written -- I believe you said you wrote, was it a client or a server with GraphQL? |
**Mat Ryer:** A client, yeah. |
**Jon Calhoun:** So when you were writing that, do you remember what library you were using? |
**Mat Ryer:** I wrote the thing. I wrote the client library. |
**Jon Calhoun:** Okay. |
**Mat Ryer:** I wrote the Machine Box GraphQL client library as well, yeah. |
**Jon Calhoun:** Okay. |
**Mat Ryer:** Thanks for bringing it up. |
**Jon Calhoun:** No problem. So when you were writing it, what language were you writing it in, Mat? |
**Mat Ryer:** I was writing in Go, and I was consuming a third-party GraphQL service. And at the time, there wasn't a way to do it, but you can just do it -- it's normal HTTP, as long as you get the request body that makes sense for the server... And I think you pass the query in as a parameter, but it is like a JSON-e... |
So it's like a very simple, lightweight library, and it was very easy to put together. But that's definitely not the case on the server. I sort of almost can't really imagine what that looks like on server code, to implement the server-side GraphQL. |
**Mark Sandstrom:** \[12:14\] Yeah. I looked at your client right quick, and it looks like you specify the query as a string when you're making the query. Like you said, it populates the data... So in that case, you have the query string, and the struct, and you have to keep the two in sync with one another... So there... |
**Mat Ryer:** Right... |
**Mark Sandstrom:** But that can get very complex, because it's not as simple as JSON, where it's just the field name. You have to start passing arguments, and there are things like fragments and interfaces. |
**Mat Ryer:** Well, that is sure cool... |
**Ben Kraft:** I guess the way that I think about it is on the client all the complexity is in the query and in the types that you're deserializing it into... And so I think Mat's client just kind of lets you deal with that, and it's really straightforward. I think shurcooL's client kind of tries to hide the query; you... |
But then on the other side, on the server, the types are actually a lot simpler, because you just have your user type, and it's got a bunch of fields, and a bunch of methods... And then the hard part is the server library itself, that glues all of that stuff together and actually has all this logic, to say "Did the use... |
**Mat Ryer:** Yeah. So it's a perfect kind of problem for code gen, I think, that particular piece. And you mentioned earlier that there were functions that basically are called optionally if somebody asks for friends of the user. There's another function that's gonna get called. So that goes some way to explain, on th... |
**Mark Sandstrom:** It's up to whoever is writing the server and what the database is that you're using. At Khan Academy we use a document database for a lot of things. We do have SQL here and there... |
**Mat Ryer:** So that's interesting then. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.