Francesco

Francesco

Hello!

I have recently discovered that calling Absinthe.Plug.put_options , in my case to add some information to the context, actually overwrites whatever might have been set as the context before. I have two plugs for my router, one that auths and adds the user to the context, and another that adds a workspace ID to the context. I know I could just combine these plugs but that kind of defeats the point of plugs (modular parts of a pipeline).

Absinthe.Plug.put_options(conn, context: %{authenticated: true, current_user: user})
Absinthe.Plug.put_options(conn, context: %{workspace: current_workspace})

So the question is, is there any way that I could modify the context object instead of overwriting it? I’d be happy to just get the current context, merge the new options, and call put_options with the merged object but I can’t see a great way to get the current context. Only way I can see is to grab it from conn.private.absinthe but that doesn’t feel right.

Showing Posts 1 to 3

Catharz

Catharz

I had the same issue and just ended up merging the context.

e.g. to resolve some subfields in our queries, we need the language passed down to their resolvers like this:

  object :search_queries do
    @desc "Find products using search terms."
    field :search, non_null(:search_result) do
      # Store the language on the context for field level resolution
      middleware fn res, _ ->
        %{res | context: Map.merge(res.context, %{args: res.arguments})}
      end

      @desc "Search keywords."
      arg :keywords, :string, default_value: ""

      @desc "Country code."
      arg :country, :string, default_value: "US"

      @desc "Currency."
      arg :currency, :string, default_value: "USD"

      @desc "Language."
      arg :language, :string, default_value: "EN"

      @desc "Page number to return."
      arg :page, :integer, default_value: 1

      @desc "Number of items per page."
      arg :page_size, :integer, default_value: 20

      @desc "The width of images (in pixels) to suit the user's phone"
      arg :image_width, :integer, default_value: 500

      resolve &SearchResolver.search/2
    end
  end

That makes the language value available at the field level where we need to translate strings.

We do something slightly similar in our global middleware(s). e.g.

    %{
      res
      | middleware:
          res.middleware ++
            [
              {{__MODULE__, :complete},
               [
                 start_time_mono: start_time_mono
               ]}
            ]
    }
benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

@Francesco PR welcome for an update_options function. In the short term feel free to grab it from conn.private.

Francesco

Francesco OP

@Catharz thanks for the help :slight_smile:

@benwilson512 I’m definitely keen to try my hand at the PR as soon as I can spare a few hours! But yep just using conn.private is working fine in the meantime

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews