zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

register_triggers.ex (1851B)


      1 defmodule Absinthe.Phase.Schema.RegisterTriggers do
      2   @moduledoc false
      3 
      4   use Absinthe.Phase
      5 
      6   def run(blueprint, _opts) do
      7     %{schema_definitions: [schema]} = blueprint
      8 
      9     subscription_object =
     10       Enum.find(schema.type_definitions, fn type ->
     11         type.identifier == :subscription
     12       end)
     13 
     14     mutation_object =
     15       Enum.find(schema.type_definitions, fn type ->
     16         type.identifier == :mutation
     17       end)
     18 
     19     mutation_object =
     20       if subscription_object && mutation_object do
     21         mutation_object
     22         |> register_triggers(subscription_object.fields)
     23         |> setup_middleware
     24       else
     25         # TODO: return errors if there isn't a mutation field that is on the
     26         # triggers list
     27         mutation_object
     28       end
     29 
     30     schema =
     31       Map.update!(schema, :type_definitions, fn definitions ->
     32         Enum.map(definitions, fn
     33           %{identifier: :subscription} -> subscription_object
     34           %{identifier: :mutation} -> mutation_object
     35           type -> type
     36         end)
     37       end)
     38 
     39     {:ok, %{blueprint | schema_definitions: [schema]}}
     40   end
     41 
     42   defp register_triggers(mutation_object, sub_fields) do
     43     update_fields(mutation_object, fn mut_field ->
     44       triggers =
     45         for sub_field <- sub_fields,
     46             sub_triggers = Absinthe.Type.function(sub_field, :triggers),
     47             is_map(sub_triggers),
     48             Map.has_key?(sub_triggers, mut_field.identifier),
     49             do: sub_field.identifier
     50 
     51       %{mut_field | triggers: triggers}
     52     end)
     53   end
     54 
     55   defp setup_middleware(mutation_object) do
     56     update_fields(mutation_object, fn field ->
     57       Map.update!(field, :middleware, &Absinthe.Subscription.add_middleware/1)
     58     end)
     59   end
     60 
     61   defp update_fields(mutation_object, fun) do
     62     Map.update!(mutation_object, :fields, fn fields ->
     63       Enum.map(fields, fun)
     64     end)
     65   end
     66 end