From def8dce71554bae3a026ca2d0616c813ff2faa94 Mon Sep 17 00:00:00 2001 From: jacekwegr Date: Mon, 10 Oct 2022 13:16:24 +0200 Subject: [PATCH] Set default value when null is passed --- big_tests/tests/graphql_muc_light_SUITE.erl | 25 +++++++++++++++++++++ big_tests/tests/graphql_stanza_SUITE.erl | 14 ++++++++++++ priv/graphql/schemas/global/muc_light.gql | 2 +- rebar.lock | 2 +- src/graphql/mongoose_graphql.erl | 6 ++++- 5 files changed, 46 insertions(+), 3 deletions(-) diff --git a/big_tests/tests/graphql_muc_light_SUITE.erl b/big_tests/tests/graphql_muc_light_SUITE.erl index 4b7d1930640..6b98374ea04 100644 --- a/big_tests/tests/graphql_muc_light_SUITE.erl +++ b/big_tests/tests/graphql_muc_light_SUITE.erl @@ -116,6 +116,7 @@ admin_muc_light_tests() -> admin_get_room_config, admin_get_room_config_non_existent_domain, admin_blocking_list, + admin_blocking_list_null, admin_blocking_list_errors ]. @@ -881,6 +882,30 @@ admin_blocking_list_story(Config, Alice, Bob) -> Res5 = get_user_blocking(AliceBin, Config), ?assertMatch([], get_ok_value(?GET_BLOCKING_LIST_PATH, Res5)). +admin_blocking_list_null(Config) -> + escalus:fresh_story_with_config(Config, [{alice, 1}, {bob, 1}], + fun admin_blocking_list_null_story/3). + +admin_blocking_list_null_story(Config, Alice, Bob) -> + AliceBin = escalus_client:full_jid(Alice), + BobBin = escalus_client:full_jid(Bob), + BobShortBin = escalus_utils:jid_to_lower(escalus_client:short_jid(Bob)), + Res = get_user_blocking(AliceBin, Config), + ?assertMatch([], get_ok_value(?GET_BLOCKING_LIST_PATH, Res)), + Res2 = set_blocking(AliceBin, [{<<"USER">>, null, BobBin}], Config), + ?assertNotEqual(nomatch, binary:match(get_ok_value(?SET_BLOCKING_LIST_PATH, Res2), + <<"successfully">>)), + Res3 = get_user_blocking(AliceBin, Config), + ?assertEqual([#{<<"entityType">> => <<"USER">>, + <<"action">> => <<"DENY">>, + <<"entity">> => BobShortBin}], + get_ok_value(?GET_BLOCKING_LIST_PATH, Res3)), + Res4 = set_blocking(AliceBin, [{<<"USER">>, <<"ALLOW">>, BobBin}], Config), + ?assertNotEqual(nomatch, binary:match(get_ok_value(?SET_BLOCKING_LIST_PATH, Res4), + <<"successfully">>)), + Res5 = get_user_blocking(AliceBin, Config), + ?assertMatch([], get_ok_value(?GET_BLOCKING_LIST_PATH, Res5)). + admin_blocking_list_errors(Config) -> InvalidUser = make_bare_jid(?UNKNOWN, ?UNKNOWN_DOMAIN), Res = get_user_blocking(InvalidUser, Config), diff --git a/big_tests/tests/graphql_stanza_SUITE.erl b/big_tests/tests/graphql_stanza_SUITE.erl index ede9e54a6be..d0210f405a8 100644 --- a/big_tests/tests/graphql_stanza_SUITE.erl +++ b/big_tests/tests/graphql_stanza_SUITE.erl @@ -40,6 +40,7 @@ admin_get_last_messages_cases() -> admin_get_last_messages_for_unknown_user, admin_get_last_messages_with, admin_get_last_messages_limit, + admin_get_last_messages_limit_null, admin_get_last_messages_limit_enforced, admin_get_last_messages_before]. @@ -399,6 +400,19 @@ admin_get_last_messages_limit_story(Config, Alice, Bob) -> get_ok_value([data, stanza, getLastMessages], Res), check_stanza_map(M1, Bob). +admin_get_last_messages_limit_null(Config) -> + escalus:fresh_story_with_config(Config, [{alice, 1}, {bob, 1}], + fun admin_get_last_messages_limit_null_story/3). + +admin_get_last_messages_limit_null_story(Config, Alice, Bob) -> + admin_send_message_story(Config, Alice, Bob), + mam_helper:wait_for_archive_size(Alice, 1), + Caller = escalus_client:full_jid(Alice), + Res = get_last_messages(Caller, null, null, null, Config), + #{<<"stanzas">> := [M1], <<"limit">> := 50} = + get_ok_value([data, stanza, getLastMessages], Res), + check_stanza_map(M1, Alice). + admin_get_last_messages_limit_enforced(Config) -> escalus:fresh_story_with_config(Config, [{alice, 1}, {bob, 1}], fun admin_get_last_messages_limit_enforced_story/3). diff --git a/priv/graphql/schemas/global/muc_light.gql b/priv/graphql/schemas/global/muc_light.gql index 8c03627f317..1852da4da95 100644 --- a/priv/graphql/schemas/global/muc_light.gql +++ b/priv/graphql/schemas/global/muc_light.gql @@ -13,7 +13,7 @@ input BlockingInput{ "Type of entity to block" entityType: BlockedEntityType! "Type of blocking action" - action: BlockingAction! + action: BlockingAction! = DENY "Entity's JID" entity: JID! } diff --git a/rebar.lock b/rebar.lock index 1a31e04263e..31d78fbfeca 100644 --- a/rebar.lock +++ b/rebar.lock @@ -49,7 +49,7 @@ {<<"goldrush">>,{pkg,<<"goldrush">>,<<"0.1.9">>},1}, {<<"graphql">>, {git,"https://github.com/esl/graphql-erlang.git", - {ref,"72bf290d50b4b5e2f462e06ccfdc5645a6d6401a"}}, + {ref,"7bca478ed484b0859f986f3c74f1013c872265bb"}}, 0}, {<<"gun">>,{pkg,<<"gun">>,<<"1.3.3">>},0}, {<<"hackney">>,{pkg,<<"hackney">>,<<"1.13.0">>},1}, diff --git a/src/graphql/mongoose_graphql.erl b/src/graphql/mongoose_graphql.erl index 8d8f2fe9d8f..e0422c766de 100644 --- a/src/graphql/mongoose_graphql.erl +++ b/src/graphql/mongoose_graphql.erl @@ -74,7 +74,8 @@ execute(Ep, #{document := Doc, {ok, #{ast := Ast2, fun_env := FunEnv}} = graphql:type_check(Ep, Ast), ok = graphql:validate(Ast2), - Coerced = graphql:type_check_params(Ep, FunEnv, OpName, Vars), + Vars2 = remove_null_args(Vars), + Coerced = graphql:type_check_params(Ep, FunEnv, OpName, Vars2), Ctx2 = Ctx#{params => Coerced, operation_name => OpName, authorized => AuthStatus, @@ -125,6 +126,9 @@ graphql_parse(Doc) -> graphql_err:abort([], parse, Err) end. +remove_null_args(Vars) -> + maps:filter(fun(_Key, Value) -> Value /= null end, Vars). + admin_mapping_rules() -> #{objects => #{ 'AdminQuery' => mongoose_graphql_admin_query,