Francesco

Francesco

Modify Absinthe resolution context in a resolver?

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

Marked As Solved

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 #2

Last Post!

Francesco

Francesco

Thanks @benwilson512 ! Exactly what I needed :slight_smile:

Where Next?

Popular in Questions Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

We're in Beta

About us Mission Statement