Advanced GraphQL, v2

Custom Directives Exercise

Scott Moss

Scott Moss

Superfilter AI
Advanced GraphQL, v2

Check out a free preview of the full Advanced GraphQL, v2 course

The "Custom Directives Exercise" Lesson is part of the full, Advanced GraphQL, v2 course featured in this preview video. Here's what you'd learn in this lesson:

The students are instructed to create a custom date format directive for the fields that format timestamps, to write a field level authentication directive, and to add directives to the schema.

Preview
Close

Transcript from the "Custom Directives Exercise" Lesson

[00:00:00]
>> You're going to create a custom date format directive for fields that formats timestamps. Except the arguments, arguments are extra credits. Everything I just showed you about arguments as extra credit, just at least make a directive that you can put on anything that's a created at field, and it'll format it.

[00:00:18]
And don't worry about the formatting there's a function that does it for you automatically. Create a field level auth directive, which is also extra credit. So we already have those authorization helpers, see if you can get rid of all that on your resolvers, and instead, make a directive that does the same thing, and just add them on the fields yourself.

[00:00:37]
And then add your directives to your Schema, obviously. Otherwise, it's not gonna work. So maybe a walk around on that. If you go to the Utils function, there's gonna be a format date that takes in a time stamp, and then it takes in a format. It uses a package called date-fns.

[00:00:54]
But the format is pretty standard timestamp format, and you could do things like year, year, year, year, or month, month, month, month, day, day day. So you can look at the docs, but this function takes in a stamp and takes in a format and returns it. So use this inside of your directive to actually format a date.

[00:01:14]
The other thing you might have to do, and there's two ways you can do it, is in your type desk. Anything that says created at, you might have to change it to a string inside of your Schemas here, if it's not already. There should be strings already, but they're not.

[00:01:29]
Make sure they're all strings. If not, you can like I said, you can change them all to strings here, but you can also change them inside the directive, which is really cool. So directives are really powerful. I showed you some pretty crazy stuff, but I didn't show you that you can also do things like field dot type is now going to be a graphqL string type.

[00:01:49]
You can completely change the whole type of a field right here, if you wanted to. You can do anything you want. So if you wanted to change created at to a string, if they used to be integers as timestamps, but after you format they became strings, then you would have to change it to a string right here.

[00:02:03]
So you can do all that types of crazy stuff. Up to you, how you want to do it. And then again, the auth stuff, this is literally the same thing that we did already for the authorization. You should be able to do a copy and paste, minus some setup here.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now