PeriodicTask trigger mutation/suscription from backend

I have a periodic task (Task module that runs at startup under application.ex) that runs every hour. The thing is that every hour this task should trigger a message to users listening to a suscription [examStatus suscription].

Should I make a graphql query? Or there is a another way to solve this?

  object :exam_suscriptions do

    field :exam_status, :exam do
      arg :exam_id, non_null(:id)
      config fn args, _ -> {:ok, topic: "exam:" <> args.exam_id} end

      trigger [:update_exam_status], topic: fn
        exam_status -> ["exam:" <> "#{exam_status.id}"]
      end
    end
  end
1 Like

You can tracks all live topics, and send publications to them via Airbase.Subscription.publish/3

You mean Absinthe.Subscription.publish/3? In that case what do the second and third parameters mean?

Absinthe.Subscription.publish(ExampleWeb.Endpoint, comment, comment_added: "absinthe-graphql/absinthe")`

The second arg is the root value you are publishing, in your case probably an exam. The third arg is a keyword list of subscription fields you are publishing to, and then topics for those subscription fields.

1 Like

If my mutation is this:

mutation {
  updateExamStatus(input: {
    isActive: true
    isFinished: false
  }, examId: 1) {
    id
    name
    isActive
    isFinished
  }
}

The function should be like this?

exam_id = 1
Absinthe.Subscription.publish(AdmissionWeb.Endpoint, exam_id, exam_status: 1)

You may want to publish the actual exam if that is what your subscription field expects eg:

exam = Repo.get(Exam, exam_id)
Absinthe.Subscription.publish(AdmissionWeb.Endpoint, exam, exam_status: exam_status)

where exam_status is a status value (are those really integers?)

The exam would be the expect response in the suscription and the exam_status would be the suscription topic.

Thanks!