Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit 0552741

Browse files
committed
refactor(schema): wip
1 parent 02f4663 commit 0552741

File tree

4 files changed

+186
-87
lines changed

4 files changed

+186
-87
lines changed

lib/groupher_server_web/schema/Helper/fields.ex

Lines changed: 98 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
defmodule GroupherServerWeb.Schema.Helper.Fields do
22
@moduledoc """
3-
common fields
3+
general fields used in schema definition
44
"""
55
import Helper.Utils, only: [get_config: 2]
6+
import Absinthe.Resolution.Helpers, only: [dataloader: 2]
67

78
alias GroupherServer.CMS
9+
alias GroupherServerWeb.Middleware, as: M
10+
alias GroupherServerWeb.Resolvers, as: R
811

912
@page_size get_config(:general, :page_size)
10-
@supported_emotions get_config(:article, :emotions)
11-
@supported_comment_emotions get_config(:article, :comment_emotions)
1213

14+
@emotions get_config(:article, :emotions)
15+
@comment_emotions get_config(:article, :comment_emotions)
1316
@article_threads get_config(:article, :threads)
1417

1518
@doc "general article fields for grqphql resolve fields"
@@ -38,24 +41,98 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do
3841
end
3942
end
4043

44+
@doc """
45+
generate thread enum based on @article_threads
46+
47+
e.g:
48+
49+
enum :post_thread, do: value(:post)
50+
enum :job_thread, do: value(:job)
51+
# ..
52+
"""
4153
defmacro article_thread_enums do
4254
@article_threads
43-
|> Enum.map(fn thread ->
44-
quote do
45-
enum(unquote(:"#{thread}_thread"), do: value(unquote(thread)))
55+
|> Enum.map(
56+
&quote do
57+
enum(unquote(:"#{&1}_thread"), do: value(unquote(&1)))
4658
end
47-
end)
59+
)
4860
end
4961

62+
@doc """
63+
generate thread value based on @article_threads
64+
65+
e.g:
66+
67+
value(:post)
68+
value(:job)
69+
# ...
70+
"""
5071
defmacro article_values do
5172
@article_threads
52-
|> Enum.map(fn thread ->
53-
quote do
54-
value(unquote(thread))
73+
|> Enum.map(
74+
&quote do
75+
value(unquote(&1))
5576
end
56-
end)
77+
)
78+
end
79+
80+
@doc """
81+
general emotion enum for articles
82+
#NOTE: xxx_user_logins field is not support for gq-endpoint
83+
"""
84+
defmacro emotion_values() do
85+
@emotions
86+
|> Enum.map(
87+
&quote do
88+
value(unquote(:"#{&1}"))
89+
end
90+
)
91+
end
92+
93+
defmacro emotion_values(:comment) do
94+
@emotions
95+
|> Enum.map(
96+
&quote do
97+
value(unquote(:"#{&1}"))
98+
end
99+
)
100+
end
101+
102+
@doc """
103+
general emotions for articles
104+
105+
e.g:
106+
------
107+
beer_count
108+
viewer_has_beered
109+
latest_bear_users
110+
"""
111+
defmacro emotion_fields() do
112+
@emotions
113+
|> Enum.map(
114+
&quote do
115+
field(unquote(:"#{&1}_count"), :integer)
116+
field(unquote(:"viewer_has_#{&1}ed"), :boolean)
117+
field(unquote(:"latest_#{&1}_users"), list_of(:simple_user))
118+
end
119+
)
120+
end
121+
122+
defmacro emotion_fields(:comment) do
123+
@comment_emotions
124+
|> Enum.map(
125+
&quote do
126+
field(unquote(:"#{&1}_count"), :integer)
127+
field(unquote(:"viewer_has_#{&1}ed"), :boolean)
128+
field(unquote(:"latest_#{&1}_users"), list_of(:simple_user))
129+
end
130+
)
57131
end
58132

133+
@doc """
134+
general timestamp with active_at for article
135+
"""
59136
defmacro timestamp_fields(:article) do
60137
quote do
61138
field(:inserted_at, :datetime)
@@ -79,6 +156,9 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do
79156
end
80157
end
81158

159+
@doc """
160+
general pagination fields except entries
161+
"""
82162
defmacro pagination_fields do
83163
quote do
84164
field(:total_count, :integer)
@@ -98,6 +178,9 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do
98178
end
99179
end
100180

181+
@doc """
182+
general social used for user profile
183+
"""
101184
defmacro social_fields do
102185
quote do
103186
field(:qq, :string)
@@ -115,19 +198,13 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do
115198
end
116199
end
117200

118-
import Absinthe.Resolution.Helpers, only: [dataloader: 2]
119-
120-
alias GroupherServer.CMS
121-
alias GroupherServerWeb.Middleware, as: M
122-
alias GroupherServerWeb.Resolvers, as: R
123-
124201
defmacro threads_count_fields() do
125202
@article_threads
126-
|> Enum.map(fn thread ->
127-
quote do
128-
field(unquote(:"#{thread}s_count"), :integer)
203+
|> Enum.map(
204+
&quote do
205+
field(unquote(:"#{&1}s_count"), :integer)
129206
end
130-
end)
207+
)
131208
end
132209

133210
# TODO: remove
@@ -216,62 +293,6 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do
216293
end
217294
end
218295

219-
@doc """
220-
general emotion enum for articles
221-
#NOTE: xxx_user_logins field is not support for gq-endpoint
222-
"""
223-
defmacro emotion_enum() do
224-
@supported_emotions
225-
|> Enum.map(fn emotion ->
226-
quote do
227-
value(unquote(:"#{emotion}"))
228-
end
229-
end)
230-
end
231-
232-
@doc """
233-
general emotion enum for comments
234-
#NOTE: xxx_user_logins field is not support for gq-endpoint
235-
"""
236-
defmacro comment_emotion_enum() do
237-
@supported_comment_emotions
238-
|> Enum.map(fn emotion ->
239-
quote do
240-
value(unquote(:"#{emotion}"))
241-
end
242-
end)
243-
end
244-
245-
@doc """
246-
general emotions for articles
247-
#NOTE: xxx_user_logins field is not support for gq-endpoint
248-
"""
249-
defmacro emotion_fields() do
250-
@supported_emotions
251-
|> Enum.map(fn emotion ->
252-
quote do
253-
field(unquote(:"#{emotion}_count"), :integer)
254-
field(unquote(:"viewer_has_#{emotion}ed"), :boolean)
255-
field(unquote(:"latest_#{emotion}_users"), list_of(:simple_user))
256-
end
257-
end)
258-
end
259-
260-
@doc """
261-
general emotions for comments
262-
#NOTE: xxx_user_logins field is not support for gq-endpoint
263-
"""
264-
defmacro comment_emotion_fields() do
265-
@supported_comment_emotions
266-
|> Enum.map(fn emotion ->
267-
quote do
268-
field(unquote(:"#{emotion}_count"), :integer)
269-
field(unquote(:"viewer_has_#{emotion}ed"), :boolean)
270-
field(unquote(:"latest_#{emotion}_users"), list_of(:simple_user))
271-
end
272-
end)
273-
end
274-
275296
@doc """
276297
general collect folder meta info
277298
"""

lib/groupher_server_web/schema/Helper/mutations.ex

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
11
defmodule GroupherServerWeb.Schema.Helper.Mutations do
22
@moduledoc """
3-
common fields
3+
general mutations used for articles
4+
5+
e.g:
6+
in schema/cms/mutation/post.ex
7+
8+
add following:
9+
article_upvote_mutation(:post)
10+
11+
post will have two mutation endpoint:
12+
13+
upvote_post
14+
unto_emotion_post
15+
16+
same for the job/repo .. article thread
417
"""
518
alias GroupherServerWeb.Middleware, as: M
619
alias GroupherServerWeb.Resolvers, as: R
720

21+
@doc """
22+
upvote mutation for article
23+
24+
include:
25+
-----
26+
upvote_[thread]
27+
unto_emotion_[thread]
28+
"""
829
defmacro article_upvote_mutation(thread) do
930
quote do
1031
@desc unquote("upvote to #{thread}")
@@ -27,6 +48,14 @@ defmodule GroupherServerWeb.Schema.Helper.Mutations do
2748
end
2849
end
2950

51+
@doc """
52+
pin mutation for article
53+
54+
include:
55+
-----
56+
pin_[thread]
57+
unto_pin_[thread]
58+
"""
3059
defmacro article_pin_mutation(thread) do
3160
quote do
3261
@desc unquote("pin to #{thread}")
@@ -55,6 +84,14 @@ defmodule GroupherServerWeb.Schema.Helper.Mutations do
5584
end
5685
end
5786

87+
@doc """
88+
mark delete mutation for article
89+
90+
include:
91+
-----
92+
mark_delete_[thread]
93+
unto_mark_delete_[thread]
94+
"""
5895
defmacro article_mark_delete_mutation(thread) do
5996
quote do
6097
@desc unquote("mark delete a #{thread} type article, aka soft-delete")
@@ -81,6 +118,14 @@ defmodule GroupherServerWeb.Schema.Helper.Mutations do
81118
end
82119
end
83120

121+
@doc """
122+
delete mutation for article
123+
124+
include:
125+
-----
126+
delete_[thread]
127+
mark_delete_[thread]
128+
"""
84129
# TODO: if post belongs to multi communities, unset instead delete
85130
defmacro article_delete_mutation(thread) do
86131
quote do
@@ -97,6 +142,14 @@ defmodule GroupherServerWeb.Schema.Helper.Mutations do
97142
end
98143
end
99144

145+
@doc """
146+
emotion mutation for article
147+
148+
include:
149+
-----
150+
emotion_to_[thread]
151+
unto_emotion_to_[thread]
152+
"""
100153
defmacro article_emotion_mutation(thread) do
101154
quote do
102155
@desc unquote("emotion to #{thread}")
@@ -121,6 +174,14 @@ defmodule GroupherServerWeb.Schema.Helper.Mutations do
121174
end
122175
end
123176

177+
@doc """
178+
report mutation for article
179+
180+
include:
181+
-----
182+
report_[thread]
183+
undo_report_[thread]
184+
"""
124185
defmacro article_report_mutation(thread) do
125186
quote do
126187
@desc unquote("report a #{thread}")
@@ -145,6 +206,14 @@ defmodule GroupherServerWeb.Schema.Helper.Mutations do
145206
end
146207
end
147208

209+
@doc """
210+
sink mutation for article
211+
212+
include:
213+
-----
214+
sink_[thread]
215+
undo_sink_[thread]
216+
"""
148217
defmacro article_sink_mutation(thread) do
149218
quote do
150219
@desc unquote("sink a #{thread}")
@@ -173,9 +242,17 @@ defmodule GroupherServerWeb.Schema.Helper.Mutations do
173242
end
174243
end
175244

245+
@doc """
246+
lock comment of a article
247+
248+
include:
249+
-----
250+
lock_[thread]_comment
251+
undo_lock_[thread]_comment
252+
"""
176253
defmacro article_lock_comment_mutation(thread) do
177254
quote do
178-
@desc unquote("lock comment to a #{thread}")
255+
@desc unquote("lock comment of a #{thread}")
179256
field unquote(:"lock_#{thread}_comment"), :article do
180257
arg(:id, non_null(:id))
181258
arg(:community_id, non_null(:id))

0 commit comments

Comments
 (0)