DEV Community

wolfiton
wolfiton

Posted on • Updated on

Absinthe Journey with Wolfiton or How to Bring the Absinthe Tutorial UP to Date- Part 7 The User

Hi everyone

Series navigation
PART 1 Introduction
PART 2 Foundation
PART 3 The Walls
PART 4 The Door
PART 5 Testing
PART 6 Can you fly?
PART 7 The User

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

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:

hub checkout -b 01-User 
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

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

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:

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:

@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

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:

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:

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

Top comments (0)