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

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews