تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Intro to Resolvers0:00

Okay, let's take a look at using resolvers. Resolvers allow you to add functionality to your queries and mutations. Now, so far, we've been using directives, which are a big part of Lighthouse. But if you have some functionality that directives can't handle, then you would make use of resolvers. I think I was referring to resolvers as custom directives in previous videos, which are a thing as well. But in this video, I'd like to focus on resolvers. So let's rewrite some of these queries using resolvers instead of directives. So let's start with this posts query, we'll make one called postsResolver.

Creating Posts Query Resolver0:26

So let's rewrite some of these queries using resolvers instead of directives. So let's start with this posts query, we'll make one called postsResolver. And the return type is the same. So it's a Post array that's required. And instead of a directive, we'll create a new resolver here. So if we do php artisan and look for Lighthouse here, you'll see that we can make use of either the query or mutation command to create a resolver. So let's do that php artisan lighthouse:query. And this one's called postsResolver. Okay.

And this one's called PostsResolver. Okay. And that was created, and it should be within GraphQL queries. And there is PostsResolver. So now you can write out whatever custom query you want in here. So all you have to do is make sure it returns a Post array. And that should be good. So pretty straightforward. Let's go ahead and return. We can say postAll.

Let's go ahead and return. We can say post all. But remember, I sorted it by the latest posts. So let's do post latest. And we can say get here. Let's make sure to import Post. And that should be good. So let's try that out in our playground. So we have this new posts resolver. Let's go here to our playground.

Testing and Pagination Note1:46

So we have this new PostsResolver. Let's go here to our playground. Let's refresh this. Let's get rid of this. Let's say query. There it is, PostsResolver. And that should work. Title, let's say created that. And we do get all of the posts sorted correctly. Now we did lose pagination here, and it's a bit tricky to get pagination working with

Building Single Post Resolver2:05

And we do get all of the posts sorted correctly. Now we did lose pagination here, and it's a bit tricky to get pagination working with resolvers. So I recommend just sticking to directives for that. Let's go ahead and rewrite our post query here using a resolver as well. So it would be postResolver. It returns a Post. And let's go ahead and run that command again, but rename it to postResolver. Okay. And let's go to that file postResolver.

Okay. And let's go to that file postResolver.php. And let's write our custom query in here. So pretty straightforward. Again, we can say return Post::where. So we're searching for the slug. Actually, I don't have the slug yet. So we're searching by ID. And I forgot the arguments here. So it should be an ID, which is of type ID.

And I forgot the arguments here. So it should be an ID, which is of type ID. And that's required. So let's save that. Let's go back here. Post where. Or we can use the find method as well, but I'll stick to where because we're going to change this to a slug later on. So let's say where ID is equal to. Now you see this array being passed in.

So let's say where id is equal to. Now you see this array being passed in. We can grab the id from that array. So it's called args. And it's called id. And let's grab the first one here, or like I said, you can just make use of find if you want. Let's make sure to import Post. And hopefully I did this correctly. Okay.

And hopefully I did this correctly. Okay. Let's try that out. Back here to GraphQL playground, let's refresh. Let's try post resolver. Okay. Say ID of one. And let's say title. And that should be good. There we go.

Resolver Namespaces and @field3:46

And that should be good. There we go. If you take a look at the Lighthouse config, there's a section for namespaces and the namespace where these files are defined are in here already. So we have queries and mutations. So if you define these files somewhere else, then they're not going to work unless you use the field directive to specify where they are. So let's take a look at that. Let's go to directives. Let's look for field here.

Let's go to directives. Let's look for field here. And you can see it says assign a resolver function to a field. So if it's not in the default place, or if you want to be explicit about it, then go ahead and use this directive. So let's grab that. Let's put it in our two new resolvers that we created here. So we can say field and the location. So this one would be queries. And this one's called postsResolver.

So this one would be queries. And this one's called PostsResolver. Now if you don't specify a method here, then it's going to look for the __invoke method, which is what we have here, right here. So we can just leave it as the class. So very similar to the controller syntax. So let's go ahead and grab this and put it on the PostsResolver as well. Again, this is only required if you either put it in a different location, or if you just want to be explicit about it. Okay, save that.

Creating Mutation Resolver5:08

just want to be explicit about it. Okay, save that. And it should still work the same. So back here. If you run this again, it should still work. It does. Let's make one more resolver for a mutation. So we'll recreate the createPost mutation here. So let's duplicate this. Let's rename it to createPostResolver.

So let's duplicate this. Let's rename it to createPostResolver. And I'm going to keep the rules as a directive here. Just like with pagination. It's a bit tricky to do within the resolver. But we will replace this create directive here. So same command, except now it's mutation. And it's called createPostResolver. Okay. Let's go to that file.

Okay. Let's go to that file. Create. Actually, let's add the field first. Again, you don't have to, but I just want to be consistent here. So let's grab this. Let's put it on our new resolver here. It's within mutations. And it's called createPostResolver.

And it's called CreatePostResolver. Okay. Let's go to that file. CreatePostResolver. And here we can just create the post manually. So let's do that. Let's say return Post::create. And we have three fields. We have the userId, which is again coming from the arguments. So args['userId'].

We have the userId, which is again coming from the arguments. So args userId. And we have the same for title and body. So let me just change these title and body. Okay. Let's make sure to import Post here. And let's give that a try. Let's go back to GraphQL Playground. Let's refresh. Let's change this to a mutation.

Let's refresh. Let's change this to a mutation. That new one should be in there. There it is. We have userId. Let's say one. We have title testing, and we have our body testing again. And let's just say ID and title. Okay. There we go.

Okay. There we go. That seemed to work. Let's check our database to make sure 54 is the new post. And there it is right there. Cool. So say, for example, you wanted to do something else when creating a post, we now can do that in our resolver here. So for example, if we wanted to send an email when a new post is created, we can just do it in here.

So for example, if we wanted to send an email when a new Post is created, we can just do it in here. So I'm not going to do that. I'm just going to do log info sending email just to see it working. Make sure to import log. And now we'll save this as a variable post, and then we can return that down here. Return post. Okay. So let's try that one more time. I am going to clear my Laravel log here just to see the log being logged in there.

So let's try that one more time. I am going to clear my Laravel log here just to see the log being logged in there. And let's try that one more time. Let's run this again. Let's open up our log here. And there is the sending of the email. So yeah, if you need any custom functionality that directives can't handle, be sure to reach for resolvers.

Custom Resolvers for QueriesCustom Resolvers for Mutations

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟