Hi everyone
Problem with some code highlighting for this part of the series. An issue has been opened.
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 Accountswith aUserSchema and aUsersMigration withusernamefield andpasswordfield.
- 
Bonus: Create a Userquery and aUsertype.
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.migrateWith 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
endNow 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
endThe 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)
 endThis 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
endSo 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
endNow let's add all our modifications to GitHub:
hub add .
hub commit -m "Added User query with user types and a user resolver"
hub pushPart 7 is finished, I hope you enjoyed Dear Reader.
Also, if you liked the article please share it with others on social.