Lessons: 23Length: 3.3 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

6.2 Getting a List of Users

In the previous lesson, we set up the UI for creating a project. But we need our list of users so that we can appropriately select the users to assign tasks to. So, we need to write the query that is going to handle that request. And really there's nothing special about this, we have done this several times. It's just that we need to do it now, so we're going to do it now. And let's take our projects query and use that as a basis, because that's at least going to get us up and running. Let's call it UsersQuery and then we will, of course, make the necessary changes. So the class name is one. We don't need auth, we don't need the project model, either. We just need the user model. And as far as the attributes are concerned, let's call that UsersQuery. This retrieves users and there are no args, because we are just simply retrieving all of our users. So we will get rid of that. Now our return type is not going to be a list of projects, it's going to be a list of users. And the resolve method will be much more simple, we simply just return User::all();. So, that should be fine. We need to go to our config and register that, so let's open up the graphql config. Now, this is a protected endpoint, or not an endpoint, this is a protected query. So let's put this in the default schema and let's just call this users. And of course, the query name, the class is UsersQuery. So we are done with the server, now let's focus on the clients. So let's go to resources > js > components, and that was inside of views > CreateProject.vue. And let's do this inside of the created hook, so let's do that there. And we will make a request using our query method and let's call this users. Whenever we get a response, we are simply going to set our users = res.data.data.users; and there we go. So now we just need to define the query inside of our queries. And do we have something? No, let's just write this. We're gonna call it users: and just like everything else, we'll use the formal syntax, so we'll have GetUser. We have no variables because we have no arguments, but we do need to select some data. Now as far as our user interface is concerned, all we really need is the ID and the name. We don't need the email address. So let's just get those. And that should work, I would think. So let's refresh the page. Hopefully, we will see our list of users and we do. Now let's make sure that everything else is going to work. So if we select Jeremy and Jim on the assigned to dropdown box, there should be Jeremy and Jim. If we just select Jeremy, we have Jeremy. If we add several tasks, we should see the same thing regardless of what task it is. So, everything is working fine as far as our user interface is concerned. So, now we just need to focus on retrieving this information and sending it to the server. And we will do that in the next lesson.

Back to the top