1

Im trying to apply filters in subscription but I’m having some trouble in the resolver file.

the absinth resolution requires as a parameter a {:ok, params} o {:error,params } so it can execute, the problem is that everything y send appears at my subscription, i want to stop the execution so nothing is sent to my subscription

this is what I have tried


  def subscribe(item, args, %{context: %{current_token: _current_token}}) do
    filter_subscription(item, args)
  end

  def subscribe(_args, _info, _ctx) do
    {:error, "Not Authorized"}
  end

  def filter_subscription(item, %{filter: filter}) do
    IO.inspect item
    IO.inspect(filter)

    case filter do
       %{name: name}->
          case String.contains?(item.name, name) do
            true ->
                {:ok, item}
            false ->
                nil
          end
        _hey ->
          IO.puts "error"
    end
  end

end  ```
Gerardo Davila
  • 161
  • 1
  • 1
  • 7
  • I guess you're asking a similar question to this one: https://stackoverflow.com/questions/54894293/subscription-with-authorization-skip-trigger-if-condition-isnt-met? It basically comes down to use the topic call to make sure the client is only interested in that filtered data, and not everything else – Jonas Dellinger Mar 23 '19 at 00:18
  • Well im thinking of not using topics because you can’t scale it and you have to create a channel for each topic – Gerardo Davila Mar 23 '19 at 19:05
  • I can’t use topics because I can’t make a topic/channel for every possible filter value. – Gerardo Davila Mar 23 '19 at 19:05

1 Answers1

1

Generally you can determine what gets sent in one of two ways:

A) If you want to send the object only to certain subscribers - you use topics as the intermediate step. The config function in your field determines which topic a subscriber is listening to, and when you publish you decide which topics to publish to (either by calling Absinthe.Subscription.publish once for each relevant topic, or via the trigger function in your subscription field declaration). Publishing to no topics is an option of course!

B) If you just have one topic for all subscribers, but want to decide whether to send an object or not based on its content, either you decide whether or not to call Absinthe.Subscription.publish based on the object, or if you’re using trigger then your function would decide whether to return the topic name, or an empty list []

When your resolver is running, absinthe has already decided it’s sending a subscription result to the subscriber. You can’t stop it sending something, you just get to choose what. I guess if you really wanted to do something in your resolver, you could return {:ok, nil} so your subscribers get a null value and could handle that, but that’s pretty weird.

Since in the comments on the question you say you can't create a topic for every possible value, I guess you can take path B.

(Yes, I'm reposting my reply from the absinthe slack channel here! Hope you see it and find it useful!)

amcvitty
  • 413
  • 3
  • 9