From 0988ff390cc9fc37fe6cf68271ac5fcae14e622c Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Fri, 25 Jan 2019 15:41:10 +0100 Subject: [PATCH] Move queries and mutations to submodules Signed-off-by: Thomas Citharel --- lib/mobilizon_web/schema.ex | 215 ++---------------- lib/mobilizon_web/schema/actors/group.ex | 1 + lib/mobilizon_web/schema/actors/person.ex | 32 ++- lib/mobilizon_web/schema/comment.ex | 11 + lib/mobilizon_web/schema/event.ex | 46 ++++ lib/mobilizon_web/schema/events/category.ex | 20 ++ .../schema/events/participant.ex | 11 + lib/mobilizon_web/schema/user.ex | 66 ++++++ lib/mobilizon_web/schema/utils.ex | 21 ++ 9 files changed, 220 insertions(+), 203 deletions(-) create mode 100644 lib/mobilizon_web/schema/utils.ex diff --git a/lib/mobilizon_web/schema.ex b/lib/mobilizon_web/schema.ex index b0a0d16e5..564522fe3 100644 --- a/lib/mobilizon_web/schema.ex +++ b/lib/mobilizon_web/schema.ex @@ -13,6 +13,7 @@ defmodule MobilizonWeb.Schema do import_types(Absinthe.Type.Custom) import_types(Absinthe.Plug.Types) + import_types(MobilizonWeb.Schema.UserType) import_types(MobilizonWeb.Schema.ActorInterface) import_types(MobilizonWeb.Schema.Actors.PersonType) import_types(MobilizonWeb.Schema.Actors.GroupType) @@ -116,20 +117,6 @@ defmodule MobilizonWeb.Schema do Root Query """ query do - @desc "Get all events" - field :events, list_of(:event) do - arg(:page, :integer, default_value: 1) - arg(:limit, :integer, default_value: 10) - resolve(&Resolvers.Event.list_events/3) - end - - @desc "Get all groups" - field :groups, list_of(:group) do - arg(:page, :integer, default_value: 1) - arg(:limit, :integer, default_value: 10) - resolve(&Resolvers.Group.list_groups/3) - end - @desc "Search through events, persons and groups" field :search, list_of(:search_result) do arg(:search, non_null(:string)) @@ -138,180 +125,24 @@ defmodule MobilizonWeb.Schema do resolve(&Resolvers.Event.search_events_and_actors/3) end - @desc "Get an event by uuid" - field :event, :event do - arg(:uuid, non_null(:uuid)) - resolve(&Resolvers.Event.find_event/3) - end - - @desc "Get all participants for an event uuid" - field :participants, list_of(:participant) do - arg(:uuid, non_null(:uuid)) - arg(:page, :integer, default_value: 1) - arg(:limit, :integer, default_value: 10) - resolve(&Resolvers.Event.list_participants_for_event/3) - end - - @desc "Get a group by it's preferred username" - field :group, :group do - arg(:preferred_username, non_null(:string)) - resolve(&Resolvers.Group.find_group/3) - end - - @desc "Get an user" - field :user, :user do - arg(:id, non_null(:id)) - resolve(&Resolvers.User.find_user/3) - end - - @desc "Get the current user" - field :logged_user, :user do - resolve(&Resolvers.User.get_current_user/3) - end - - @desc "Get the current actor for the logged-in user" - field :logged_person, :person do - resolve(&Resolvers.Person.get_current_person/3) - end - - @desc "Get a person by it's preferred username" - field :person, :person do - arg(:preferred_username, non_null(:string)) - resolve(&Resolvers.Person.find_person/3) - end - - @desc "Get the persons for an user" - field :identities, list_of(:person) do - resolve(&Resolvers.Person.identities/3) - end - - @desc "Get the list of categories" - field :categories, non_null(list_of(:category)) do - arg(:page, :integer, default_value: 1) - arg(:limit, :integer, default_value: 10) - resolve(&Resolvers.Category.list_categories/3) - end + import_fields(:user_queries) + import_fields(:person_queries) + import_fields(:group_queries) + import_fields(:event_queries) + import_fields(:participant_queries) + import_fields(:category_queries) end @desc """ Root Mutation """ mutation do - @desc "Create an event" - field :create_event, type: :event do - arg(:title, non_null(:string)) - arg(:description, non_null(:string)) - arg(:begins_on, non_null(:datetime)) - arg(:ends_on, :datetime) - arg(:state, :integer) - arg(:status, :integer) - arg(:public, :boolean) - arg(:thumbnail, :string) - arg(:large_image, :string) - arg(:publish_at, :datetime) - arg(:online_address, :string) - arg(:phone_address, :string) - arg(:organizer_actor_id, non_null(:id)) - arg(:category, non_null(:string)) - - resolve(&Resolvers.Event.create_event/3) - end - - @desc "Delete an event" - field :delete_event, :deleted_object do - arg(:event_id, non_null(:integer)) - arg(:actor_id, non_null(:integer)) - - resolve(&Resolvers.Event.delete_event/3) - end - - @desc "Create a comment" - field :create_comment, type: :comment do - arg(:text, non_null(:string)) - arg(:actor_username, non_null(:string)) - - resolve(&Resolvers.Comment.create_comment/3) - end - - @desc "Create a category with a title, description and picture" - field :create_category, type: :category do - arg(:title, non_null(:string)) - arg(:description, non_null(:string)) - arg(:picture, non_null(:upload)) - resolve(&Resolvers.Category.create_category/3) - end - - @desc "Create an user" - field :create_user, type: :user do - arg(:email, non_null(:string)) - arg(:password, non_null(:string)) - - resolve(handle_errors(&Resolvers.User.create_user/3)) - end - - @desc "Validate an user after registration" - field :validate_user, type: :login do - arg(:token, non_null(:string)) - resolve(&Resolvers.User.validate_user/3) - end - - @desc "Resend registration confirmation token" - field :resend_confirmation_email, type: :string do - arg(:email, non_null(:string)) - arg(:locale, :string, default_value: "en") - resolve(&Resolvers.User.resend_confirmation_email/3) - end - - @desc "Send a link through email to reset user password" - field :send_reset_password, type: :string do - arg(:email, non_null(:string)) - arg(:locale, :string, default_value: "en") - resolve(&Resolvers.User.send_reset_password/3) - end - - @desc "Reset user password" - field :reset_password, type: :login do - arg(:token, non_null(:string)) - arg(:password, non_null(:string)) - arg(:locale, :string, default_value: "en") - resolve(&Resolvers.User.reset_password/3) - end - - @desc "Login an user" - field :login, :login do - arg(:email, non_null(:string)) - arg(:password, non_null(:string)) - resolve(&Resolvers.User.login_user/3) - end - - @desc "Change default actor for user" - field :change_default_actor, :user do - arg(:preferred_username, non_null(:string)) - resolve(&Resolvers.User.change_default_actor/3) - end - - @desc "Create a new person for user" - field :create_person, :person do - arg(:preferred_username, non_null(:string)) - arg(:name, :string, description: "The displayed name for the new profile") - - arg(:description, :string, description: "The summary for the new profile", default_value: "") - - resolve(&Resolvers.Person.create_person/3) - end - - @desc "Create a group" - field :create_group, :group do - arg(:preferred_username, non_null(:string), description: "The name for the group") - arg(:name, :string, description: "The displayed name for the group") - arg(:description, :string, description: "The summary for the group", default_value: "") - - arg(:admin_actor_username, :string, - description: "The actor's username which will be the admin (otherwise user's default one)" - ) - - resolve(&Resolvers.Group.create_group/3) - end + import_fields(:user_mutations) + import_fields(:person_mutations) + import_fields(:group_mutations) + import_fields(:event_mutations) + import_fields(:category_mutations) + import_fields(:comment_mutations) # @desc "Upload a picture" # field :upload_picture, :picture do @@ -319,24 +150,4 @@ defmodule MobilizonWeb.Schema do # resolve(&Resolvers.Upload.upload_picture/3) # end end - - def handle_errors(fun) do - fn source, args, info -> - case Absinthe.Resolution.call(fun, source, args, info) do - {:error, %Ecto.Changeset{} = changeset} -> format_changeset(changeset) - val -> val - end - end - end - - def format_changeset(changeset) do - # {:error, [email: {"has already been taken", []}]} - errors = - changeset.errors - |> Enum.map(fn {_key, {value, context}} -> - [message: "#{value}", details: context] - end) - - {:error, errors} - end end diff --git a/lib/mobilizon_web/schema/actors/group.ex b/lib/mobilizon_web/schema/actors/group.ex index 120655939..7a24382a9 100644 --- a/lib/mobilizon_web/schema/actors/group.ex +++ b/lib/mobilizon_web/schema/actors/group.ex @@ -5,6 +5,7 @@ defmodule MobilizonWeb.Schema.Actors.GroupType do use Absinthe.Schema.Notation import Absinthe.Resolution.Helpers, only: [dataloader: 1] import_types(MobilizonWeb.Schema.Actors.MemberType) + alias MobilizonWeb.Resolvers @desc """ Represents a group of actors diff --git a/lib/mobilizon_web/schema/actors/person.ex b/lib/mobilizon_web/schema/actors/person.ex index 24acaba5e..70610a324 100644 --- a/lib/mobilizon_web/schema/actors/person.ex +++ b/lib/mobilizon_web/schema/actors/person.ex @@ -4,8 +4,8 @@ defmodule MobilizonWeb.Schema.Actors.PersonType do """ use Absinthe.Schema.Notation import Absinthe.Resolution.Helpers, only: [dataloader: 1] - import_types(MobilizonWeb.Schema.UserType) alias Mobilizon.Events + alias MobilizonWeb.Resolvers @desc """ Represents a person identity @@ -46,4 +46,34 @@ defmodule MobilizonWeb.Schema.Actors.PersonType do description: "A list of the events this actor has organized" ) end + + object :person_queries do + @desc "Get the current actor for the logged-in user" + field :logged_person, :person do + resolve(&Resolvers.Person.get_current_person/3) + end + + @desc "Get a person by it's preferred username" + field :person, :person do + arg(:preferred_username, non_null(:string)) + resolve(&Resolvers.Person.find_person/3) + end + + @desc "Get the persons for an user" + field :identities, list_of(:person) do + resolve(&Resolvers.Person.identities/3) + end + end + + object :person_mutations do + @desc "Create a new person for user" + field :create_person, :person do + arg(:preferred_username, non_null(:string)) + arg(:name, :string, description: "The displayed name for the new profile") + + arg(:description, :string, description: "The summary for the new profile", default_value: "") + + resolve(&Resolvers.Person.create_person/3) + end + end end diff --git a/lib/mobilizon_web/schema/comment.ex b/lib/mobilizon_web/schema/comment.ex index 61a4569fa..196347782 100644 --- a/lib/mobilizon_web/schema/comment.ex +++ b/lib/mobilizon_web/schema/comment.ex @@ -3,6 +3,7 @@ defmodule MobilizonWeb.Schema.CommentType do Schema representation for Comment """ use Absinthe.Schema.Notation + alias MobilizonWeb.Resolvers.Comment @desc "A comment" object :comment do @@ -29,4 +30,14 @@ defmodule MobilizonWeb.Schema.CommentType do value(:moderated, description: "Visible only after a moderator accepted") value(:invite, description: "visible only to people invited") end + + object :comment_mutations do + @desc "Create a comment" + field :create_comment, type: :comment do + arg(:text, non_null(:string)) + arg(:actor_username, non_null(:string)) + + resolve(&Comment.create_comment/3) + end + end end diff --git a/lib/mobilizon_web/schema/event.ex b/lib/mobilizon_web/schema/event.ex index a06409243..003dee4bf 100644 --- a/lib/mobilizon_web/schema/event.ex +++ b/lib/mobilizon_web/schema/event.ex @@ -8,6 +8,7 @@ defmodule MobilizonWeb.Schema.EventType do import_types(MobilizonWeb.Schema.AddressType) import_types(MobilizonWeb.Schema.Events.ParticipantType) import_types(MobilizonWeb.Schema.Events.CategoryType) + alias MobilizonWeb.Resolvers @desc "An event" object :event do @@ -70,4 +71,49 @@ defmodule MobilizonWeb.Schema.EventType do value(:confirmed, description: "The event is confirmed") value(:cancelled, description: "The event is cancelled") end + + object :event_queries do + @desc "Get all events" + field :events, list_of(:event) do + arg(:page, :integer, default_value: 1) + arg(:limit, :integer, default_value: 10) + resolve(&Resolvers.Event.list_events/3) + end + + @desc "Get an event by uuid" + field :event, :event do + arg(:uuid, non_null(:uuid)) + resolve(&Resolvers.Event.find_event/3) + end + end + + object :event_mutations do + @desc "Create an event" + field :create_event, type: :event do + arg(:title, non_null(:string)) + arg(:description, non_null(:string)) + arg(:begins_on, non_null(:datetime)) + arg(:ends_on, :datetime) + arg(:state, :integer) + arg(:status, :integer) + arg(:public, :boolean) + arg(:thumbnail, :string) + arg(:large_image, :string) + arg(:publish_at, :datetime) + arg(:online_address, :string) + arg(:phone_address, :string) + arg(:organizer_actor_id, non_null(:id)) + arg(:category, non_null(:string)) + + resolve(&Resolvers.Event.create_event/3) + end + + @desc "Delete an event" + field :delete_event, :deleted_object do + arg(:event_id, non_null(:integer)) + arg(:actor_id, non_null(:integer)) + + resolve(&Resolvers.Event.delete_event/3) + end + end end diff --git a/lib/mobilizon_web/schema/events/category.ex b/lib/mobilizon_web/schema/events/category.ex index 25e263fe6..521000ac7 100644 --- a/lib/mobilizon_web/schema/events/category.ex +++ b/lib/mobilizon_web/schema/events/category.ex @@ -3,6 +3,7 @@ defmodule MobilizonWeb.Schema.Events.CategoryType do Schema representation for Category """ use Absinthe.Schema.Notation + alias MobilizonWeb.Resolvers @desc "A category" object :category do @@ -11,4 +12,23 @@ defmodule MobilizonWeb.Schema.Events.CategoryType do field(:picture, :picture, description: "The category's picture") field(:title, :string, description: "The category's title") end + + object :category_queries do + @desc "Get the list of categories" + field :categories, non_null(list_of(:category)) do + arg(:page, :integer, default_value: 1) + arg(:limit, :integer, default_value: 10) + resolve(&Resolvers.Category.list_categories/3) + end + end + + object :category_mutations do + @desc "Create a category with a title, description and picture" + field :create_category, type: :category do + arg(:title, non_null(:string)) + arg(:description, non_null(:string)) + arg(:picture, non_null(:upload)) + resolve(&Resolvers.Category.create_category/3) + end + end end diff --git a/lib/mobilizon_web/schema/events/participant.ex b/lib/mobilizon_web/schema/events/participant.ex index 47a6b36b9..f811299af 100644 --- a/lib/mobilizon_web/schema/events/participant.ex +++ b/lib/mobilizon_web/schema/events/participant.ex @@ -4,6 +4,7 @@ defmodule MobilizonWeb.Schema.Events.ParticipantType do """ use Absinthe.Schema.Notation import Absinthe.Resolution.Helpers, only: [dataloader: 1] + alias MobilizonWeb.Resolvers @desc "Represents a participant to an event" object :participant do @@ -15,4 +16,14 @@ defmodule MobilizonWeb.Schema.Events.ParticipantType do field(:actor, :actor, description: "The actor that participates to the event") field(:role, :integer, description: "The role of this actor at this event") end + + object :participant_queries do + @desc "Get all participants for an event uuid" + field :participants, list_of(:participant) do + arg(:uuid, non_null(:uuid)) + arg(:page, :integer, default_value: 1) + arg(:limit, :integer, default_value: 10) + resolve(&Resolvers.Event.list_participants_for_event/3) + end + end end diff --git a/lib/mobilizon_web/schema/user.ex b/lib/mobilizon_web/schema/user.ex index fa34d5aa4..c0eba4562 100644 --- a/lib/mobilizon_web/schema/user.ex +++ b/lib/mobilizon_web/schema/user.ex @@ -3,6 +3,8 @@ defmodule MobilizonWeb.Schema.UserType do Schema representation for User """ use Absinthe.Schema.Notation + alias MobilizonWeb.Resolvers.User + import MobilizonWeb.Schema.Utils @desc "A local user of Mobilizon" object :user do @@ -33,4 +35,68 @@ defmodule MobilizonWeb.Schema.UserType do description: "The token sent when requesting password token" ) end + + object :user_queries do + @desc "Get an user" + field :user, :user do + arg(:id, non_null(:id)) + resolve(&User.find_user/3) + end + + @desc "Get the current user" + field :logged_user, :user do + resolve(&User.get_current_user/3) + end + end + + object :user_mutations do + @desc "Create an user" + field :create_user, type: :user do + arg(:email, non_null(:string)) + arg(:password, non_null(:string)) + + resolve(handle_errors(&User.create_user/3)) + end + + @desc "Validate an user after registration" + field :validate_user, type: :login do + arg(:token, non_null(:string)) + resolve(&User.validate_user/3) + end + + @desc "Resend registration confirmation token" + field :resend_confirmation_email, type: :string do + arg(:email, non_null(:string)) + arg(:locale, :string, default_value: "en") + resolve(&User.resend_confirmation_email/3) + end + + @desc "Send a link through email to reset user password" + field :send_reset_password, type: :string do + arg(:email, non_null(:string)) + arg(:locale, :string, default_value: "en") + resolve(&User.send_reset_password/3) + end + + @desc "Reset user password" + field :reset_password, type: :login do + arg(:token, non_null(:string)) + arg(:password, non_null(:string)) + arg(:locale, :string, default_value: "en") + resolve(&User.reset_password/3) + end + + @desc "Login an user" + field :login, :login do + arg(:email, non_null(:string)) + arg(:password, non_null(:string)) + resolve(&User.login_user/3) + end + + @desc "Change default actor for user" + field :change_default_actor, :user do + arg(:preferred_username, non_null(:string)) + resolve(&User.change_default_actor/3) + end + end end diff --git a/lib/mobilizon_web/schema/utils.ex b/lib/mobilizon_web/schema/utils.ex new file mode 100644 index 000000000..a2bf553df --- /dev/null +++ b/lib/mobilizon_web/schema/utils.ex @@ -0,0 +1,21 @@ +defmodule MobilizonWeb.Schema.Utils do + def handle_errors(fun) do + fn source, args, info -> + case Absinthe.Resolution.call(fun, source, args, info) do + {:error, %Ecto.Changeset{} = changeset} -> format_changeset(changeset) + val -> val + end + end + end + + def format_changeset(changeset) do + # {:error, [email: {"has already been taken", []}]} + errors = + changeset.errors + |> Enum.map(fn {_key, {value, context}} -> + [message: "#{value}", details: context] + end) + + {:error, errors} + end +end