1
1
defmodule GroupherServerWeb.Schema.Helper.Fields do
2
2
@ moduledoc """
3
- common fields
3
+ general fields used in schema definition
4
4
"""
5
5
import Helper.Utils , only: [ get_config: 2 ]
6
+ import Absinthe.Resolution.Helpers , only: [ dataloader: 2 ]
6
7
7
8
alias GroupherServer.CMS
9
+ alias GroupherServerWeb.Middleware , as: M
10
+ alias GroupherServerWeb.Resolvers , as: R
8
11
9
12
@ page_size get_config ( :general , :page_size )
10
- @ supported_emotions get_config ( :article , :emotions )
11
- @ supported_comment_emotions get_config ( :article , :comment_emotions )
12
13
14
+ @ emotions get_config ( :article , :emotions )
15
+ @ comment_emotions get_config ( :article , :comment_emotions )
13
16
@ article_threads get_config ( :article , :threads )
14
17
15
18
@ doc "general article fields for grqphql resolve fields"
@@ -38,24 +41,95 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do
38
41
end
39
42
end
40
43
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
+ """
41
53
defmacro article_thread_enums do
42
54
@ 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 ) ) )
46
58
end
47
- end )
59
+ )
48
60
end
49
61
62
+ @ doc """
63
+ generate thread value based on @article_threads
64
+
65
+ e.g:
66
+
67
+ value(:post)
68
+ value(:job)
69
+ # ...
70
+ """
50
71
defmacro article_values do
51
72
@ article_threads
52
- |> Enum . map ( fn thread ->
53
- quote do
54
- value ( unquote ( thread ) )
73
+ |> Enum . map (
74
+ & quote do
75
+ value ( unquote ( & 1 ) )
55
76
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 ( metric \\ :article ) do
85
+ emotions =
86
+ case metric do
87
+ :comment -> @ comment_emotions
88
+ _ -> @ emotions
89
+ end
90
+
91
+ emotions
92
+ |> Enum . map (
93
+ & quote do
94
+ value ( unquote ( :"#{ & 1 } " ) )
95
+ end
96
+ )
97
+ end
98
+
99
+ @ doc """
100
+ general emotions for articles
101
+
102
+ e.g:
103
+ ------
104
+ beer_count
105
+ viewer_has_beered
106
+ latest_bear_users
107
+ """
108
+ defmacro emotion_fields ( ) do
109
+ @ emotions
110
+ |> Enum . map (
111
+ & quote do
112
+ field ( unquote ( :"#{ & 1 } _count" ) , :integer )
113
+ field ( unquote ( :"viewer_has_#{ & 1 } ed" ) , :boolean )
114
+ field ( unquote ( :"latest_#{ & 1 } _users" ) , list_of ( :simple_user ) )
115
+ end
116
+ )
117
+ end
118
+
119
+ defmacro emotion_fields ( :comment ) do
120
+ @ comment_emotions
121
+ |> Enum . map (
122
+ & quote do
123
+ field ( unquote ( :"#{ & 1 } _count" ) , :integer )
124
+ field ( unquote ( :"viewer_has_#{ & 1 } ed" ) , :boolean )
125
+ field ( unquote ( :"latest_#{ & 1 } _users" ) , list_of ( :simple_user ) )
126
+ end
127
+ )
57
128
end
58
129
130
+ @ doc """
131
+ general timestamp with active_at for article
132
+ """
59
133
defmacro timestamp_fields ( :article ) do
60
134
quote do
61
135
field ( :inserted_at , :datetime )
@@ -79,6 +153,9 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do
79
153
end
80
154
end
81
155
156
+ @ doc """
157
+ general pagination fields except entries
158
+ """
82
159
defmacro pagination_fields do
83
160
quote do
84
161
field ( :total_count , :integer )
@@ -98,6 +175,9 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do
98
175
end
99
176
end
100
177
178
+ @ doc """
179
+ general social used for user profile
180
+ """
101
181
defmacro social_fields do
102
182
quote do
103
183
field ( :qq , :string )
@@ -115,19 +195,13 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do
115
195
end
116
196
end
117
197
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
-
124
198
defmacro threads_count_fields ( ) do
125
199
@ article_threads
126
- |> Enum . map ( fn thread ->
127
- quote do
128
- field ( unquote ( :"#{ thread } s_count" ) , :integer )
200
+ |> Enum . map (
201
+ & quote do
202
+ field ( unquote ( :"#{ & 1 } s_count" ) , :integer )
129
203
end
130
- end )
204
+ )
131
205
end
132
206
133
207
# TODO: remove
@@ -216,62 +290,6 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do
216
290
end
217
291
end
218
292
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
-
275
293
@ doc """
276
294
general collect folder meta info
277
295
"""
0 commit comments