Absinthe Journey with Wolfiton or How to Bring the Absinthe Tutorial UP to Date- Part 7 The User
Tags: beginners, elixir, graphql, absinthe
Published: 2020-03-16 14:21:05

Hi everyone

**Series navigation**
[PART 1 Introduction](https://dev.to/wolfiton/absinthe-journey-with-wolfiton-or-how-to-bring-the-absinthe-tutorial-up-to-date-part-1-introduction-5fgg)
[PART 2 Foundation](https://dev.to/wolfiton/absinthe-journey-with-wolfiton-or-how-to-bring-the-absinthe-tutorial-up-to-date-part-2-foundation-3jnh)
[PART 3 The Walls](https://dev.to/wolfiton/absinthe-journey-with-wolfiton-or-how-to-bring-the-absinthe-tutorial-up-to-date-part-3-the-walls-n86/)
[PART 4 The Door](https://dev.to/wolfiton/absinthe-journey-with-wolfiton-or-how-to-bring-the-absinthe-tutorial-up-to-date-part-4-the-door-10m0)
[PART 5 Testing](https://dev.to/wolfiton/testing-1ohn)
[PART 6 Can you fly?](https://dev.to/wolfiton/absinthe-journey-with-wolfiton-or-how-to-bring-the-absinthe-tutorial-up-to-date-part-6-can-you-fly-3al5)
[PART 7 The User](https://dev.to/wolfiton/absinthe-journey-with-wolfiton-or-how-to-bring-the-absinthe-tutorial-up-to-date-part-7-the-user-58)

**Problem with some code highlighting for this part of the series. An issue has been opened.**

Solved by @rhymes

Welcome to part 7 of the series but before we jump in let's recap what we did in the sixth part:

- homework: Create a Context `Accounts` with a `User` Schema and a `Users` Migration with `username` field and `password` field.

- Bonus: Create a `User` query and a `User` type.

**How was this exercise for you *Dear Reader* did you managed to build it?**

**Section navigation**

- [User context with schema and migration](#section-1)

- [User query and user type](#section-2)

So we will have two sections today in the first part we are going to create a Context `Accounts` with a `User` Schema and a `Users` Migration with `username` field and `password` field.

In the second part, we are going to create a `User` query and a `User` type.

#Section 1
User context with schema and migration

Let's create a new branch using:

```shell
hub checkout -b 01-User 
```

```shell
mix phx.gen.context Accounts User users username:unique password
mix ecto.migrate
```

With these commands we finished the first part of our homework and can add our work to git

```shell
hub add .

hub commit -m "User context with schema and migration"

hub push --set-upstream origin 01-User
```

#Section 2
User query and user type

Create a new file in: `lib/wolf_blog_web/types` and call it `user_types.ex`

```elixir
defmodule WolfBlogWeb.Types.UserTypes do
  use Absinthe.Schema.Notation

  @desc "User fields that can be interrogated(get)"
  object :user do
    @desc "The user id"
    field :id, :id
    @desc "The username of the user"
    field :username, :string
    @desc "The password of the user"
    field :password, :string
  end
end
```

Now change `schema.ex` to this:

```elixir
defmodule WolfBlogWeb.Schema do
  use Absinthe.Schema
  alias WolfBlogWeb.Resolvers.PostResolver

  import_types(WolfBlogWeb.Types.PostTypes)
  import_types(WolfBlogWeb.Types.UserTypes)

  query do
    @desc "list_all_posts"
    field :posts, list_of(:post) do
      resolve(&PostResolver.list_all_posts/3)
    end

    @desc "list_all_users"
    field :users, list_of(:user) do
      resolve(&UserResolver.list_all_users/3)
    end
  end
end
```

The new part that we added to the  `schema.ex` is this:

```elixir
@desc "list_all_users"
 field :users, list_of(:user) do
 resolve(&UserResolver.list_all_users/3)
 end
```

This completes the bonus section of the homework **Dear Reader**.



But wait, Wolfiton I get an error that my resolver is not found?!



I am glad you noticed **Dear Reader**



As you can remember from our previous articles in these series. To make `Phoenix` and `Absinthe` work together we need a `Resolver` to connect the `Context` with our `Schema`.



So in `lib/wolf_blog_web/resolvers`, create a new file called `user_resolver.ex`

```elixir
defmodule WolfBlogWeb.Resolvers.UserResolver do
  alias WolfBlog.Accounts

  def list_all_users(_, _, _) do
    {:ok, Accounts.list_users()}
  end
end
```

So now we only need to add this alias in `schema.ex`:

`alias WolfBlogWeb.Resolvers.UserResolver`

So our `schema.ex` will look like this:


```elixir
defmodule WolfBlogWeb.Schema do
  use Absinthe.Schema
  alias WolfBlogWeb.Resolvers.PostResolver
  alias WolfBlogWeb.Resolvers.UserResolver

  import_types(WolfBlogWeb.Types.PostTypes)
  import_types(WolfBlogWeb.Types.UserTypes)

  query do
    @desc "list_all_posts"
    field :posts, list_of(:post) do
      resolve(&PostResolver.list_all_posts/3)
    end

    @desc "list_all_users"
    field :users, list_of(:user) do
      resolve(&UserResolver.list_all_users/3)
    end
  end
end
```

Now let's add all our modifications to GitHub:

```shell
hub add .

hub commit -m "Added User query with user types and a user resolver"

hub push
```



Part 7 is finished, I hope you enjoyed **Dear Reader**.

Also, if you liked the article please share it with others on social.

Credits:

Thanks, @rhymes for helping me with my [issue](https://github.com/thepracticaldev/dev.to/issues/6665)