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

refactor(schema): make schema article unrelated #388

Merged
merged 3 commits into from
Jun 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/groupher_server_web/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule GroupherServerWeb.Schema do
"""
use Absinthe.Schema
# use ApolloTracing
import GroupherServerWeb.Schema.Helper.Imports

alias GroupherServerWeb.Middleware, as: M
alias GroupherServerWeb.Schema.{Account, Billing, CMS, Delivery, Statistics, Helper}
Expand Down Expand Up @@ -38,11 +39,11 @@ defmodule GroupherServerWeb.Schema do
import_types(CMS.Queries)
import_types(CMS.Mutations.Community)
import_types(CMS.Mutations.Operation)
import_types(CMS.Mutations.Post)
import_types(CMS.Mutations.Job)
import_types(CMS.Mutations.Repo)

import_types(CMS.Mutations.Comment)

import_article_fields(:mutations, :module)

query do
import_fields(:account_queries)
import_fields(:billing_queries)
Expand All @@ -63,9 +64,8 @@ defmodule GroupherServerWeb.Schema do
# cms
import_fields(:cms_mutation_community)
import_fields(:cms_opertion_mutations)
import_fields(:cms_post_mutations)
import_fields(:cms_job_mutations)
import_fields(:cms_repo_mutations)

import_article_fields(:mutations)

import_fields(:cms_comment_mutations)
end
Expand Down
172 changes: 95 additions & 77 deletions lib/groupher_server_web/schema/Helper/fields.ex
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
defmodule GroupherServerWeb.Schema.Helper.Fields do
@moduledoc """
common fields
general fields used in schema definition
"""
import Helper.Utils, only: [get_config: 2]
import Absinthe.Resolution.Helpers, only: [dataloader: 2]

alias GroupherServer.CMS
alias GroupherServerWeb.Middleware, as: M
alias GroupherServerWeb.Resolvers, as: R

@page_size get_config(:general, :page_size)
@supported_emotions get_config(:article, :emotions)
@supported_comment_emotions get_config(:article, :comment_emotions)

@emotions get_config(:article, :emotions)
@comment_emotions get_config(:article, :comment_emotions)
@article_threads get_config(:article, :threads)

@doc "general article fields for grqphql resolve fields"
Expand Down Expand Up @@ -38,24 +41,95 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do
end
end

@doc """
generate thread enum based on @article_threads

e.g:

enum :post_thread, do: value(:post)
enum :job_thread, do: value(:job)
# ..
"""
defmacro article_thread_enums do
@article_threads
|> Enum.map(fn thread ->
quote do
enum(unquote(:"#{thread}_thread"), do: value(unquote(thread)))
|> Enum.map(
&quote do
enum(unquote(:"#{&1}_thread"), do: value(unquote(&1)))
end
end)
)
end

@doc """
generate thread value based on @article_threads

e.g:

value(:post)
value(:job)
# ...
"""
defmacro article_values do
@article_threads
|> Enum.map(fn thread ->
quote do
value(unquote(thread))
|> Enum.map(
&quote do
value(unquote(&1))
end
end)
)
end

@doc """
general emotion enum for articles
#NOTE: xxx_user_logins field is not support for gq-endpoint
"""
defmacro emotion_values(metric \\ :article) do
emotions =
case metric do
:comment -> @comment_emotions
_ -> @emotions
end

emotions
|> Enum.map(
&quote do
value(unquote(:"#{&1}"))
end
)
end

@doc """
general emotions for articles

e.g:
------
beer_count
viewer_has_beered
latest_bear_users
"""
defmacro emotion_fields() do
@emotions
|> Enum.map(
&quote do
field(unquote(:"#{&1}_count"), :integer)
field(unquote(:"viewer_has_#{&1}ed"), :boolean)
field(unquote(:"latest_#{&1}_users"), list_of(:simple_user))
end
)
end

defmacro emotion_fields(:comment) do
@comment_emotions
|> Enum.map(
&quote do
field(unquote(:"#{&1}_count"), :integer)
field(unquote(:"viewer_has_#{&1}ed"), :boolean)
field(unquote(:"latest_#{&1}_users"), list_of(:simple_user))
end
)
end

@doc """
general timestamp with active_at for article
"""
defmacro timestamp_fields(:article) do
quote do
field(:inserted_at, :datetime)
Expand All @@ -79,6 +153,9 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do
end
end

@doc """
general pagination fields except entries
"""
defmacro pagination_fields do
quote do
field(:total_count, :integer)
Expand All @@ -98,6 +175,9 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do
end
end

@doc """
general social used for user profile
"""
defmacro social_fields do
quote do
field(:qq, :string)
Expand All @@ -115,19 +195,13 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do
end
end

import Absinthe.Resolution.Helpers, only: [dataloader: 2]

alias GroupherServer.CMS
alias GroupherServerWeb.Middleware, as: M
alias GroupherServerWeb.Resolvers, as: R

defmacro threads_count_fields() do
@article_threads
|> Enum.map(fn thread ->
quote do
field(unquote(:"#{thread}s_count"), :integer)
|> Enum.map(
&quote do
field(unquote(:"#{&1}s_count"), :integer)
end
end)
)
end

# TODO: remove
Expand Down Expand Up @@ -216,62 +290,6 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do
end
end

@doc """
general emotion enum for articles
#NOTE: xxx_user_logins field is not support for gq-endpoint
"""
defmacro emotion_enum() do
@supported_emotions
|> Enum.map(fn emotion ->
quote do
value(unquote(:"#{emotion}"))
end
end)
end

@doc """
general emotion enum for comments
#NOTE: xxx_user_logins field is not support for gq-endpoint
"""
defmacro comment_emotion_enum() do
@supported_comment_emotions
|> Enum.map(fn emotion ->
quote do
value(unquote(:"#{emotion}"))
end
end)
end

@doc """
general emotions for articles
#NOTE: xxx_user_logins field is not support for gq-endpoint
"""
defmacro emotion_fields() do
@supported_emotions
|> Enum.map(fn emotion ->
quote do
field(unquote(:"#{emotion}_count"), :integer)
field(unquote(:"viewer_has_#{emotion}ed"), :boolean)
field(unquote(:"latest_#{emotion}_users"), list_of(:simple_user))
end
end)
end

@doc """
general emotions for comments
#NOTE: xxx_user_logins field is not support for gq-endpoint
"""
defmacro comment_emotion_fields() do
@supported_comment_emotions
|> Enum.map(fn emotion ->
quote do
field(unquote(:"#{emotion}_count"), :integer)
field(unquote(:"viewer_has_#{emotion}ed"), :boolean)
field(unquote(:"latest_#{emotion}_users"), list_of(:simple_user))
end
end)
end

@doc """
general collect folder meta info
"""
Expand Down
44 changes: 44 additions & 0 deletions lib/groupher_server_web/schema/Helper/imports.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
defmodule GroupherServerWeb.Schema.Helper.Imports do
@moduledoc """
helper for import cms article related fields
"""

import Helper.Utils, only: [get_config: 2]
@article_threads get_config(:article, :threads)

alias GroupherServerWeb.Schema.{CMS}

@doc """
import article fields based on @article_threads
e.g:
----
import_types(:cms_post_mutations)
import_types(:cms_job_mutations)
# ...
"""
defmacro import_article_fields(:mutations) do
@article_threads
|> Enum.map(
&quote do
import_fields(unquote(:"cms_#{&1}_mutations"))
end
)
end

@doc """
import article fields based on @article_threads
e.g:
----
import_types(CMS.Mutations.Post)
import_types(CMS.Mutations.Job)
# ...
"""
defmacro import_article_fields(:mutations, :module) do
@article_threads
|> Enum.map(
&quote do
import_types(unquote(Module.concat(CMS.Mutations, Recase.to_pascal(to_string(&1)))))
end
)
end
end
Loading