Skip to content

Commit

Permalink
Define Status in GraphQL schema
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonVides committed Sep 26, 2022
1 parent b3c25aa commit e365b77
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 18 deletions.
10 changes: 5 additions & 5 deletions big_tests/tests/graphql_domain_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ create_domain(DomainName, Config) ->
ParsedResult = get_ok_value([data, domain, addDomain], Result),
?assertEqual(#{<<"domain">> => DomainName,
<<"hostType">> => ?HOST_TYPE,
<<"enabled">> => null}, ParsedResult).
<<"status">> => null}, ParsedResult).

unknown_host_type_error_formatting(Config) ->
DomainName = ?EXAMPLE_DOMAIN,
Expand Down Expand Up @@ -143,14 +143,14 @@ wrong_host_type_error_formatting(Config) ->
disable_domain(Config) ->
Result = disable_domain(?EXAMPLE_DOMAIN, Config),
ParsedResult = get_ok_value([data, domain, disableDomain], Result),
?assertMatch(#{<<"domain">> := ?EXAMPLE_DOMAIN, <<"enabled">> := false}, ParsedResult),
?assertMatch(#{<<"domain">> := ?EXAMPLE_DOMAIN, <<"status">> := <<"DISABLED">>}, ParsedResult),
{ok, Domain} = rpc(mim(), mongoose_domain_sql, select_domain, [?EXAMPLE_DOMAIN]),
?assertEqual(#{host_type => ?HOST_TYPE, status => disabled}, Domain).

enable_domain(Config) ->
Result = enable_domain(?EXAMPLE_DOMAIN, Config),
ParsedResult = get_ok_value([data, domain, enableDomain], Result),
?assertMatch(#{<<"domain">> := ?EXAMPLE_DOMAIN, <<"enabled">> := true}, ParsedResult).
?assertMatch(#{<<"domain">> := ?EXAMPLE_DOMAIN, <<"status">> := <<"ENABLED">>}, ParsedResult).

get_domains_by_host_type(Config) ->
Result = get_domains_by_host_type(?HOST_TYPE, Config),
Expand All @@ -163,7 +163,7 @@ get_domain_details(Config) ->
ParsedResult = get_ok_value([data, domain, domainDetails], Result),
?assertEqual(#{<<"domain">> => ?EXAMPLE_DOMAIN,
<<"hostType">> => ?HOST_TYPE,
<<"enabled">> => true}, ParsedResult).
<<"status">> => <<"ENABLED">>}, ParsedResult).

delete_domain(Config) ->
Result1 = remove_domain(?EXAMPLE_DOMAIN, ?HOST_TYPE, Config),
Expand Down Expand Up @@ -202,7 +202,7 @@ domain_admin_get_domain_details(Config) ->
ParsedResult = get_ok_value([data, domain, domainDetails], Result),
?assertEqual(#{<<"domain">> => ?DOMAIN_ADMIN_EXAMPLE_DOMAIN,
<<"hostType">> => ?HOST_TYPE,
<<"enabled">> => true}, ParsedResult).
<<"status">> => <<"ENABLED">>}, ParsedResult).

domain_admin_set_domain_password(Config) ->
Result = set_domain_password(?DOMAIN_ADMIN_EXAMPLE_DOMAIN, <<"secret">>, Config),
Expand Down
11 changes: 10 additions & 1 deletion priv/graphql/schemas/global/domain.gql
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,14 @@ type Domain{
"Domain hostType"
hostType: String
"Is domain enabled?"
enabled: Boolean
status: DomainStatus
}

enum DomainStatus {
"Domain is enabled and ready to route traffic"
ENABLED
"Domain is disabled and won't be loaded into MongooseIM"
DISABLED
"Domain has been marked for deletion and is disabled until all data is removed"
DELETING
}
3 changes: 2 additions & 1 deletion src/domain/mongoose_domain_api.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@
-ignore_xref([get_all_dynamic/0]).
-ignore_xref([stop/0]).

-type status() :: enabled | disabled | deleting.
-type domain() :: jid:lserver().
-type host_type() :: mongooseim:host_type().
-type subdomain_pattern() :: mongoose_subdomain_utils:subdomain_pattern().

-export_type([status/0]).

-spec init() -> ok | {error, term()}.
init() ->
Expand Down
9 changes: 4 additions & 5 deletions src/domain/mongoose_domain_sql.erl
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@

-import(mongoose_rdbms, [prepare/4, execute_successfully/3]).

-type status() :: enabled | disabled | deleting.
-type event_id() :: non_neg_integer().
-type domain() :: binary().
-type domain() :: jid:lserver().
-type row() :: {event_id(), domain(), mongooseim:host_type() | null}.
-export_type([row/0]).

Expand Down Expand Up @@ -331,7 +330,7 @@ set_domain_for_deletion_settings(Pool, Domain) ->
ExtStatus = status_to_int(deleting),
execute_successfully(Pool, domain_update_settings_status, [ExtStatus, Domain]).

-spec set_status(domain(), status()) -> ok | {error, term()}.
-spec set_status(domain(), mongoose_domain_api:status()) -> ok | {error, term()}.
set_status(Domain, Status) ->
transaction(fun(Pool) ->
case select_domain(Domain) of
Expand Down Expand Up @@ -359,12 +358,12 @@ row_to_map({HostType, Status}) ->
IntStatus = mongoose_rdbms:result_to_integer(Status),
#{host_type => HostType, status => int_to_status(IntStatus)}.

-spec int_to_status(0..2) -> status().
-spec int_to_status(0..2) -> mongoose_domain_api:status().
int_to_status(0) -> disabled;
int_to_status(1) -> enabled;
int_to_status(2) -> deleting.

-spec status_to_int(status()) -> 0..2.
-spec status_to_int(mongoose_domain_api:status()) -> 0..2.
status_to_int(disabled) -> 0;
status_to_int(enabled) -> 1;
status_to_int(deleting) -> 2.
Expand Down
4 changes: 2 additions & 2 deletions src/graphql/admin/mongoose_graphql_domain_admin_mutation.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ execute(_Ctx, admin, <<"removeDomain">>, #{<<"domain">> := Domain, <<"hostType">
execute(_Ctx, admin, <<"enableDomain">>, #{<<"domain">> := Domain}) ->
case mongoose_domain_api:enable_domain(Domain) of
ok ->
{ok, #domain{enabled = true, domain = Domain}};
{ok, #domain{status = enabled, domain = Domain}};
{error, Error} ->
error_handler(Error, Domain, <<>>)
end;
execute(_Ctx, admin, <<"disableDomain">>, #{<<"domain">> := Domain}) ->
case mongoose_domain_api:disable_domain(Domain) of
ok ->
{ok, #domain{enabled = false, domain = Domain}};
{ok, #domain{status = disabled, domain = Domain}};
{error, Error} ->
error_handler(Error, Domain, <<>>)
end;
Expand Down
3 changes: 1 addition & 2 deletions src/graphql/admin/mongoose_graphql_domain_admin_query.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ execute(_Ctx, admin, <<"domainsByHostType">>, #{<<"hostType">> := HostType}) ->
execute(_Ctx, admin, <<"domainDetails">>, #{<<"domain">> := Domain}) ->
case mongoose_domain_sql:select_domain(Domain) of
{ok, #{host_type := HostType, status := Status}} ->
{ok, #domain{host_type = HostType, domain = Domain,
enabled = Status =:= enabled}};
{ok, #domain{host_type = HostType, domain = Domain, status = Status}};
{error, not_found} ->
{error, #{what => domain_not_found, domain => Domain}}
end.
2 changes: 1 addition & 1 deletion src/graphql/mongoose_graphql_domain.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

execute(_Ctx, #domain{host_type = HostType}, <<"hostType">>, _Args) ->
{ok, HostType};
execute(_Ctx, #domain{enabled = Enabled}, <<"enabled">>, _Args) ->
execute(_Ctx, #domain{status = Enabled}, <<"status">>, _Args) ->
{ok, Enabled};
execute(_Ctx, #domain{domain = Name}, <<"domain">>, _Args) ->
{ok, Name}.
4 changes: 4 additions & 0 deletions src/graphql/mongoose_graphql_enum.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

-ignore_xref([input/2, output/2]).

input(<<"DomainStatus">>, Type) ->
{ok, list_to_binary(string:to_lower(binary_to_list(Type)))};
input(<<"PresenceShow">>, Show) ->
{ok, list_to_binary(string:to_lower(binary_to_list(Show)))};
input(<<"PresenceType">>, Type) ->
Expand Down Expand Up @@ -36,6 +38,8 @@ input(<<"TelephoneTags">>, Name) -> {ok, Name};
input(<<"LogLevel">>, Name) -> {ok, list_to_atom(string:to_lower(binary_to_list(Name)))};
input(<<"MetricType">>, Name) -> {ok, Name}.

output(<<"DomainStatus">>, Type) ->
{ok, list_to_binary(string:to_upper(atom_to_list(Type)))};
output(<<"PresenceShow">>, Show) ->
{ok, list_to_binary(string:to_upper(binary_to_list(Show)))};
output(<<"PresenceType">>, Type) ->
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/mongoose_graphql_types.hrl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-record(domain, {
domain :: binary(),
host_type = null :: binary() | null,
enabled = null :: boolean() | null
status = null :: mongoose_domain_api:status() | null
}).

-record(resolver_error, {reason :: atom(),
Expand Down

0 comments on commit e365b77

Please sign in to comment.