Skip to content
450: Javascript-Driven Development? episode artwork

Dec 10, 2024 39 min

450: Javascript-Driven Development?

from The Bike Shed Hosted by thoughtbot

Joël and Stephanie go back to fundamentals as they pick apart some recent conversations they’ve been having around the office. Together they discuss the advantages of GraphQL over a REST API, how they utilise JSONB over a regular column or table, and the use-cases for and against a frontend framework like React. But what’s the theme that ties all these conversations together? — The article mentioned in this episode was Why I’m over GraphQL Your hosts for this episode have been thoughtbot’s own Stephanie Minn and Joël Quenneville. If you would like to support the show, head over to our GitHub page, or check out our website. Got a question or comment about the show? Why not write to our hosts: [email protected] This has been a thoughtbot podcast. Stay up to date by following us on social media - LinkedIn - Mastodon - Instagram © 2024 thoughtbot, inc.

Episode details come from the podcast's RSS feed.

Embed this episode

Ready to play

450: Javascript-Driven Development?

0:00 39:57
of matches

Transcript Auto-generated, may contain errors

Hello, and welcome to another episode of The Bike Shed, a weekly podcast from your friends at ThoughtBot about developing great software. I'm Stephanie Min. And I'm Joelle Kinville. And together, we're here to share a bit of what we've learned along the way. So, Joelle, what's new in your world? What's new in my world are time zones, the, I guess, perpetually evergreen topic. Yeah, I was about to say, is that really new? I guess it's new right now. That's what you're revisiting. I have them top of mind right now because I've been dealing with localization issues around dates.

So an event happens on a particular day and we log that date and then we want to tell someone when that event occurred. And so we output that date on the user interface and they reach out to us and say, hey, the date is off by one. The issue here is that we're working with a third party API that is saving this event as a timestamp in some unknown time zone and then giving us back also a timestamp where they've truncated all of the hours and minutes. to zero and have also dropped the time zone so that we just get a day part that day part depending on when like where the user is where they triggered the event from and i guess where this api's uh implementation is uh can occur where there's enough of a gap between the time zones that you get an off by one error where the day is different So figuring out a good way to resolve that has been interesting. But then also it's gotten me thinking about a lot of these sort of like follow up questions around dealing with time zones and dealing with dates in particular. Yeah, I have felt similar pain before around dates when you think maybe you can get away with not caring about time zones, but that's not true. I actually have a friend who's currently vacationing in Australia and I was texting with him and he was like, hello from the future because he, you know, from my point of view from North America. Yeah, we are.

texting in real time, but living in different dates, which is always blow my mind. But I can imagine that is extra tough, especially working with a third party API. That sounds really annoying. Yeah, I think the zeroing out of the hours and minutes is probably the wrong thing to do in all situations. You should return either a date object or a timestamp, but not a timestamp where you've modified the time. Because now you're effectively giving me the beginning of the day on the day that this occurred. Now I can use that and just try to get a date object anyway.

But what I'm starting to think is that if you care about sort of localizing things, dates are not the right type to handle something like that, which you probably do want as a full timestamp that has the time zone information. Because even if you know the time zone information for a date, you can't deterministically localize it to a user's setting. So if I give you a date, I say, OK, December 25th, and we captured something on that date in the UTC time zone. In Eastern time, did that event occur on the 24th or on the 25th? Both are possible. And there's no way deterministically to know that. Because if you were late in the evening on the 24th, then it would register as the 25th in UTC. So when you've got something as broad grained as a date, you can't localize it using time zones because it's non-deterministic. And that thought is a little bit kind of blowing my mind. Like, OK, maybe I want, if I do care for localization, I should always be using timestamp. Right. I guess I was wondering how you are supposed to move forward when this is coming from a third party where you're not sure how they're getting that date in the first place.

There are a variety of things. We can talk to the providers. We're in conversation with them. I think this is partly a bug in their API. And so they need to fix it. I think they don't mean to zero out the time. And also, they probably don't mean to not include the zone.

I'm kind of shocked at how many APIs I've worked with that don't use standards when it comes to sending timestamps down. ISO 8601 exists, has existed for 20 years, 30 years. And people still do their own custom string from time thing that's like close to the standard, but not quite. Yeah, that's brutal. When you first were seeing time zone issues, you know, you've been dealing with them. The first question I was going to ask was if the application stores its...

dates in UTC, but you can do everything within your application, but still have to interact with the outside world that may have different presentations of something that, like you said, should be standard. And we've talked about my sort of note-taking system in the past on the show, where I sort of have a small atomic note for each discrete thought I have, and I try to make each title a thesis. And this bug has spawned a lot of new notes in that system, like new thoughts I have about working with time and time zones and dates and assumptions that can and can't be made. So it's been really, really good for me. Nice. What's been new in your world?

I have been in the world of authorization, but one that is very new to me. The project I'm working on now is basically a reimplementation of this kind of application authorization system using software as a service called AuthZ, which creates like an open source tool called SpiceDB, which is a whole authorization system based off of something from Google called Zanzibar. So, so many new terms for me. And I think the main thing that has me... feeling, rethinking how I understand auth is that rather than it being like role-based or identity-based, which is what I'm used to, it's a lot more granular than that. And it's actually more about relationships between resources and the actors that are trying to do something with those resources. It's kind of a whole new paradigm. And I'm having to learn a lot of new ways of thinking about authorization because the needs of the application essentially are kind of at that level of granularity.

To mention, it's kind of a different paradigm. The way you describe it almost sounds a little bit like pundit policies, where you're trying to answer the question, can this actor take this action on this resource? Yes, it is a bit like that. However, I think because they are kind of expecting just to really need like enterprise level authorization granularity, that's kind of why we're moving away from things that are... typically available in Rails libraries and moving towards a whole ecosystem for authorization. And it's basically like a separate database, a separate schema. The difficulty has been figuring out how the relationships are implemented with this new tool, where there can be a lot more different paths to determine whether someone is allowed to do something on a certain resource, and it can get quite complicated. So there's just a lot of...

kind of shifting in terms of how I'm thinking about that. Whereas I think even with policies, it does feel a little bit more easier to reason about from that main resource level. I feel like the sort of main authorization gems that we tend to see in the Rails ecosystem are Pundit and CanCanCan. And they kind of take sort of two different dimensions to look at authorization. Pundit is very resource-centric. We've got a policy for comments. And we say, who can edit this comment? Who can create a comment? Whereas CanCanCan tends to be a little bit more actor-centric. A user who is an admin can do the following things.

Where would you put this offset library? Both. I think it's like, that's kind of what I was trying to explain earlier, where it doesn't prefer one of those dimensions over the other. It's actually about the relationship of both. And it's so flexible in the sense that you, like some actor, have all of these permissions based off of different, just different rules. And they can almost like even conflict. And you then have to figure out just like walking through the relationships, like how is this actor able to access this resource?

And I think that's where it gets quite complex. I actually was thinking about modeling it with a graph. I know that's something we talk about a lot on the show because it seems a little bit easier to think about that way. Whereas those traditional libraries, I feel like I've not had as much of that mental overhead when figuring out, when being able to focus on one side of active versus resource. How have you felt the learning curve has been for that new take on authorization?

pretty high. I mean, to be fair, I've only been doing this for a couple of weeks. But it's not just the paradigm, it's also the technology. And it at times has felt a bit overkill for what our current needs are, but I think the intention is that it's supposed to be kind of a longer-term solution. But the main thing that we're trying to do right now is actually reach parity with their current authorization logic, just using this different technology to implement it. And so since the previous authorization just was implemented with...

I think they were using Pundit. It feels like a lot of effort to just do something that already works. But hopefully this is something that will pay off for them in the long term. I wanted to get your thoughts on a few different conversations I've been having recently. And they feel like a few different isolated conversations, but I think there's kind of an emerging theme that comes out of them. So the first one, a conversation around when do you use JSONB as a column type in your Postgres database? If you're sort of faced with a decision of, oh, do I take this data, put it in a JSONB column, or do I create some sort of schema and...

create out multiple columns or maybe a separate table or something like that. What are the heuristics that you'd like to use to make that decision? I mean, I feel like I've really not had to reach for JSONB columns very frequently, but I would say the times that I have have been when we almost don't really know what that data structure is going to look like and we want to be as flexible as possible. And honestly, I think a lot of the times I've had to face it have been some version of storing configuration settings. So I would say that my heuristic is you probably...

want to avoid it unless you really can't tell what your data is going to look like. So it's interesting that you bring configuration settings because I feel like people bring that up a lot. People are like, oh, let's implement a couple of config settings here. Let's just throw it in a JSONB column. Why are config settings sort of a more compelling case for you? Because that's generally something where you know what the config settings are ahead of time. Hmm. I don't know about that. I think there has been a time when there was like a table essentially representing third-party integrations and those were where configuration settings could be.

variable. And I also think that it would be difficult to model that using a more traditional approach where you are introducing tables for each integration or whatever. I guess I've seen it go both ways, but that feels like one of the situations where you are maybe wanting a little bit more flexibility because you have a lot of different types of things or you kind of foresee like... needing more data and at least like wanting it to be less overhead to kind of, you know, if you have to like add a column every time you are like, oh, actually we now need this additional configuration that is a bit of a pain. So I guess that goes to like the kind of popularity of no schema databases in the mid-2010s. They're like, oh, we don't need to change a schema. When you add new fields to certain objects in your JSONB column, do you go and backfill the existing ones so that they all sort of match each other? Or is it up to the calling code to have conditionals if we're dealing with older data?

I think more the latter. And maybe, you know, obviously I've also seen that become just really frustrating because it almost ends up being that your calling code, your client side code or whatever, then has to parse and or validate the data stored in your JSON. So, yeah, you know, you do end up paying for it, I think. I think I tend to be very skeptical of JSONB columns and of, quote unquote, schema-less data. especially if you want to do anything in particular with it. The moment you want to take actions on that data, you're going to want to have certain guarantees about the shape of it. This reminds me of a more general principle in programming, which is that the more generic or broad your type is, the fewer operations you can take on it.

So a function that accepts literally anything as input, if you don't have explicit type checking code or something inside of it, you're saying, if I get an integer, do this thing. If I get this thing, do that thing. Functions that can work on literally any input, I think potentially only the identity function might be a valid function that actually works on literally anything, which is just you take an input and you return it back out. Because it is so generic that any operation you want to do is not going to work on some of the possible inputs in there. Okay. I just had a potentially spicy thought, which is I think that a lot of people do use JSONB columns when they have to store something but actually aren't using it yet. It's almost, or not even almost, it is, I think, a little bit preemptive because they don't quite know how they're going to use it, but just know that they probably want it. That's how I've kind of, I feel like I've only been on the either the side of, okay, we're introducing the column now to store some data. Let's just use a JSON column. And then far down the line, like, oh, this is really annoying to have to parse and then coerce into a certain something else to be able to use it.

I'm curious then like what else you see as like. an alternative. I tend to put things directly in tables as much as possible. I enjoy having a schema. I know people try to use JSONB as a way to avoid having to think about a schema. In general, you end up having what's effectively an implicit schema, which is the way I use the data. Right. Yep. Which then if it changes, you end up having to have all these conditionals in your code or some kind of schema validation step or something like that. So I like using the database already as a schema tool depending on the types of queries. And I don't know how true this is anymore. I think you can get better performance if you're having to query through something. So let's say you have to join based off of a, let's say,

You're doing a where clause based off of a config value. My instinct is that you can write indexes that will make it faster to do that on a large table if you're, let's say it's just a Boolean, but it might be something fancier if that's its own column rather than indexing into a JSON field. But database engines are constantly getting better. And I don't know that that assumption is true. So I might be wrong here. My gut feeling is that you get better performance for queries if you're using real columns. The idea of an implicit schema is very interesting to me because I think that's just something people don't.

really come to terms with that. Even if you don't have one that's enforced at the database level, you still probably have one that you just have to kind of piecemeal together from the code that consumes it. So that's a really good point. And maybe that's okay up to a certain point, but also maybe it is worth investing in actually thinking about it up front. I think it's especially tricky over time once you start having sort of multiple generations of that implicit schema so that rows that were created a year ago have data in a very different shape than rows that were created.

six months ago, which have a different shape than rows that were created today. So if you're not going back and renormalizing your data every time your implicit schema changes, then your calling code has to sort of like work for all generations of that data. So on the topic of schemas, you had another little discussion in here that I think is also kind of on the same page, the debate between GraphQL and REST. This one's been really interesting because we've been trying to better define some of our best practices internally at ThoughtBot and trying to wrestle with when we would recommend a REST API versus a GraphQL API. And what are the advantages of either? As you mentioned, I'm a fan of schemas. So one of the things that I appreciate about GraphQL is the schema itself, which you can have with REST API. It's just not a thing that comes with it by default. You'd have to provide a JSON schema or something similar. This conversation was triggered by an article that was making the rounds about someone talking about why they left GraphQL after 12 years or something like that. One of those with a bit of an inflammatory headline. But it sort of talks about some of the technical limitations of GraphQL, particularly for server-side implementations. I think this one's interesting because...

People really like GraphQL because it's like, just get the data that you need. At least that's kind of the main selling point that I have always heard. Or just generally kind of to avoid the problem of REST APIs either becoming really bloated with just having too much data or the opposite where it's not quite enough. But when I think about the idea of querying just for what you need, in some ways I think the schema... It just ends up living either on the client side instead of server side. What do you think about that statement? I'm curious what you mean by the schema living client side versus server side. You're suggesting that in a GraphQL API, the schema lives on the server? No, the opposite. So when I think about implementing an API to get certain data, from a REST API perspective, oftentimes are choosing what you want to expose to the client. Whereas...

With GraphQL, you're asking for something specific from the server. And so I think in some ways the... way that it gets determined what data is actually required ends up being either a backend concern or a frontend concern, depending on whether you're using GraphQL or REST. If you're doing GraphQL, it is much more generic because who knows what kind of data the frontend might ask for. And so it does make it harder to do performance optimizations. I think that's one of the common frustrations of people implementing GraphQL resolvers on the backend. It's very easy for these sort of generic things that compose to trigger situations where you have n plus one queries or things like that.

The article that had been making the rounds mentioned that there's several sort of vectors in there to effectively create denial of service attacks where you put in like really simple queries that can be sort of infinitely expensive to resolve on the backend. Have you found instances where you have liked using GraphQL over REST or are you a REST head? I don't know. I just came up with that term. I like the schema aspect of GraphQL. That's really nice. And I think the thing that's interesting with GraphQL is the tooling that goes around it that you get sort of by default.

with GraphQL or a little bit by default with GraphQL for something like schema. And then the JavaScript community in particular, but some others as well have built a lot of really robust tooling around consuming GraphQL APIs, which makes that really nice. So it's less about the protocol itself being good, but more I think that the community around it has created some really useful tooling that makes it nice to be a consumer of a GraphQL API. I've done less work as a sort of producer or like server of a GraphQL API. My understanding is that that gets really annoying pretty quickly. Yeah, that makes sense. Well, when you were saying that you were really liking the schema, I guess maybe what I was trying to get at earlier is that.

that is a byproduct of GraphQL? The graph is not the interesting part of GraphQL for me, ironically enough. And you could get all these things out of a nicely done REST API if they provided a schema and maybe if there was a little bit of better tooling around a JSON schema and things like that. It's funny because with the graph parts, I think are really just kind of shaping things to be more similar to objects and relationships, which if you are thinking in terms of RESTful resources, you kind of already have a little bit. So it makes sense that it's like, that's not the compelling part to you.

I think the one aspect of the graph that can be compelling is avoiding needing to do round trips to the server. So if you're getting data from two different related resources, instead of making a request at one endpoint and then a request at another endpoint and composing the values together, you might be able to do it all within one web request. So maybe you get some performance benefits there. Although if you can do them in parallel and with HTTP2, I don't know if that becomes an issue still. Yeah, I was just about to say that that composition can also happen server side, I think. So again, maybe a byproduct, but...

Not one that's exclusive to that tool. I want to come back to this, but I also wanted to bring up the third conversation that came up. And this was, and this is maybe a controversial one. When building a Rails app, how do you decide how to pick your front-end technology? Do you go just standard Rails views? Do you do Hotwire? Do you do something like React? And more specifically, because I think common wisdom is that pick React if your front end is really interactive and then stay simpler for less interactivity. What are the heuristics for how you determine when you have enough interactivity or when it's worth the cost of bringing in React? Because React can be a really expensive thing to bring into a Rails app. Yeah. It's funny because I think that was like the out-of-the-box default choice for a long time.

when there wasn't a lot of complex interactivity. And I think that might have just been just the trend at the time many years ago. But when I think about applications that have needed React that I've worked on... I think a lot of modals, but I don't know. And sometimes I almost think they kind of end up influencing each other because if you do have React already as part of your application, you tend to then build or design more features in kind of a single page application type way. Whereas...

it doesn't need to be that way. And if you didn't have a, like you said, more costly front-end framework, you would naturally lean away from a product design level. So it's hard to say, when would you bring it in? Because I almost think that that decision was already made at the beginning. Right, right. If you're in a project that already is using React everywhere, it's very hard to avoid it. That's just a, to use a term from the Sustainable Rails book, it's a carrying cost that you just accepted. And I think that carrying cost can be shockingly high.

If a feature requires front and back end work, it can be almost, I sometimes say two and a half times the cost of a regular Rails feature because you need to implement it on the back end. That's once. You implement the same-ish thing on the front end. That's twice. And you need the data transfer API work in between the two. That's like another half. That's sort of my rough back of the napkin math around that. I will say that I have worked in a lot of apps that still have both just kind of conventional Rails views and then have layered on React as a front-end framework for just some more of their newer or snappier features. And as much as people complain about, oh, we still have this ERB page lying around, I actually don't think that's like that.

terrible because it it's like showing like yeah you can you know have your application mature that way and you know, when those parts of the application maybe need a redesign or something like that, then you can consider moving it over. But I think that is a very good way to handle that, like, you're probably not going to need it until you do. The pain of that, I think, is just part of the work. I don't think that's necessarily a good reason to say, oh, we should have started with React. Right. I would generally argue against starting with something like that just because unless your first few features are highly interactive, that's not going to, that's just going to slow you down at the beginning.

It sounds, though, for something that has some interactivity but not necessarily on every page, your ideal compromise is a mix where you're rendering not just an app that then you interact with everywhere, but some pages are static and then some render an app. Yeah, I've seen that work quite well, actually. And I am curious, though, from I think on a recent project that you were on, you were working with Hotwire. How much JavaScript were you dealing with in that situation? And do you think, did you miss it? So on that project, I did almost no JavaScript. It's pretty shocking how far you can get writing just...

things that work natively in the browser, a couple of DOM attributes from Hotwire, and then occasionally you need to add a little bit of JavaScript for some fancier things. But for most things where you're just like, oh, I want to click a button and something comes into view or I get a pop-up or something like that, you're really just writing regular ERB. Everything else is effectively just progressive enhancement on top of it. So it ends up being really minimal. I think allowed us to move much faster than if we had to do a traditional front-and-back-end setup.

So I want to like zoom out a little bit now. We sort of looked at the three questions. I think it was interesting that these three separate conversations happened within a pretty short period. And they're all sort of sitting in the back of my mind. And I think I'm realizing that my instincts for where I would default on each of those choices or where I would start with is maybe different than where a lot of people do. So I tend to maybe avoid a JSONB column unless there's a really good justification for it. Much as I like schemas, I think I would default to a REST API over GraphQL, again, unless you can justify it. And then similarly, I would avoid bringing in a React frontend at first until, again, you can justify it. So my sort of starting point on all of those, I think, is different than where a lot of people are. Yeah, I'm curious if we had a JavaScript.

developer answer these questions if they would answer them completely opposite? Quite possibly. The React one might just be familiarity, and that's fair. The other two, though, I think are interesting in that when I work with JSON, I treat it as a sort of serialization format that is inherently untrustworthy, that needs to be parsed out. Whereas in JavaScript, it's just those are the native objects that you work with. So there's much more, I think, comfort with just taking JSON and treating it as objects you can work with, trusting it in a way that I'm often deeply uncomfortable with. And so...

For me, a JSONB column is like, oh, there's some sort of serialization step happening. In a JavaScript app, that's just your live objects get persisted to the database and they come back to you. There's no effective serialization step happening there. Yeah. I wonder if that is also true for people who are using JavaScript on the server side. And we didn't even really talk very much about.

Like a lot of the ways to enforce those things, I think maybe at least from the ecosystem that we work in, it does feel a little bit more, like you're saying, untrustworthy. I often don't see as much enforcement in terms of like the schemas and types. It's not that it doesn't exist in the JavaScript ecosystem. It definitely does exist. Two popular libraries for doing this. There's one called Zod and one called IOTS. People popularly will use to validate their inputs if they're getting JSON from a outside API or something like that.

I assume if you're reading from a JSON column in a database, you might want to apply something like that as well. Some of that might be a bit of a typed versus dynamic programming, which is interesting because JavaScript sort of contains both extremes, where you've got the TypeScript people that are much more structured. Anybody coming from a more functional programming background is all about making sure that your data transforms properly. And then you've sort of got your more vanilla JS, it's data, just trust me, it'll work side of things. It's big enough that there are many sort of sub-communities within it. So I'm curious if you think that there is some kind of theme with all these questions that have been coming up.

like why we're having a lot of these debates right now. I think it's based on a lot of people's experience with the way they tend to work with code. And so, for example, GraphQL is something where, especially I think some of the tooling in the JavaScript world, such as Apollo, is really quite good. And so that creates a virtuous feedback cycle for GraphQL is the best, maybe not because it's the best like transport technology, but because on the consumer side, the tooling is so good that it makes it the best choice for you or for your application. Yeah, that's really interesting that...

people maybe feel that way from a, it's like, this is what everyone is using. And that virtuous cycle of like the tooling is very good. I hear other people talk about it. And so I think it's a good choice. But then I think right now in particular, a lot more alternatives are starting to exist and people are leaning a little bit away from some of those like really complex front end.

web applications. So it's kind of interesting to see that those tooling choices end up being a bit more, not like questioned, but just maybe more so like, is this what I need? It's definitely worth it to sort of question your technologies every now and then, which is a little bit of what I'm on the journey that I've been on the past couple weeks. Tooling is an interesting situation though, where would you prefer to work with a technology that is maybe not as good, but has better tooling or a technology that maybe technically is better, but in practice is more annoying to use because the tooling is subpar? I guess another thing I was kind of picking up on in this conversation is that I think you end up having to build a lot of tooling in this ecosystem because it actually isn't the right choice. I feel this way about React sometimes where it's like its own universe and sometimes doesn't even play well with native browser APIs. And so then you have to build more things to kind of interact with that the way that you need it to. In some ways, you're just like, okay, I have to go all in on this and even just to make the application work the way I want it to.

whereas maybe you would get some of those things a lot more easily if you didn't, you know, kind of live in React land. So I'm curious for you, where would you land as your sort of default on all those three questions? I think I am maybe a little less skeptical of JSON-B than you are. Sometimes I have chosen it and it's fine, but I also, you know, didn't always have to live with those decisions. Front-end framework-wise, if I got to choose, like purely just for myself, I think I would not use a front-end framework at all. But I feel like...

Again, that kind of progression from Rails templates to eventually being like, okay, we're ready now to build some really slick UI features. That might be the time that I would introduce React. And then, yeah, like GraphQL, I have tried it a couple of times. And I don't know, it's been not so fun for me. I feel like you end up having a lot of, a lot more, I don't know, like boilerplate and just kind of prefer your traditional REST API. And even for things that don't necessarily seem RESTful, I think you and I have talked a lot about ways.

that we can model it to be more restful. And that is an exercise that I really enjoy. Is this experience been on the client side consuming a GraphQL API that's been frustrating or on the back end where you're producing or like resolving one? Resolving, resolving it. So maybe that's where that opinion is also being informed. I think that's consistent with a lot of what I've heard. People enjoy consuming GraphQL APIs, but don't like producing them. On that note, shall we wrap up? Let's wrap up.

Show notes for this episode can be found at bikeshed.thoughtbot.com. If you enjoyed listening, one really easy way to support the show is to leave us a quick rating or even a review in iTunes. It really helps other folks find the show. If you have any feedback or questions, you can reach us at hosts at bikeshed.fm via email. Thank you so much for listening to The Bike Shed, and we'll see you next week. Bye!

Frequently asked questions

How long is this episode of The Bike Shed?

This episode is 39 minutes long.

When was this The Bike Shed episode published?

This episode was published on December 10, 2024.

Is there a transcript available for this episode?

Yes, a full transcript is available for this episode. You can read the complete transcript on the episode page.

Can I download this The Bike Shed episode?

Yes. Use the download control on the episode player to save the publisher-provided media file.