Skip to content

Commit

Permalink
Separate admin and user api in schema and resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Premwoik committed Oct 28, 2021
1 parent 63d4555 commit 23a373b
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 54 deletions.
40 changes: 39 additions & 1 deletion priv/graphql/api_schema.gql
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
type Query {
"Contains all admin available queries"
admin: AdminQuery
"Contains all user available queries"
user: UserQuery
}

type Mutation {
"Contains all user available mutations"
admin: AdminMutation
"Contains all user available mutations"
user: UserMutation
}


"""
Contains all admin available queries.
Only authenticated admin can execute this queries.
"""
type AdminQuery{
"Get all enabled domains by hostType"
domainsByHostType(hostType: String!): [String!]
"Get information about the domain"
domainDetails(domain: String!): Domain
}

type Mutation {
"""
Contains all admin available mutations
Only authenticated admin can execute this mutations.
"""
type AdminMutation{
"Add new domain"
addDomain(domain: String!, hostType: String!): Domain
"Remove domain"
Expand All @@ -16,6 +39,21 @@ type Mutation {
disableDomain(domain: String!): Domain
}


"""
Contains all user available queries.
Only authenticated user can execute this queries.
"""
type UserQuery{
}

"""
Contains all user available mutations.
Only authenticated user can execute this mutations.
"""
type UserMutation{
}

"""
A dynamic domain representation.
Some operation could return not complete object i.e. some fields can be null.
Expand Down
4 changes: 4 additions & 0 deletions src/mongoose_graphql.erl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ mapping_rules() ->
#{objects => #{
'Query' => mongoose_graphql_query,
'Mutation' => mongoose_graphql_mutation,
'AdminQuery' => mongoose_graphql_admin_query,
'AdminMutation' => mongoose_graphql_admin_mutation,
'UserQuery' => mongoose_graphql_user_query,
'UserMutation' => mongoose_graphql_user_mutation,
'Domain' => mongoose_graphql_domain,
'default' => mongoose_graphql_default
}
Expand Down
37 changes: 37 additions & 0 deletions src/mongoose_graphql/mongoose_graphql_admin_mutation.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
-module(mongoose_graphql_admin_mutation).

-export([execute/4]).

-ignore_xref([execute/4]).

-include("mongoose_graphql_types.hrl").

execute(_Ctx, admin, <<"addDomain">>, #{<<"domain">> := Domain, <<"hostType">> := HostType}) ->
case mongoose_domain_api:insert_domain(Domain, HostType) of
ok ->
{ok, #domain{domain = Domain, host_type = HostType}};
{error, _} = Err ->
Err
end;
execute(_Ctx, admin, <<"removeDomain">>, #{<<"domain">> := Domain, <<"hostType">> := HostType}) ->
case mongoose_domain_api:delete_domain(Domain, HostType) of
ok ->
DomainObj = #domain{domain = Domain, host_type = HostType},
{ok, #{<<"domain">> => DomainObj, <<"msg">> => <<"Domain removed!">>}};
{error, _} = Err ->
Err
end;
execute(_Ctx, admin, <<"enableDomain">>, #{<<"domain">> := Domain}) ->
case mongoose_domain_api:enable_domain(Domain) of
ok ->
{ok, #domain{enabled = true, domain = Domain}};
{error, _} = Err ->
Err
end;
execute(_Ctx, admin, <<"disableDomain">>, #{<<"domain">> := Domain}) ->
case mongoose_domain_api:disable_domain(Domain) of
ok ->
{ok, #domain{enabled = false, domain = Domain}};
{error, _} = Err ->
Err
end.
20 changes: 20 additions & 0 deletions src/mongoose_graphql/mongoose_graphql_admin_query.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-module(mongoose_graphql_admin_query).

-export([execute/4]).

-ignore_xref([execute/4]).

-include("mongoose_graphql_types.hrl").

execute(_Ctx, admin, <<"domainsByHostType">>, #{<<"hostType">> := HostType}) ->
Domains = mongoose_domain_api:get_domains_by_host_type(HostType),
Domains2 = lists:map(fun(D) -> {ok, D} end, Domains),
{ok, Domains2};
execute(_Ctx, admin, <<"domainDetails">>, #{<<"domain">> := Domain}) ->
case mongoose_domain_sql:select_domain(Domain) of
{ok, #{host_type := HostType, enabled := Enabled}} ->
{ok, #domain{host_type = HostType, domain = Domain,
enabled = Enabled}};
{error, not_found} ->
{error, not_found}
end.
43 changes: 5 additions & 38 deletions src/mongoose_graphql/mongoose_graphql_mutation.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,13 @@

-import(mongoose_graphql_permission, [if_permitted/3]).

-include("mongoose_graphql_types.hrl").

execute(Ctx, _Obj, <<"addDomain">>, #{<<"domain">> := Domain, <<"hostType">> := HostType}) ->
if_permitted(Ctx, admin,
fun() ->
case mongoose_domain_api:insert_domain(Domain, HostType) of
ok ->
{ok, #domain{domain = Domain, host_type = HostType}};
{error, _} = Err ->
Err
end
end);
execute(Ctx, _Obj, <<"removeDomain">>, #{<<"domain">> := Domain, <<"hostType">> := HostType}) ->
execute(Ctx, _Obj, <<"admin">>, #{}) ->
if_permitted(Ctx, admin,
fun() ->
case mongoose_domain_api:delete_domain(Domain, HostType) of
ok ->
DomainObj = #domain{domain = Domain, host_type = HostType},
{ok, #{<<"domain">> => DomainObj, <<"msg">> => <<"Domain removed!">>}};
{error, _} = Err ->
Err
end
{ok, admin}
end);
execute(Ctx, _Obj, <<"enableDomain">>, #{<<"domain">> := Domain}) ->
if_permitted(Ctx, admin,
fun() ->
case mongoose_domain_api:enable_domain(Domain) of
ok ->
{ok, #domain{enabled = true, domain = Domain}};
{error, _} = Err ->
Err
end
end);
execute(Ctx, _Obj, <<"disableDomain">>, #{<<"domain">> := Domain}) ->
if_permitted(Ctx, admin,
execute(Ctx, _Obj, <<"user">>, #{}) ->
if_permitted(Ctx, user,
fun() ->
case mongoose_domain_api:disable_domain(Domain) of
ok ->
{ok, #domain{enabled = false, domain = Domain}};
{error, _} = Err ->
Err
end
{ok, user}
end).
20 changes: 5 additions & 15 deletions src/mongoose_graphql/mongoose_graphql_query.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,13 @@

-import(mongoose_graphql_permission, [if_permitted/3]).

-include("mongoose_graphql_types.hrl").

execute(Ctx, _Obj, <<"domainsByHostType">>, #{<<"hostType">> := HostType}) ->
execute(Ctx, _Obj, <<"admin">>, #{}) ->
if_permitted(Ctx, admin,
fun() ->
Domains = mongoose_domain_api:get_domains_by_host_type(HostType),
Domains2 = lists:map(fun(D) -> {ok, D} end, Domains),
{ok, Domains2}
{ok, admin}
end);
execute(Ctx, _Obj, <<"domainDetails">>, #{<<"domain">> := Domain}) ->
if_permitted(Ctx, admin,
execute(Ctx, _Obj, <<"user">>, #{}) ->
if_permitted(Ctx, user,
fun() ->
case mongoose_domain_sql:select_domain(Domain) of
{ok, #{host_type := HostType, enabled := Enabled}} ->
{ok, #domain{host_type = HostType, domain = Domain,
enabled = Enabled}};
{error, not_found} = Err ->
Err
end
{ok, user}
end).
1 change: 1 addition & 0 deletions src/mongoose_graphql/mongoose_graphql_user_mutation.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-module(mongoose_graphql_user_mutation).
1 change: 1 addition & 0 deletions src/mongoose_graphql/mongoose_graphql_user_query.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-module(mongoose_graphql_user_query).

0 comments on commit 23a373b

Please sign in to comment.