Francesco

Francesco

Hello!

First the problem: I have a login mutation. It returns the user object on successful login. Originally the user object simply contained built in types (string, boolean, ect) so no extra resolvers aside from the login resolver where needed. However I then added another field called workspaces to the user which has its own resolver.

  object :user do
    field(:first_name, :string)
    ...

    field :workspaces, list_of(:workspace) do
      resolve(&ScribeWeb.Resolvers.Workspaces.get_workspace/3)
    end
  end

get_workspace grabs the user_id from the resolution context and uses this to fetch that users workspaces. This works fine if the user is logged in already and the user id is already on the context. However when logging in this is not the case.

The question: Is it possible to modify the resolution context and add the user id in the login resolver, so that when the get_workspace resolver is called it has access to the now logged in users id?

My current workaround fix: I created another user object but this time the workspaces field didn’t have a resolver. Inside the login resolver I now make another call to the db to get the users and update the user struct with these results manually.

  def login(_parent, %{login_input: args}, %{context: context}) do
    %{email: email, password: password} = args

    Scribe.Accounts.authenticate(email, password)
    |> case do
      {:ok, user} = result ->
        workspaces = Scribe.Workspaces.get_workspace(%{user_id: user.id})
        updated_user = %{user | workspaces: workspaces}
        {:ok, updated_user}

      error ->
        error
    end
end

This just seems a little crappy because I now need to maintain two almost identical user objects in my schema, one with a workspace resolver and one without.

Thanks in advance for the help

First 2 of 2 Posts Switch mode

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hi @francesco.

You can’t do this within the resolver itself, but you can add some middleware that runs after your resolver which can do this:


    field :login, :user do
      resolve(&login/3)
      middleware fn resolution, _ ->
        case resolution.value do
          {:ok, user} ->
            Map.update!(resolution, :context, &Map.put(&1, :current_user, user))
          _ ->
            resolution
        end
      end
    end
11
Post #1
Francesco

Francesco OP

Thanks @benwilson512 ! Exactly what I needed :slight_smile:

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement