-
-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathcomment.ex
More file actions
95 lines (78 loc) · 1.89 KB
/
comment.ex
File metadata and controls
95 lines (78 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
defmodule AshGraphql.Test.Comment do
@moduledoc false
use Ash.Resource,
domain: AshGraphql.Test.Domain,
data_layer: Ash.DataLayer.Ets,
extensions: [AshGraphql.Resource]
graphql do
type :comment
queries do
list :list_comments, :read
action :list_ranked_comments, :ranked_comments
end
mutations do
create :create_comment, :create
end
end
actions do
default_accept(:*)
defaults([:create, :update, :destroy])
read :read do
primary?(true)
end
create :with_required do
argument(:text, :string, allow_nil?: false)
argument(:required, :string, allow_nil?: false)
change(set_attribute(:text, arg(:text)))
end
read :paginated do
pagination(required?: true, offset?: true, countable: true)
end
action :ranked_comments, {:array, RankedComment} do
run(fn _input, _ctx ->
res =
Ash.read!(__MODULE__)
|> Enum.with_index()
|> Enum.map(fn {c, i} ->
%{
rank: i,
comment: c
}
end)
{:ok, res}
end)
end
end
attributes do
uuid_primary_key(:id)
attribute(:text, :string, public?: true)
attribute :type, :atom do
public?(true)
writable?(false)
default(:comment)
constraints(one_of: [:comment, :reply])
end
create_timestamp(:created_at)
end
calculations do
calculate(
:timestamp,
:utc_datetime_usec,
expr(created_at),
public?: true
)
calculate :arg_returned,
:integer,
expr(^arg(:seconds)) do
argument(:seconds, :integer, allow_nil?: false)
public?(true)
end
end
relationships do
belongs_to(:post, AshGraphql.Test.Post, public?: true)
belongs_to :author, AshGraphql.Test.User do
public?(true)
attribute_writable?(true)
end
end
end