text
stringlengths
0
2.35k
**Mark Sandstrom:** Yeah.
**Ben Kraft:** That's definitely something people do, and something that in writing a client has -- we've seen some interesting use cases come out of folks who were using our client on GitHub... Like, "How the heck did you get this schema like that?" And it's "Oh, they're using one of those things that just kind of tur...
**Mat Ryer:** Yeah, exactly. I think you still have to design your API. So Mark, you were saying that you take the schema and then you generate interfaces and things, which then makes...
**Mark Sandstrom:** Yeah, that's what glqgen does. So you're expected to implement the interface type and the struct that you create that implements this interface. That's where you can inject all the dependencies into the resolvers. The resolver functions themselves are methods on the struct. And I also mentioned that...
**Jon Calhoun:** I think one thing that's worth pointing out here is that because gqlgen is schema-first and because GraphQL has a schema, that means that this is a typed query language, where you actually know what all the fields are... Whereas when some people are used to like a JSON API, there's not really a type st...
**Mark Sandstrom:** Yeah.
**Jon Calhoun:** I remember working with one API where there was like one field on it, and if it had one item to return back, it gave you back a string with the ID, but if there was multiple, it gave you an array back... There was something about the request where you could actually request multiple; so if you requeste...
**Mat Ryer:** Well, they're trying to be helpful, aren't they? They're trying to optimize. It's really clever. If there's just one, it's a string; if there's many, it's a slice of string... That feels intelligent, in some way. I mean, you don't realize that "Actually, yeah, I'd rather just have it as an array."
I built an API once that was just all arrays. There weren't any objects. And if there was only one object, it would be an array with one object in... Because then you just could write it once. You could write an implementation that just looped over this data, and it worked for one and multiple. So you'd kind of get tha...
**Mark Sandstrom:** We doubled down on making our GraphQL schema the authoritative place for our documentation... So attached to each field, you can put a doc string; attached to each type, you can put a doc string... So really, in our systems, if you wanna understand the broad data model and how things connect togethe...
**Ben Kraft:** \[31:49\] One of the nice things about GraphQL is that it's a fairly opinionated type system. I mean, you can certainly have a type that's like string or a list of string, but it's gonna encourage you to do the list of string. If you design your GraphQL schema well, that's really where you make that deci...
**Jon Calhoun:** Alright, so when it comes to actually working with GraphQL - and this is a Go Time podcast... So were there any challenges that you guys experienced that felt specific to Go, or maybe specific to a typed language, versus using JavaScript or something else when using it?
**Ben Kraft:** Yeah, so I can talk a lot about this, so please, cut me off. But I think the first challenge we had, which will contextualize the client that we wrote, is that -- you know, the great thing about GraphQL is it's a typed language, and the great thing about Go is that it's a typed language. But you've gotta...
So the client that we wrote, genclient - it uses your queries and it uses the schema, and it generates the correct types for you. So if you make a query that's incorrect, you can do that, but at compile time it's gonna say, "Sorry, we checked against the schema. There's no field called 'namee'. Maybe you meant 'name'."...
So that's something that I think Go is really powerful, especially using Go and using code gen, and using a really well typed language. You get the typing all the way; if you're using a library like gqlgen, where you've got good typing on the server, and you're using a library like genclient, where you've got good typi...
**Mat Ryer:** And they both share the same schema, right? The source of the code gen is the same data... Because there's another advantage to this. It's not just -- I mean, it is what you've said, really, but as a sort of iteration, development engineering process, having that confidence of being able to, first of all,...
**Ben Kraft:** Yeah. And it really helps when you're evolving your API if you wanna add a field that's pretty safe. But if you wanna remove a field or change the type of a field, you have to check that nobody is using it in a way that's gonna break. And that's the kind of thing that if you have a typed server, you have...
**Mat Ryer:** Yeah, especially if you're using TypeScript in the frontend as well; you literally have types there, too. I mean... Yeah, really.
**Ben Kraft:** Yeah. And a lot of the inspiration for gen client came from clients that already exist in TypeScript, that'll similarly generate your TypeScript types from your schema, and then just use them.
**Mat Ryer:** It's good, ain't it? I'm usually not a fickle man... This episode has completely convinced me to now use GraphQL on the server. To be honest, I've never comprehended -- I thought it was built writing SQL, and writing joint queries, and things like this, in the backend.
**Jon Calhoun:** \[36:03\] I think that gets confusing because --
**Ben Kraft:** You can do that, but...
**Jon Calhoun:** Like, you see all these services out there that are like "This is a database with GraphQL built in", or it will turn your Postgres database into a GraphQL server... It is kind of confusing at times, because you're not really sure what it's doing or how that's working, or whether that's what you'd want....
**Mat Ryer:** Yeah, that can't be great. That sort of general approach can't be great. Maybe on small projects, or -- frankly, any small project you can get away with a lot. It's a great tip - if you want to write really robust software, don't make it popular. Just don't have that many users... If you can. I'm very goo...
**Jon Calhoun:** Like you said, the fields - knowing that you don't have to request them all... I've definitely built some smaller APIs where sending the entire resource back just doesn't matter at the end of the day... So it's like, that's not a real benefit for me. It really just becomes a -- you know, trying to figu...
**Ben Kraft:** Even with that, I think the big benefit is really -- it's not that you can skip requesting name; it's an extra 10 bytes, or whatever... It's that you can provide those linkages to friends, and friends of friends, and friends of friends of friends, and you can decide exactly how far down you want to go. I...
**Break:** \[38:06\]
**Jon Calhoun:** So I guess I'm kind of curious on both of your opinions, since you've built some stuff with this... Typically, when I'm helping somebody learn to build an API or do something like that, my general advice is to start with just a simple REST JSON API that returns some data... Mostly because if you only h...
So I guess, what is your advice on when to get started with it, and then can you share a little bit about how you guys got started, resources, that sort of thing?
**Ben Kraft:** I would say get started when you start to find REST frustrating. When you find it's like "Oh, now I have to go change all these six different APIs because I wanted to change one page" or "Now I have to figure out who's using that API, because I wanted to make a change to it, and it's used by 16 different...
**Mark Sandstrom:** Yeah. And I was gonna say, we also had some tooling for typing REST responses... And so you know, if you find yourself building out that kind of tooling, perhaps using something that's going to assist you that has that already built in is a good option.
**Jon Calhoun:** Yeah, that makes a lot of sense. Earlier I mentioned the Stripe API, and if I understand correctly, they have a ton of tooling built around what they do... But at the same time, I think some of these companies had to invest stuff back when GraphQL didn't exist... So it's like, "How do we make this work...
**Mat Ryer:** Would you use GraphQL if you didn't have relational data in any way? Would you still use it now?
**Mark Sandstrom:** Yeah.
**Mat Ryer:** Cool. Simple as that.
**Jon Calhoun:** That's all the answer you get, Mat. Just yes.
**Mat Ryer:** Yeah. Fair play. It's just a boolean answer; I like it. Maybe you could answer in a string now, since we're talking about types...
**Ben Kraft:** I don't know, I think most data is somehow relational. There are relationships between your data. If there's not, what is the data even? And there are certainly exceptions, but I think if you genuinely have -- just your entire data is one big table, maybe you don't need GraphQL. But I think almost everyb...
**Jon Calhoun:** I guess one question I have related to that is if you're releasing a public API - GitHub is an example of one that's a GraphQL API - do you feel that releasing it as a GraphQL API makes it harder to get adoption, in the sense that people are kind of... There's a certain subset of developers who know ho...
**Mat Ryer:** Yeah, I suppose there is also a bit more cognitive effort to actually deciding what data you need, and discovering that, whereas it's much easier to just say "Get this thing", but in reality, what difference does that really make?
**Ben Kraft:** Yeah, I mean -- I think there's a risk of that for anything. I think GraphQL is easiest to use when you're working with the people who are writing the client. There are certainly public GraphQL APIs, but I think when you are kind of both sides, it's really the easiest to adopt.
**Mark Sandstrom:** \[44:04\] Yeah. And I think writing a GraphQL query is pretty intuitive. Mat, you mentioned tooling where you can write a query, select a field, and actually that tooling will pop up, this is the type, and here are all the other fields that it has, and the documentation for those fields... So it doe...
**Jon Calhoun:** Alright, we're getting sort of near the end of the episode, so is there anything else you guys would like to talk about before we move on to Unpopular Opinions?
**Mat Ryer:** Related to GraphQL, please.
**Mark Sandstrom:** Yeah, I know, I know... I think it was pretty good. We had it on the list to mention federation. Something I can mention right quick around that is - you had asked what advantage GraphQL was giving us at Khan Academy... We moved from a monolith written in Python to Go services, and federation was ke...
**Mat Ryer:** What do you mean by federation in this case?
**Mark Sandstrom:** Yeah, so this is where different services own different fields, and you send your query to a gateway instead of a single backend service. It will figure out which services to contact, to connect to to get that data and stitch together and return it. So we treated Python as just any other service. An...
**Mat Ryer:** That's cool.
**Mark Sandstrom:** Yeah. It worked really well.
**Jon Calhoun:** The federation is one of those things that I've looked at because it looks cool, but I've never had a project I've been working on that's like at the scale where this makes sense... So it hasn't been something I've actually dove into. So it's interesting to see that that worked for you guys, for your p...