Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set default value when null is passed to GraphQL API #3803

Merged
merged 1 commit into from
Oct 18, 2022
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
25 changes: 25 additions & 0 deletions big_tests/tests/graphql_muc_light_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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
].

Expand Down Expand Up @@ -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),
Expand Down
14 changes: 14 additions & 0 deletions big_tests/tests/graphql_stanza_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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].

Expand Down Expand Up @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion priv/graphql/schemas/global/muc_light.gql
Original file line number Diff line number Diff line change
Expand Up @@ -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!
}
Expand Down
2 changes: 1 addition & 1 deletion rebar.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
6 changes: 5 additions & 1 deletion src/graphql/mongoose_graphql.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down