So now if I save this, restart my sandbox, refresh my HTML page, and make this request, we should now have authenticated requests. And if we change our page's default landing query to be a little bit more interesting, like we could query instead of account owner, which is just the default field that we got, amount and name, then when we refresh this and land on that default query now and run it, we get our transactions back.
And so now any developer that comes to our little dev portal in the future will be able to log into their bank account and immediately get their transactions with pretty much a login click and a run query click. So now that our graph is super easy to query, its popularity is totally going to take off. I just know it, because everybody's going to want to know how much their bagels are costing them. And before that happens, I think we should put into place some protections so that people follow the rules of using our graph from day one.
And one of the rules that we had from our slides was that we want every request that comes to our graph to have our required headers. So in this case, let's say we want every request to have a required Apollo GraphQL client name header, because then we can plug that into our other observability. So the way we're going to accomplish that is we're going to extend the logic in this context function. So if a request does not have our Apollo GraphQL client name header, then we are going to throw a new error, all requests must be from an identified client. Now, if I run this query, we should get that error back, which we are, because our little web page here isn't identifying itself as a client yet. But over here, just like we extended our request using this handle request function with an authorization header, we can also pretty easily add in an Apollo GraphQL client name header, and just say this is our portal embedded explorer. Now if I refresh my page here and run this query, we should be good to go again. And if we were to look into our observability tools, now the requests coming from our portal embedded explorer will be identified as such.
So now that we're enforcing required headers are indeed present on our requests, another rule that we had from the slides was that all operations should also have a name attached. This one is a little bit more involved because unlike headers, which are just a property that you can pull off the request directly, operation names are embedded in the body of the operation and the body of the operation when it goes over the network and first hits our server is entirely in a stringified format. And so in order to know whether or not the operation is actually named, we have to have our server parse that operation into an abstract syntax tree and then ask at that point, does this operation have a name? So unfortunately we can't just continue to extend the logic that we have in context here, but fortunately we can make use of Apollo Server 3's plugin architecture. So down here if we come back to the Apollo server docs, and look at how the plugin request lifecycle works, there's an entire set of states here that every request goes through the Apollo server, and at any point we could kind of plug into the states and add some augmented logic. And so the first stage of the request lifecycle where the request operation has been fully parsed is the stage right after validation did start, so this would be did resolve operation. So we're going to come in here and hook into, we're going to come in here, add another plugin, and then hook into did resolve operation and its context. And all of this is inside the request did start lifecycle. So here we're going to say if there's no context.operation name, then throw a new error all operations must be named. And now if I run our operation again we should still be just fine because this operation is named, but if I delete that name and run this operation we should get an error. So we are good to go. So with just one small plugin in Apollo Server and some logic in our context function, we are now automatically enforcing that all clients who are using our graph are following our rules. And as this graph takes off and more people discover that they can use our API and learn all about their bagel transactions, we will now know exactly who is using our API and how, which is pretty amazing. And the last thing that I wanted to cover in our demo is how to make this graph truly approachable. It's one thing to give folks that are going to be developing against our graph a link to this sandbox that we made and say, have at it, figure it out, good luck. But it's another thing entirely to give someone a link to a homepage that welcomes them and tells them where to start. And I showed you all how to make a public graph in studio earlier because that's how we embedded the explorer, but I didn't really tell you what public graphs were all about.
Comments