|
| 1 | +defmodule LinearCli.Linear.ProjectUpdate do |
| 2 | + @moduledoc """ |
| 3 | + A Linear project update - a status post on a project (e.g. "This week's |
| 4 | + progress..."), distinct from editing the project's own fields. New in |
| 5 | + this port - Ruby has no equivalent. Created via the projectUpdateCreate |
| 6 | + mutation (schema/LinearAPI.graphql). |
| 7 | + """ |
| 8 | + |
| 9 | + use Ash.Resource, domain: LinearCli.Linear |
| 10 | + |
| 11 | + actions do |
| 12 | + create :create do |
| 13 | + argument :project_id, :string, allow_nil?: false |
| 14 | + argument :body, :string, allow_nil?: false |
| 15 | + argument :health, :string, allow_nil?: true |
| 16 | + manual LinearCli.Linear.ProjectUpdate.Create |
| 17 | + end |
| 18 | + end |
| 19 | + |
| 20 | + attributes do |
| 21 | + attribute :id, :string, primary_key?: true, allow_nil?: false, public?: true |
| 22 | + attribute :body, :string, public?: true |
| 23 | + attribute :health, :string, public?: true |
| 24 | + attribute :url, :string, public?: true |
| 25 | + end |
| 26 | + |
| 27 | + @doc "GraphQL field selection for a project update's own fields." |
| 28 | + def base_fields do |
| 29 | + "id body health url createdAt" |
| 30 | + end |
| 31 | + |
| 32 | + @doc false |
| 33 | + def from_map(map) do |
| 34 | + struct!(__MODULE__, |
| 35 | + id: map["id"], |
| 36 | + body: map["body"], |
| 37 | + health: map["health"], |
| 38 | + url: map["url"] |
| 39 | + ) |
| 40 | + end |
| 41 | +end |
| 42 | + |
| 43 | +defmodule LinearCli.Linear.ProjectUpdate.Create do |
| 44 | + @moduledoc false |
| 45 | + use Ash.Resource.ManualCreate |
| 46 | + |
| 47 | + alias LinearCli.Api |
| 48 | + alias LinearCli.Linear.ProjectUpdate |
| 49 | + |
| 50 | + def create(changeset, _opts, _context) do |
| 51 | + args = changeset.arguments |
| 52 | + |
| 53 | + input = |
| 54 | + %{"projectId" => args.project_id, "body" => args.body} |
| 55 | + |> maybe_put_health(args.health) |
| 56 | + |
| 57 | + case Api.call(document(), %{"input" => input}) do |
| 58 | + {:ok, %{"projectUpdateCreate" => %{"projectUpdate" => update_map}}} |
| 59 | + when is_map(update_map) -> |
| 60 | + {:ok, ProjectUpdate.from_map(update_map)} |
| 61 | + |
| 62 | + {:ok, other} -> |
| 63 | + {:error, {:unexpected_response, other}} |
| 64 | + |
| 65 | + {:error, reason} -> |
| 66 | + {:error, reason} |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + defp maybe_put_health(input, nil), do: input |
| 71 | + defp maybe_put_health(input, health), do: Map.put(input, "health", health) |
| 72 | + |
| 73 | + # A function, not a module attribute: ProjectUpdate.base_fields/0 reaches |
| 74 | + # into no other file today, but kept consistent with every other |
| 75 | + # document/0 in this codebase for the same reason they all are. |
| 76 | + defp document do |
| 77 | + "mutation($input: ProjectUpdateCreateInput!) { projectUpdateCreate(input: $input) { projectUpdate { #{ProjectUpdate.base_fields()} } } }" |
| 78 | + end |
| 79 | +end |
0 commit comments