Skip to content

Instantly share code, notes, and snippets.

@rhymes
Created March 16, 2020 16:46
Show Gist options
  • Save rhymes/ca47ec77e64a7b8ee6bfb83b1dafb44c to your computer and use it in GitHub Desktop.
Save rhymes/ca47ec77e64a7b8ee6bfb83b1dafb44c to your computer and use it in GitHub Desktop.

Revisions

  1. rhymes created this gist Mar 16, 2020.
    173 changes: 173 additions & 0 deletions post.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,173 @@
    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 `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.