spotifyovercastrssapple-podcasts

elm-graphql

We talk about elm-graphql, how to organize your SelectionSets, and other best practices.
November 2, 2020
#16

How can elm graphql decoding fail?

Backend Frameworks for Full-Stack GraphQL Type Safety

Transcript

[00:00:00]
Hello, Jeroen.
[00:00:02]
Hello, Dillon.
[00:00:03]
How are you doing today?
[00:00:04]
Pretty good.
[00:00:05]
How about you?
[00:00:06]
I'm good.
[00:00:07]
And well, we've made it 15 episodes without talking about Elm GraphQL.
[00:00:13]
That's surprising.
[00:00:15]
Yeah, we've had a lot on our minds.
[00:00:19]
So we've gotten to talk about a lot of good stuff.
[00:00:22]
But there's a lot to say about Elm GraphQL as well.
[00:00:25]
And we will do that then.
[00:00:26]
As some of the people in the audience may not know, Dillon is the author of Elm GraphQL.
[00:00:31]
That's pretty much how I learned about you, watching your talks about it.
[00:00:36]
Maybe let's get into it and can you tell us what Elm GraphQL is?
[00:00:39]
So what GraphQL is, is it's a schema for your API.
[00:00:44]
So it's just describing all of the endpoints that your server can return.
[00:00:49]
Now, I like to break it down into two pieces.
[00:00:52]
The name GraphQL has two parts.
[00:00:54]
It's a graph and it's a query language.
[00:00:57]
So the query language is the part that it's a schema that describes what you can query for
[00:01:02]
and the types of those values that it will return.
[00:01:04]
The graph part is a whole nother piece.
[00:01:06]
Essentially, the graph part is that rather than making several follow up requests,
[00:01:12]
when you get one bit of data and you say, okay, now that I know the ID of all of the users that I'm connected to,
[00:01:19]
I want to find their first names and get their avatar,
[00:01:24]
so I need to make a follow up request to get that information.
[00:01:27]
Or you sort of throw in a bunch of additional information into that rest endpoint
[00:01:32]
because you know that you very frequently get that information together.
[00:01:36]
It's a way of sort of traversing a graph to get all of the data that you need in a single request.
[00:01:42]
So that's one of the goals of GraphQL is to solve the underfetching and the overfetching problem.
[00:01:49]
You can fetch exactly the data you need and you can traverse these relationships.
[00:01:53]
And it's quite a nice way to sort of build up data and evolve an API where you don't need to know exactly
[00:02:00]
how you're going to get the data at first.
[00:02:02]
You can sort of start exploring data and the types are all known.
[00:02:06]
So one of the really interesting things about GraphQL is that it has a complete specification of all of the type information built into it.
[00:02:14]
So any GraphQL server, you can ask it what are all of the data types and all the things I can query for,
[00:02:20]
and it will respond with that information.
[00:02:23]
Is that this schema that it gives you in one giant response or?
[00:02:27]
Yes, it's actually like these magical little queries that you can do in GraphQL syntax.
[00:02:35]
So like you can in any context, you can ask for the type of something and it will tell you, you know, this is a person object.
[00:02:44]
This is an ID, you know, you can query for those types.
[00:02:48]
So those little introspection, those introspection requests are built into the GraphQL query language and you can,
[00:02:56]
a server will respond with that information.
[00:02:58]
So that's what Elm GraphQL does.
[00:03:00]
That's where Elm GraphQL comes in is it uses that introspection information to ask the server what data can I get?
[00:03:08]
And then it generates an entire API for your server.
[00:03:11]
So like the way I think of.
[00:03:13]
And in this case, we talk about the library API or module API.
[00:03:17]
Yes, good distinction.
[00:03:19]
Exactly.
[00:03:20]
So it takes your server's API and it builds an Elm API at DSL domain specific language for interacting with your API.
[00:03:30]
So like I think of it kind of like, do you ever see these libraries that will be, you know,
[00:03:36]
like a Ruby library for interacting with the GitHub API or a node library for consuming this API?
[00:03:43]
And it's kind of custom tailored to, you know, have these functions for interacting with it in a more high level way.
[00:03:50]
That's essentially what Elm GraphQL does is you point it at your GraphQL API and it gives you one of those libraries.
[00:03:58]
That's basically what it does for you.
[00:04:00]
And so it does that by just generating a bunch of functions that define something called a selection set.
[00:04:06]
So that's sort of at the core of Elm GraphQL is this concept of a selection set.
[00:04:11]
Yeah, just the general look and feel of the API that is generated kind of looks like Elm HTTP with decoding, kind of.
[00:04:20]
We will get into details, but that is just what the people or the audience should have in mind, I guess.
[00:04:26]
Right. And then it looks a lot like building up a decoder, which you can do in a pipeline style or like the map and functions, map three, map five functions.
[00:04:35]
So building up an Elm GraphQL selection set, which is this GraphQL concept of the data you're querying for.
[00:04:44]
But in Elm GraphQL, it allows you to query for that data and then it knows how to decode that into Elm data.
[00:04:50]
So you can think of it like a JSON decoder, except it knows how to query the server for that data.
[00:04:57]
And it knows it's going to get the data because it knows that the underlying types are a string and a float.
[00:05:03]
So it looks a lot like building up a JSON decoder and then sending it with HTTP.
[00:05:08]
All right. So with the JSON decoder, you always say, I want this field and I assume that it's a string.
[00:05:15]
But what you're saying here is you say, I want this field and in GraphQL will well, Elm GraphQL will tell you this is a type that is expected.
[00:05:24]
You don't have to specify it yourself.
[00:05:26]
Exactly, exactly.
[00:05:27]
So maybe to compare, if we were getting an object from a REST endpoint that had a person with a first name and a last name,
[00:05:36]
then you would maybe do decode.mapTo and then decode that in.
[00:05:42]
You'd give a constructor for a person type alias record, which has a string first name, string last name.
[00:05:49]
Then you would do you'd have decode.fieldFirst, decode.string, and then you'd have decode.fieldLast, decode.string.
[00:05:59]
So you're actually when you're doing that, you're stating several assumptions.
[00:06:03]
You're stating the assumption that it's going to exist at this level under something called field, under a field called first and a field called last.
[00:06:13]
And the type of those values are going to be string and they're not going to be null.
[00:06:17]
And so you're stating several assumptions there.
[00:06:20]
And if any of those assumptions are incorrect, then you get a decoder error, which is awesome.
[00:06:26]
We've discussed that on our JSON decoders episode that that's an awesome feature of Elm that it can fail fast
[00:06:34]
instead of getting way into the far reaches of your application and giving you a mysterious error.
[00:06:41]
Well, it's better than to have a decoding error than to have a runtime error.
[00:06:46]
Exactly. Where you sort of core some type and you add it to something and you're like, how did this turn into null or why is this empty string?
[00:06:53]
That's great. But if we have a schema that describes our entire API, we can do better.
[00:06:59]
And that's that's the goal of Elm GraphQL is what if we could know that those assumptions were correct?
[00:07:05]
Not when we run the decoder, but when we compile, it will actually tell us.
[00:07:10]
And so so what would that query to get a person's first and last name look like in Elm GraphQL?
[00:07:16]
Well, in a nutshell, the way you build up that decoder, the selection set for that person would look like instead of decode dot map to you could start with selection set dot map to.
[00:07:28]
And then you give the person constructor and then you have person dot first person dot last.
[00:07:36]
Now, what is person? Person is generated code from Elm GraphQL and it's going to have something called API dot object dot person.
[00:07:48]
That module is going to contain a top level value called first, which is a selection set of type string.
[00:07:55]
Which will query the first field in a person.
[00:08:00]
Exactly. It contains all of those things that we were making assumptions about when we were writing our JSON decoder.
[00:08:07]
It contains that information. It knows it knows how to it knows where it's going to find that data that comes back from the server when it comes back.
[00:08:16]
And it knows how to it knows what the underlying data type is going to be.
[00:08:20]
And we also use it to send off the query. But those assumptions are no longer assumptions.
[00:08:26]
Those are that knowledge that is contained in the generated code.
[00:08:31]
Yeah, I think that as Elm developers, we really want to move things to to be compiler errors rather than runtime errors or decoder errors or just false assumptions that you have to handle it some way.
[00:08:44]
And I feel like Elm GraphQL was one to me, at least one of the few that pioneered type safe APIs that are generated to be that way.
[00:08:54]
Yeah, it's been cool to see like there definitely been some innovations that use code generation.
[00:08:59]
And I've seen some like call outs to Elm GraphQL.
[00:09:02]
And I think that the domain of GraphQL was ripe for some sort of nice integration with Elm because it's just you've got this type information.
[00:09:13]
It's there. It's like, you know, if you if you pull up the interactive like GraphQL query window,
[00:09:20]
you get this nice auto completion where it knows what values are there and you can look at the documentation in line and that information is there.
[00:09:29]
And yet it's sort of separated in this different world that we can't access.
[00:09:34]
And that's sort of the premise of the talk I gave types without borders is, you know,
[00:09:41]
you've got this line dividing this beautiful type of information that you'd like to have and why not bring that into the Elm world so we can have access to that information.
[00:09:51]
But yeah, as you say, that's GraphQL is one application of that idea.
[00:09:57]
But that idea itself can be used in so many ways.
[00:10:02]
Yeah, we mentioned that first like person dot first was a selection set function, a function that returns a selection set, which goes to which fetches a first field in a person and is of type string.
[00:10:17]
Yeah. And it just to clarify, it's a value in that particular case.
[00:10:22]
I mean, it depends on whether it has arguments or anything like that.
[00:10:26]
But assuming it has no arguments in the simplest case, it would be a top level value just to use the precise terminology there.
[00:10:32]
But you cannot use that person the first somewhere else.
[00:10:37]
You can only use it when you create your selecting a person.
[00:10:42]
That's right. Exactly. So that's that's the concept of a scope, which I actually just just in time for this episode, I made a documentation change.
[00:10:53]
So in the docs, there used to be a type variable, which some people may have seen called type block.
[00:10:59]
And I actually wrote that I wrote that name the very day that I discovered phantom types because I didn't know what they were.
[00:11:07]
And I was just amazed that like, oh, I can lock the type like this.
[00:11:11]
So I called it type block and I never really thought of a better name until recently.
[00:11:16]
And so finally I changed. But so a selection set has two type variables.
[00:11:21]
The first type variable is what it decodes to, just like a JSON decoder.
[00:11:25]
It's a decoder of some type variable. So a decoder of string, a decoder of person in Elm GraphQL.
[00:11:32]
It's the exact same thing. A selection set has a first type variable of the type it decodes to.
[00:11:38]
The second type variable is the scope of the selection set.
[00:11:41]
And as you say, so this is this is where sort of the graph part of GraphQL comes in.
[00:11:47]
If you have at the top level, let's say you can make some requests to get the current version of the app.
[00:11:54]
So you you say current version and it gives you a hash or a version number or something.
[00:12:01]
So that is a top level query. So in raw GraphQL syntax, what that would look like is usually people will explicitly write out the word query and then curly braces.
[00:12:13]
It looks a lot like JSON, and that's the intentional design of GraphQL is it looks like JSON, except it's not the key value pairs.
[00:12:21]
It's just the key. It just looks like the keys. And that's like the data you're requesting from the server.
[00:12:26]
So you would say query curly braces and then build version or whatever that field is called.
[00:12:33]
And that would be saying I'm requesting this top level value from the server.
[00:12:37]
And that would have a type of string, for example. And so that's like the simplest top level query.
[00:12:44]
But now if you're saying if you're getting a person, now you need to specify, well, what information do you want to know about that person?
[00:12:52]
And that is where you get this sort of graph part of GraphQL, where you're traversing these relationships and you're fetching data within other bits of data.
[00:13:02]
So you're saying I want, you know, let's say you want like the current user, you want their first and last name to show in the top right corner.
[00:13:11]
Then you would say so instead of query curly braces build version, you'd say query curly braces current user.
[00:13:20]
Now that's getting the current user, but the current user is not a simple string.
[00:13:25]
Yeah, it's not a primitive.
[00:13:26]
Yes, right. It is an object. And so within that object, now you need a nested selection set to tell it what fields you want from that object.
[00:13:36]
And that's where you you open up some new curly braces and then you say, well, I want the first and last.
[00:13:43]
So that would look like query open curlies, current user, open curlies, first new line, last and then close all the curlies.
[00:13:54]
No commas between first and last?
[00:13:57]
Commas are optional.
[00:13:58]
OK, good to know. Well, not good to know because I use Elm GraphQL at work.
[00:14:03]
So right. Exactly. And it abstracts away those details.
[00:14:08]
And Elm GraphQL actually abstracts away a few other details about the sort of low level GraphQL syntax.
[00:14:15]
For example, in in GraphQL, if you're so you can pass arguments to an object like if you if you want
[00:14:23]
a person with some ID.
[00:14:26]
Exactly. If you want to do find person, you can pass it an argument.
[00:14:30]
Now, well, what if you're saying find person with ID one and find person with ID two and you want both of those.
[00:14:37]
Right. Well, the way that GraphQL works under the hood that I say under the hood, because as an Elm GraphQL user,
[00:14:44]
this is an implementation detail that you don't deal with.
[00:14:48]
But that JSON data, you're going to get a JSON response. And when you say find user.
[00:14:55]
So I was saying that it's intentionally designed to look like JSON, but just the keys, not the values.
[00:15:01]
Well, the responses come back in the same shape.
[00:15:05]
So if you request first name, that JSON object is going to come back in the key first name.
[00:15:12]
Sorry, if you say find person, that JSON object is going to come back under the key find person.
[00:15:19]
But you're doing find person twice. And so there's going to be a collision and you get a GraphQL error.
[00:15:25]
Is that a problem that people write GraphQL manually have?
[00:15:28]
That will certainly be a runtime error that the server will fail.
[00:15:33]
You may have like a linter that helps you with that or that sort of tool.
[00:15:38]
But with Elm GraphQL, you don't even have to think about it. So it's one thing if we can make those errors impossible,
[00:15:46]
like Elm GraphQL generates an API that you're going to use things the right way.
[00:15:51]
That's one way to do it. Another way to prevent errors is by abstracting away those details and taking care of them for the user.
[00:15:58]
In this case, that's what Elm GraphQL does.
[00:16:01]
So the way that you solve this problem of a collision where you're requesting two fields with the same name is you need to give it a field alias.
[00:16:10]
And so you would just use the syntax where you say, you know, person one colon find person ID one and then person two colon find person ID two.
[00:16:20]
Now it's going to come back under the JSON key person one, which was the alias you gave it or end person two.
[00:16:26]
But Elm GraphQL takes care of that for you so you don't have to think about it.
[00:16:29]
And that's why in the FAQ in the Elm GraphQL repo, I talk about these mysterious ID numbers that show up in all of the fields.
[00:16:41]
So you may notice those. That's what that's doing.
[00:16:43]
And I wrote an article talking about like the design of that and some of the I talked about how Elm sort of pushes you towards simpler design
[00:16:51]
because it's harder to have sort of imperative state in your code.
[00:16:55]
So we'll share a link to those and people who are interested to learn more about that can take a look.
[00:17:00]
So one problem I often encounter when I play with Elm GraphQL is that I'm looking for data on person, for instance.
[00:17:10]
So what I do is I do current user.
[00:17:13]
And we totally got sidetracked, didn't we?
[00:17:15]
We forgot to mention that scope type variable prevents you from trying to request something at the top level.
[00:17:23]
That was the original point.
[00:17:25]
So maybe let's wrap that up real quick and then get to your point there.
[00:17:28]
Which is related anyway.
[00:17:30]
Okay, good.
[00:17:31]
So the scope type variable, there are two type variables.
[00:17:34]
There's what it decodes to.
[00:17:35]
That's the first type variable and the second type variable, which used to be called typelock.
[00:17:40]
And it's now called scope, which I think better reflects its purpose.
[00:17:45]
So the scope, as you were talking about, if you request this top level thing of the build version, which is just a simple built in string type at the top level.
[00:17:56]
It's not a nested object query.
[00:17:58]
Now, the type of that is going to be it's a root query.
[00:18:02]
But then once you so that would be for the build version, the scope type variable would be the root query.
[00:18:09]
But for a person selection set, the scope is going to be person.
[00:18:14]
And so the way to think about that is if you try to get first name at the top level scope, it doesn't make any sense because you're getting it from that person object.
[00:18:25]
So you can get current user, open curlies, first name.
[00:18:29]
You can do find user ID one, open curlies, first name.
[00:18:34]
So in the scope of a user object within the curlies in the find user or the current user, you are in the scope of a person selection set.
[00:18:43]
Of a person up.
[00:18:44]
And so you have access to fields in that scope.
[00:18:47]
So that's what that second type variable in a selection set represents is that scope.
[00:18:53]
Yeah. And what it helps you prevent is you asking for fields that do not exist on something else.
[00:18:59]
Exactly. Without that, Elm GraphQL would not really be able to achieve its promise of preventing invalid GraphQL queries or at least giving you a compiler error if you try to do that.
[00:19:11]
Yeah. It would give you a decoder error.
[00:19:15]
Exactly. Yeah. That's a key ingredient.
[00:19:17]
OK, so did that cover your point or was there something else you were going to bring up?
[00:19:21]
I was going to bring up something else, but that might actually just be me.
[00:19:25]
So you have that you want to have the first name of a user of a person.
[00:19:32]
So, for instance, you do current user.
[00:19:34]
Current user is a selection set on person where the scope is person.
[00:19:40]
And then you will pass in person dot first and person dot last person dot age, whatever.
[00:19:46]
But the scope is a object that the API that the Elm GraphQL CLI tool will generate for you.
[00:19:55]
But it is not related to the module that defines it.
[00:20:01]
Gotcha.
[00:20:02]
I always find it a bit tricky to connect both in my mind.
[00:20:07]
So I have a scope and I have the selection sets on that scope.
[00:20:13]
Yeah, they're in very different places.
[00:20:15]
Right. This is a good question.
[00:20:17]
And this is sort of this is one of those like little workflow things that I like to share with people when I, you know,
[00:20:24]
when I teach my Elm GraphQL workshop, these little workflow tips can make a big difference, I think.
[00:20:30]
And so this is like one of those little details that I like to share in my workshops.
[00:20:34]
This is worth money, people.
[00:20:36]
This is a for money workshop.
[00:20:38]
Is there any other kind? I guess so.
[00:20:42]
And so, OK, you had you have these two different values.
[00:20:47]
So you've got a module, as you were saying, there is this API object that person module.
[00:20:53]
And this is where you get the first name field and the last name field.
[00:20:58]
These are defining these selection sets that you can get in that scope.
[00:21:02]
Now, the technical reason why that magical type variable scope is using a value that's not in the API dot object dot person module is because it would lead to circular dependencies.
[00:21:21]
So it's not possible to do that.
[00:21:23]
However, I'm happy with the way that you work with it, but I think there's a little trick that helps to make it nice to work with.
[00:21:32]
So the trick is if you have a so when you say find find person, you've got, you know, so this is at the top level.
[00:21:43]
So it would be query dot find person, you know, API dot query dot find person.
[00:21:48]
And then you have find person takes a selection set of the nested selection set.
[00:21:56]
So what fields would you like to select on that person?
[00:22:00]
Right. So first, you give it the required arguments.
[00:22:03]
So you'd give it the required arguments of the ID for the user you're selecting, ID one.
[00:22:08]
And then the second argument you would give that function for find person would be the nested selection set.
[00:22:16]
And so the type of that is going to be selection set decodes to anything you can decode to whatever you want it to.
[00:22:23]
And then the scope must be API dot object dot person.
[00:22:29]
API dot object dot person. And the scope is API dot person.
[00:22:34]
The scope is API dot object dot person.
[00:22:36]
To also. Right. Yes. OK. So here's here's how that works.
[00:22:41]
It's not API dot object dot person dot person because it doesn't exist in the API dot object dot person module.
[00:22:48]
It exists in a module called API dot object, which contains all of those magical scope type variables.
[00:22:55]
Right. So if you want to find what fields you can use, yes, what functions you can use, you just go to that module.
[00:23:04]
Exactly. Same name as the scope. Exactly.
[00:23:08]
The name of the scope is going to look exact. It's going to be the exact name of the module, even though it's in a different module.
[00:23:16]
It's in the API dot object module, but it's called API dot object dot scope, API dot object dot person.
[00:23:23]
Now, if you do API dot object dot person, that module contains all of the fields you can use in that context.
[00:23:30]
So the scope API dot object dot person means that you can do API dot object dot person dot any field name.
[00:23:38]
So it's a bit difficult to explain that without showing code. But but hopefully that that wasn't too confusing.
[00:23:45]
It makes sense to me at least. But I think that in our case, my workplace, we're confusing ourselves by doing a lot of aliasing for the module names,
[00:23:54]
which might not be always a good idea, especially if you rename it like it's not always person, it's user, for instance.
[00:24:01]
API dot object dot person alias as user. Yeah. And then you're you're confusing yourself.
[00:24:06]
Interesting. I mean, at that point, I would be wondering, I mean, maybe it's hard to make a breaking change to the schema,
[00:24:12]
but I would be considering changing my GraphQL schema at that point.
[00:24:16]
Well, yeah. Or maybe it's just some discipline we need to get around aliasing our modules.
[00:24:24]
Right. One of the techniques that I think is really important is these selection sets.
[00:24:30]
In one sense, it's it's pretty high level. You know, it's it's not as low level as just making a GraphQL query.
[00:24:36]
But in another sense, it's very low level, much in the same way that JSON decoding is low level.
[00:24:43]
And as such, it belongs in a module most of the time.
[00:24:47]
You would move your selection sets in a different module?
[00:24:51]
Most of the time, yes. Like if it's a person now, if it's like a toy example and you're just fetching a person and showing their first and last name.
[00:25:00]
OK, but in a real non toy code base, what's going to happen is there's going to be a lot of intricate logic on that.
[00:25:08]
And you're going to have you know, you're going to have user settings and you're going to have all these details that you need to manage.
[00:25:16]
And I think it's a really good practice to start finding nice ways to organize these into modules.
[00:25:23]
So modules next to where you would use them or modules that you would share around the rest of your code base?
[00:25:30]
Modules that you would share. And I think it's really nice to encapsulate.
[00:25:34]
So like let's take a person so you can encapsulate that into, you know, let's just call it a person top level module called person.
[00:25:43]
Well, that's going to be confusing for aliasing.
[00:25:47]
Well, but you've encapsulated all of the selection sets that deal with API dot object dot person to within that module.
[00:25:56]
Unless you also need the person, the API dot object dot person module for other things.
[00:26:03]
If you move everything related to selection sets into that module, then I guess that problem is solved.
[00:26:09]
I think so. And I mean, the seams might not always be perfectly clear, but I think this is the general direction that's very nice.
[00:26:17]
You have it. So, you know, just like you would have I don't know, maybe you've got like an article and then you have, you know, the body of the article and the title and the author.
[00:26:28]
So if the author of the article is a person, then you need to make a selected and a nested selection set for that article to say, OK, I want to get the author of the article.
[00:26:39]
Well, what fields do you want? And then you need a nested selection set to say, I want the first name, last name.
[00:26:44]
Well, you can delegate that to the person module that you defined to encapsulate that Elm GraphQL selection set.
[00:26:52]
So you can say, you know, you have module person. It exposes selection. You know, you have a person dot selection, which is of type selection set.
[00:27:01]
And now that's going to fit in. So when you when you have, you know, an article module that encapsulates your selection sets for fetching an article, then you're going to say, well, get the author.
[00:27:15]
And then you're going to pass in the nested selection set, which is going to be person dot selection. That's something you defined in your own module that encapsulates the selection set.
[00:27:25]
OK, so you would have a personal selection that takes a lot of arguments.
[00:27:30]
A person dot selection wouldn't take any arguments because you're passing it to it's a it's a selection set that's describing here's what I want from a person. And then when you say article, I want the author.
[00:27:46]
That's described. So, see, that's the cool thing is you've got this module that sort of encapsulates how do I get a person?
[00:27:53]
And then you've got this this article part is describing where to get the person from. Right. So that's like the graph part of GraphQL.
[00:28:01]
And so you can just sort of say, like, OK, well, here's the query for a person that's in the person module you defined person that selection.
[00:28:09]
And then, well, what person do you want? Where do you want the person from? Well, that's the relationship. It's the person who wrote this article.
[00:28:16]
Now, there is that does make it sound a little simpler than than it turns out to be in reality, because oftentimes you have if you're getting the current user, then you want to get you know, you want to get their avatar and maybe some additional details that for for the author of a post.
[00:28:36]
You know, maybe for for the author of an article, you want to fetch the number of articles that they've posted or other articles that they've posted.
[00:28:44]
So you do have to get creative with the way you define those those things.
[00:28:48]
Because you don't want to fetch or underfetch most of the time. Right. Yes.
[00:28:53]
You want to avoid over and under fetching and you want to find the right seams for for sort of having the responsibility of the person where, you know, that that sort of describes the core details like, you know, maybe the logic for how you display an avatar.
[00:29:12]
For example, you could encapsulate that in the person module.
[00:29:17]
So that's view logic. And you could use an opaque type to have a person and you know how to render a person batch.
[00:29:25]
And you can render that at the bottom of an article. You can render that, you know, in the nav bar for the current user or maybe maybe like an author and a user are two different types of things.
[00:29:38]
You know, so you need to get creative with these things and you may need to, you know, you can you can expose different types of selection sets to you can expose within a person module.
[00:29:50]
You could expose the bare bones selection set and the selection set that includes some additional information for the for the nav bar display.
[00:30:00]
And so it's not like a one size fits all solution. This is just data modeling techniques and module design.
[00:30:09]
But my point is that I think that too often people think of, you know, an Elm GraphQL selection set as just fetching data and as like a high level unit.
[00:30:20]
But it's really not. It's something that should be encapsulated and you should build your own types that are appropriate types for your domain.
[00:30:29]
You should use all the same good data modeling techniques and everything that you would use with a JSON decoder and encapsulate those details.
[00:30:36]
Yeah, you don't have to keep the data that you get from the GraphQL endpoint as it is.
[00:30:42]
You can transform it using selection set dot map and stuff like that into the main model that you want and that you will use for reviews and for your update functions.
[00:30:52]
Exactly. That's the beauty of, you know, of this sort of approach in Elm of like this decoder style.
[00:30:59]
And, you know, yeah, we talked about this in our like JSON decoding episode that you can you can make changes at a local level.
[00:31:07]
You can, you know, within a certain JSON decoder subsection, you can map.
[00:31:12]
Whereas with JavaScript, what often happens is you just say like, all right, give me all this data.
[00:31:18]
And then you get back a giant, deeply nested JSON object.
[00:31:22]
And then you pass that into some function that transform that does a bunch of transformations on that whole object and becomes really unwieldy.
[00:31:28]
It's so much easier when you can just do a precision surgery in that one spot that you want to change with a map.
[00:31:35]
And that works the exact same way in Elm GraphQL selection sets as it does in a JSON decoder.
[00:31:40]
You just call map and you can you can turn a string into all uppercase or you can turn an int into its string version or, you know, whatever.
[00:31:50]
You can turn some some Elm data type into another data type or some error type into another error type or whatever, whatever logic you have.
[00:31:58]
Do you have other tips in your mind? Otherwise, I can ask some hard to answer questions.
[00:32:04]
Hard to answer questions are good. Other tips? I do have some other tips.
[00:32:09]
Let's go through them then. OK, cool. So, well, so I mentioned opaque types and just general module design and encapsulation.
[00:32:18]
You know, I think that's something I really would love to get out there as a more common practice.
[00:32:24]
Something that pairs really nicely with that is using this custom scalar codecs feature in Elm GraphQL.
[00:32:31]
I'm this is like probably my favorite feature of Elm GraphQL and I feel like it's not used as much as it could be.
[00:32:40]
Are custom scalar codecs specific to Elm GraphQL or to GraphQL?
[00:32:46]
Because I've seen them, but I don't know whether they're originally from.
[00:32:51]
Good question. Custom scalars are a GraphQL concept. Custom scalar codecs are an Elm GraphQL concept.
[00:32:59]
The GraphQL concept of a custom scalar is you have these built in scalars.
[00:33:05]
You have strings and ints and there's an ID built in custom type.
[00:33:10]
So a scalar is pretty much a primitive or a object, but a primitive object like?
[00:33:18]
Yeah, yes. A known declared object. Right.
[00:33:24]
So here's how I think of it. I think of a custom scalar as a contract.
[00:33:29]
Here's why that's really cool, because it's a contract that you can guarantee on the server side.
[00:33:35]
And it's a contract that you can leverage that guarantee on the client side.
[00:33:41]
So like, what does that mean? Well, if you have an ISO 8601, your favorite number.
[00:33:49]
ISO 8601. Years of practice.
[00:33:54]
I wake up and look at myself in the mirror every morning and say ISO 8601. I recommend it.
[00:34:00]
If you ever mess that one up, I will laugh at you so much.
[00:34:05]
But yeah, that's a perfect example because it's a contract. It is a specification, right?
[00:34:11]
It is a primitive, but it has some form or some contracts that it tries to uphold.
[00:34:18]
Exactly. Exactly. So that's right. There is an implicit contract in that concept of an ISO 8601 custom scalar type.
[00:34:27]
And so, well, how do you trust that contract?
[00:34:30]
Well, you trust it because on the server side, any time you're returning a value of type ISO 8601,
[00:34:38]
you define the logic for fetching those types of values and you take care of the serialization in that format.
[00:34:46]
And so you can then deserialize it with that assumption. And so it's a contract that you're making.
[00:34:52]
Now, this brings up an interesting part of working with Elm GraphQL, which is that what guarantees does Elm GraphQL make?
[00:34:59]
Does Elm GraphQL guarantee that your selection set will never have a decoder error?
[00:35:06]
And the answer is no. It essentially guarantees that you will not have a decoder error under specific conditions.
[00:35:13]
And serializing something as ISO 8601. Sorry, but Elm GraphQL really can't help you there.
[00:35:20]
That's just a contract that you're... But what it can help you with is it can say, OK, well,
[00:35:26]
any time there's a value that's of this type, I'm going to use this logic to deserialize it.
[00:35:32]
And the server can say, OK, well, any time I'm sending a value of this type, I'm going to use this logic to serialize it.
[00:35:38]
So it's... So Elm GraphQL helps you by providing this mechanism for leveraging that contract at the right point.
[00:35:46]
But you're on your own for making sure that you've wired up the contract correctly between the back end and the front end.
[00:35:53]
Yeah, if the server messes up, then you have a decoder error or some kind of error. What kind of error would you have then?
[00:36:00]
You would have... That is a good question. Let me look at the docs.
[00:36:05]
I believe I remember being very explicit that I... As much as possible in the Elm GraphQL error messages,
[00:36:13]
I tried to proactively defend against bug reports by saying, you know, the person who wrote this Elm GraphQL...
[00:36:21]
Well, I don't say it that way, but I say like, you know, somebody defined a custom codec that failed.
[00:36:28]
So it's going to give you an error like that.
[00:36:31]
Yeah, because you need to write the codec well also, right? As an implementer of the front end for Elm GraphQL.
[00:36:40]
Exactly right. You need to handle that correctly. But then it gives you a pinch point where in a single place, that codec doesn't even...
[00:36:48]
It will apply that decoder to any value of that type. So now when you go to select a date, if you parse that string,
[00:36:57]
which is ISO 8601 format into a time.posix, then when you have a selection set, which is a created at timestamp,
[00:37:05]
which has that type, when you get it, the type for you as an Elm GraphQL user building up a selection set is going to be time.posix.
[00:37:15]
There are a number of places that you can make use of that. You can make use of that for, you know, I think it's a great idea for ID types.
[00:37:22]
I recommend that people use not just the built in, you know, GraphQL custom scaler.
[00:37:30]
GraphQL has a built in scaler type called ID, which, you know, that's nice.
[00:37:36]
I mean, if it's like a globally unique ID, then maybe that's fine. But really, we can do better than that.
[00:37:45]
And I recommend having unique ID types for each type of ID, a user ID type, a product ID type, because, you know, if you're trying to pass in an ID type,
[00:38:01]
you can describe in your Elm code base, this needs a user ID. This function requires a user ID.
[00:38:10]
And now it's guaranteed to be in sync. You don't need to remember to turn something into a user ID. The codec is going to do that for you.
[00:38:20]
Yeah. So in this case, the codec would just wrap it in an ID. You wouldn't have to do anything more than it wouldn't fail ever.
[00:38:28]
It wouldn't fail as long as you get the underlying type right.
[00:38:33]
From the server.
[00:38:34]
Yes.
[00:38:35]
Yeah.
[00:38:36]
So this is a corner of the GraphQL specification that is not perfect. Overall, the GraphQL specification is very nice.
[00:38:43]
But there's currently no type information about custom scalers. So if you send a custom scaler for a user ID, there's nothing telling you, oh, and by the way,
[00:38:55]
the underlying type is a string or the underlying type is an int.
[00:38:59]
Okay, I thought that would be the case.
[00:39:02]
You would think so. And there are some active specification amendments that are in progress to help with that, although it seems like they're going to be optional, not required.
[00:39:13]
And by the way, the thing that makes it useful to have the schema describing the types in a GraphQL API is that for one thing, you can describe that data and introspect it, which is great.
[00:39:27]
But how does that guarantee anything?
[00:39:30]
Right. And the answer is, well, it guarantees that you're going to. So the GraphQL specification doesn't guarantee that the server will never send an incorrect type.
[00:39:39]
It guarantees that if it does try to send an incorrect type, it will give an error at the GraphQL level.
[00:39:46]
So the GraphQL server framework itself is responsible for not letting anything escape outside of it.
[00:39:53]
Okay, so you would get a 500 or something. I guess that's not the equivalent of a 500.
[00:40:00]
Equivalent of a 500, it might even be a 200 and then contain an errors key in the payload that it returns, which Elm GraphQL would treat as an error.
[00:40:10]
I can't even remember. It's been too long since I've been in there. But whatever the case, it's going to be treated as an error.
[00:40:16]
So now you can use a more like type safe end to end solution if you're using something like Juniper with REST, which is a type safe GraphQL framework.
[00:40:24]
Then you can you can make type guarantees from the database all the way through to returning something from the server or using something like Hasura or PostGraph file.
[00:40:34]
It's actually looking at your Postgres database schema and then inferring the type information from that.
[00:40:40]
So there are ways to keep them in lockstep, but all the GraphQL specification says is if they're mismatched, then I will not let it get through as incorrect data.
[00:40:51]
Instead, I will mark it as an error and send an error response.
[00:40:55]
So you do need to drop in in the custom scalar codecs. You do need to drop in and decode it into the low level type.
[00:41:03]
And then you can map it into whatever types you want. So you actually write a raw JSON decoder, as scary as that may sound.
[00:41:09]
Well, it's not that scary once you've once you've listened to the JSON episode that we've made.
[00:41:15]
Right. Yes, exactly.
[00:41:17]
The one thought that I had when I first heard of Elm GraphQL is like you generate all this API from the GraphQL schema definition file or API.
[00:41:28]
And that works as long as you as the API does not change.
[00:41:32]
So if my server and my front end are separated, how do I keep them in sync?
[00:41:38]
Right. OK, yes. So the way that I recommend people do this is so if you have a mono repo, which is our case, which is practical for this purpose.
[00:41:51]
OK, good. Which I'm a fan of mono repos. Anytime there's a server change that changes your GraphQL schema,
[00:41:57]
you can take that schema, generate your Elm GraphQL code, and you can actually verify that you have not broken any of the code you're calling.
[00:42:09]
Because if you so here's the step here. Here are the steps you take.
[00:42:14]
You are doing a build on your CI. You have a server change.
[00:42:19]
You generate the GraphQL schema from that. There are different ways to do that with different tools.
[00:42:24]
But now you have the latest schema that you're that you're saying I would like to deploy.
[00:42:29]
Right. So then you take that schema, you feed that into the Elm GraphQL CLI tool, which is going to generate the latest code.
[00:42:38]
So now you've updated the generated code or generated the latest Elm GraphQL code.
[00:42:44]
And now you run Elm make on your code. If there was a breaking change and you depended on it,
[00:42:50]
if you removed person dot first name and last name in favor of full name and your code was calling it,
[00:42:58]
that code will Elm make will give an error. So your new server schema change does not go live.
[00:43:04]
That is very nice when you got a server team and a front end team and the third team does not really know how you use it.
[00:43:11]
How you use the GraphQL API in the front end. They kind of have a safety net that says, hey,
[00:43:17]
I'm not going to break the front end because there there is this build step.
[00:43:22]
Yes, exactly. And on the flip side, it actually gives you more confidence about removing things that you want to deprecate.
[00:43:30]
Now, a lot of people in the GraphQL community talk about not versioning their APIs and not doing breaking changes.
[00:43:38]
I personally don't see a problem with doing a breaking change with you know, you can deprecate fields in GraphQL.
[00:43:46]
You can have like a sort of safety window and you can do it using these techniques in your build system so that you are guaranteeing that things are in lockstep.
[00:43:55]
And so you can do it in a safe way. I think it's it's a reasonable thing to do.
[00:43:59]
And it gives you more confidence because you know that you've stopped depending on things when when that build goes live.
[00:44:05]
That said, that doesn't guarantee that somebody is not on a version from last week.
[00:44:10]
Yeah, like the front that has not been deployed yet.
[00:44:14]
So there are different ways to manage that. But I actually I do know for a fact that that some people, you know, will check.
[00:44:23]
They'll check the deployed version against the client side version.
[00:44:28]
And, you know, if there's a mismatch, then you can, you know, reload or warn the user or there are different ways to approach it.
[00:44:35]
You could also keep track. You could increment some version number on your CI anytime you have a breaking GraphQL change.
[00:44:45]
Right. And you could say, OK, we don't need to restart anytime the client and server are out of sync only if there has been a breaking change.
[00:44:54]
And so you could check for that. So there are a number of ways to deal with that. But I think I think it's quite doable.
[00:44:59]
There are a lot of tools at your disposal there.
[00:45:01]
So what about when the server and the front end are not in the same repo?
[00:45:06]
Do you then try to avoid breaking changes and have the schema published somewhere where the front end can use it for at build time?
[00:45:15]
You could. I mean, at that point, you could certainly try to just avoid breaking changes.
[00:45:21]
There are there are tools out there that will give you a warning if you make a breaking change.
[00:45:26]
So you could at least put in a stopgap that says if there is GraphQL schema breaking change, then maybe it needs to be built with a certain flag or exactly something like that.
[00:45:37]
So that's that's one technique at your disposal, too. You could also you could also just you know, even if it's a mono repo, even if it's not a mono repo, you could on the back end before you make any changes to the schema,
[00:45:50]
you could fetch the latest client side code, clone that repo and run Elm make on it and make sure everything's OK.
[00:45:58]
So you can still achieve like some of those sort of characteristics of a mono repo, even if it's not technically a mono repo.
[00:46:04]
Yeah. And then, yeah, it's mostly about discipline. Like just when you do HTTP calls, right?
[00:46:11]
The thing is, it doesn't feel nice because you have Elm GraphQL that promises a lot of type safety and then you have to kind of trust whatever the server is giving you because you might be out of date.
[00:46:25]
And then you get those errors that they mentioned before.
[00:46:28]
Yeah, I think it's the kind of thing that really just requires some like infrastructure investment and some time and thought to come up with the right solution for your environment.
[00:46:38]
But all of these techniques, I think, are tools you can leverage to try to try to make that more robust.
[00:46:45]
Yeah. But even even without that, it will be much safer than if you did the GraphQL or HTTP requests on your own.
[00:46:53]
Right. Exactly. So should we talk about while we're on the topic of ways that an Elm GraphQL request can fail?
[00:47:01]
Should we talk about other ways it can fail?
[00:47:04]
Oh, well, you're getting me curious now. So, yeah. All right.
[00:47:08]
Well, well, the number one source of failure that comes up when you send an Elm GraphQL request, you do get an HTTP error potentially.
[00:47:18]
So that that's definitely, yes, that's a failure that Elm GraphQL can't help you out with too much.
[00:47:24]
There's there's another type of error, which is which is maybes or nullable fields.
[00:47:30]
So GraphQL has this concept of a nullable field. And what happens quite often, I sometimes get bug reports of why is my Elm GraphQL code generating a maybe here?
[00:47:44]
And I have a it's it's my most commonly linked to section of the FAQ document, which is well, oftentimes when people are like going to something like Elm GraphQL,
[00:47:55]
they were consuming that GraphQL schema from an untyped language and so weren't information that they made use of.
[00:48:02]
And so they just had nullable fields all over the place because it's the default in GraphQL.
[00:48:08]
When you define these GraphQL types, you have to go out of your way to put an exclamation point to say this is not nullable.
[00:48:15]
And so you'll even get like a you know, instead of getting a list of IDs, you'll get a list which could be null of IDs, any of which could be null,
[00:48:25]
which is something that happens often even in like public GraphQL APIs that people can't control.
[00:48:31]
But it is often not what what they were trying to express with those types.
[00:48:36]
But it just was like, no, it's it's not supposed to be null. It should never be null. But we've got to add those types.
[00:48:42]
So that's a server side issue, right? That is a server side issue. And that's not going to be an error.
[00:48:46]
Elm GraphQL is just going to, you know, faithfully put those maybes in everywhere and make you annoyingly deal with them.
[00:48:53]
So Elm GraphQL provides an escape patch, which is let me look up the name so I don't get it incorrect.
[00:49:00]
I made these names unappealing. So there's non null or fail. Yeah. I remember someone using that at work and and it failed.
[00:49:10]
And I definitely went out of my way to say somebody called selection set that non null or fail in the error message and say,
[00:49:18]
please find, you know, invocations of non null or fail in your code base. So I don't get bug reports there.
[00:49:24]
But yeah, so non null or fail. It's really a last resort. You want to avoid using it.
[00:49:29]
I have some similar helpers for for a list of noble elements.
[00:49:33]
And then I have like a more general map or fail. And that allows you to, you know, handle possible errors.
[00:49:40]
So if something if you want to validate some data, you can allow your decoders to fail making those guarantees.
[00:49:47]
Now, that said, I think it's I think it's a better practice to deal with those validations in custom scalar codecs.
[00:49:54]
So I would recommend reaching for that before you reach for map or fail. But in some cases, that can be useful, too.
[00:50:00]
All right. Have we covered the subject now or are there other?
[00:50:04]
I think that's pretty good. If you're if you're done with your tough, tough questions, they weren't they weren't too tough.
[00:50:08]
No, I know. I was just trying to scare you. Yeah. Yeah. That was intimidating.
[00:50:16]
How do people get started with the graph?
[00:50:19]
The number one place I recommend as a starting point is to just go to the Elm GraphQL package documentation and click the GraphQL dot selection set module.
[00:50:33]
I sort of step through the basics of a selection set. And if you understand the concept of a selection set really well, then you're going to be in good shape, I think.
[00:50:42]
So like a selection set can have zero items. You can have one item.
[00:50:46]
You can map together selection sets. You can treat it, you know, if you're familiar with a fragment in regular GraphQL,
[00:50:54]
you can build these composable pieces and mix them together just like a fragment in GraphQL, except they're just selection sets and you can mush them together.
[00:51:04]
Yeah, I feel like we should specify and this might be very late, but we're talking about Dillon Kern's slash Elm GraphQL.
[00:51:12]
That's fair. There are three others. They work differently and have different purposes, which we will probably not go into considering it's probably the we should be wrapping up now.
[00:51:24]
Yeah, I would say I think I lay that out in the readme.
[00:51:28]
Yeah. So at the very top of the readme, I point to a discourse thread that talks about the differences between the different approaches.
[00:51:36]
But in a nutshell, it's, you know, Dillon Kern's Elm GraphQL is the only one that is type safe, except there's actually one other library that's that's type safe, I believe.
[00:51:49]
But it's built around the concept of turning a single GraphQL query into an Elm function to call that.
[00:51:58]
I'm going to need to look into that, but I'll link to that, too. But Elm GraphQL takes this philosophy of being like a query builder that you can sort of compose together the pieces in Elm code rather than manipulating strings of GraphQL queries.
[00:52:16]
Yeah. Do you still do workshops around Elm GraphQL?
[00:52:19]
I might do another workshop. I have been thinking about maybe making my workshop into a video course and adding some additional content around that.
[00:52:28]
So if there's interest in that, let me know. I'd love to love to hear about it.
[00:52:32]
Yeah. And then other than that, I think types without borders gives a pretty good intro to sort of the philosophy behind it.
[00:52:39]
Which is a comfort stalk.
[00:52:40]
That's a comfort stalk I gave about Elm GraphQL and those ideas. And, you know, look at the Read Me. There are some other good resources there. And as always, there's a very helpful Slack community that you should reach out to if you want to ask questions or talk about best practices.
[00:52:54]
Yeah. All right. Any other parting words of wisdom or have we given people enough to think about here?
[00:53:00]
You should use selection set dot succeed.
[00:53:03]
Oh, I like it. That's a good note to end on.
[00:53:08]
Yeah, that's following the footsteps of the Jason episode and the tiny steps episode. Use selection set dot succeed.
[00:53:15]
Nice callback. Succeed is the key to success. Beautiful. All right, Jeroen. Well, thanks a lot. And I'll talk to you next time.
[00:53:23]
See you next time.