commit b42c31c25ce1fc12c2f710d55ee509b2b9ec63c0
parent e5e625004fe36357bfba88ff4fc7b1b2136ddcc4
Author: srfsh <dev@srf.sh>
Date: Tue, 22 Nov 2022 15:06:59 +0300
Zenflows.VF.{Duration,Measure}: fix preload
Only preload the field if all the sub-fields exist at the same time (non-nil).
Diffstat:
2 files changed, 20 insertions(+), 8 deletions(-)
diff --git a/src/zenflows/vf/duration.ex b/src/zenflows/vf/duration.ex
@@ -85,10 +85,16 @@ as a %Duration{} struct. Useful for GraphQL types as can be seen in
"""
@spec preload(Schema.t(), atom()) :: Schema.t()
def preload(schema, key) do
- %{schema | key => %__MODULE__{
- unit_type: Map.get(schema, :"#{key}_unit_type"),
- numeric_duration: Map.get(schema, :"#{key}_numeric_duration"),
- }}
+ unit_type = Map.get(schema, :"#{key}_unit_type")
+ numeric_duration = Map.get(schema, :"#{key}_numeric_duration")
+ if unit_type && numeric_duration do
+ %{schema | key => %__MODULE__{
+ unit_type: unit_type,
+ numeric_duration: numeric_duration,
+ }}
+ else
+ schema
+ end
end
@cast ~w[unit_type numeric_duration]a
diff --git a/src/zenflows/vf/measure.ex b/src/zenflows/vf/measure.ex
@@ -97,10 +97,16 @@ as a %Measure{} struct. Useful for GraphQL types as can be seen in
"""
@spec preload(Schema.t(), atom()) :: Schema.t()
def preload(schema, key) do
- %{schema | key => %__MODULE__{
- has_unit_id: Map.get(schema, :"#{key}_has_unit_id"),
- has_numerical_value: Map.get(schema, :"#{key}_has_numerical_value"),
- }}
+ has_unit_id = Map.get(schema, :"#{key}_has_unit_id")
+ has_numerical_value = Map.get(schema, :"#{key}_has_numerical_value")
+ if has_unit_id && has_numerical_value do
+ %{schema | key => %__MODULE__{
+ has_unit_id: has_unit_id,
+ has_numerical_value: has_numerical_value,
+ }}
+ else
+ schema
+ end
end
@cast ~w[has_unit_id has_numerical_value]a