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

GraphQL - Check loaded modules #3719

Merged
merged 10 commits into from
Aug 10, 2022
Merged

GraphQL - Check loaded modules #3719

merged 10 commits into from
Aug 10, 2022

Conversation

Premwoik
Copy link
Contributor

@Premwoik Premwoik commented Jul 27, 2022

This PR addresses MIM-1671 and adds support for custom directives. It introduces the mongoose_graphql_directive module that processes the schema directives. It traverses the AST, and delegates processing directives to the suitable handlers that implement the behavior. The directives have the ability to modify the annotated note. Currently, we have two handlers:

  • mongoose_graphql_directive_protected for @protected directive,
  • mongoose_graphql_directives_use for @use directive.

The mongoose_graphql_directive behavior consists of two callbacks:

  • handle_directive - handles field directives,
  • handle_object_directives- handles object directives.

Use directive

The loaded modules are checked per command so all commands have to be annotated with the directive. This is because we need to obtain the domain from the argument (an admin case). A user endpoint can obtain a domain from an authorized user.

However, categories also can be annotated and args will be aggregated and checked per command. Take a look at an example taken from the user schema:

type InboxUserMutation @use(modules: ["mod_inbox"]) @protected{
  flushBin(days: PosInt): Int @use
}

I annotated a mod_last and mod_inbox with the use directive for an example. We still need to add it to other categories.

In addition, as we have already checked if the domain or host type exists, we do not need to do it again in the resolvers. But currently, when the domain is nonexistent, we skip checking loaded modules and use delegate handling this case to the original field resolver.

Removed

Previously only the @protected was supported. We use it for checking permissions, it was handled in the mongoose_graphql_permissions module but now the logic is moved to the mongoose_graphql_directive_protected module. The mongoose_graphql_permissions has been removed.

I also removed the condition that allowed running introspection on the protected root query type because we haven't had this type protected anymore.

TODO

  • Evaluate the suitability of the solution.
  • Use the use directive in other categories.
  • Maybe provide additional tests?

@mongoose-im

This comment was marked as outdated.

@codecov
Copy link

codecov bot commented Jul 27, 2022

Codecov Report

Merging #3719 (4f80f7f) into master (3b4b32e) will increase coverage by 0.01%.
The diff coverage is 97.31%.

@@            Coverage Diff             @@
##           master    #3719      +/-   ##
==========================================
+ Coverage   82.24%   82.25%   +0.01%     
==========================================
  Files         523      526       +3     
  Lines       33801    33853      +52     
==========================================
+ Hits        27799    27847      +48     
- Misses       6002     6006       +4     
Impacted Files Coverage Δ
...ql/directive/mongoose_graphql_directive_helper.erl 80.00% <80.00%> (ø)
...c/graphql/directive/mongoose_graphql_directive.erl 97.40% <97.40%> (ø)
...directive/mongoose_graphql_directive_protected.erl 100.00% <100.00%> (ø)
...aphql/directive/mongoose_graphql_directive_use.erl 100.00% <100.00%> (ø)
src/graphql/mongoose_graphql.erl 91.11% <100.00%> (ø)
src/elasticsearch/mongoose_elasticsearch.erl 76.92% <0.00%> (-7.70%) ⬇️
src/mam/mod_mam_muc_rdbms_arch_async.erl 82.85% <0.00%> (-2.86%) ⬇️
src/mam/mod_mam_elasticsearch_arch.erl 86.60% <0.00%> (-1.79%) ⬇️
src/mod_roster_riak.erl 96.92% <0.00%> (-1.54%) ⬇️
src/global_distrib/mod_global_distrib_receiver.erl 79.51% <0.00%> (-1.21%) ⬇️
... and 10 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@mongoose-im

This comment was marked as outdated.

@mongoose-im

This comment was marked as outdated.

@Premwoik Premwoik force-pushed the graphql-check-loaded-modules branch from c8c4fee to 570d40b Compare July 28, 2022 06:04
@mongoose-im

This comment was marked as outdated.

@Premwoik Premwoik force-pushed the graphql-check-loaded-modules branch from 570d40b to 240bfab Compare July 28, 2022 15:54
@mongoose-im

This comment was marked as outdated.

@Premwoik Premwoik force-pushed the graphql-check-loaded-modules branch from 240bfab to 2263e4f Compare July 29, 2022 20:29
@mongoose-im

This comment was marked as outdated.

@Premwoik Premwoik force-pushed the graphql-check-loaded-modules branch from 2263e4f to f38d746 Compare July 29, 2022 20:59
@mongoose-im
Copy link
Collaborator

mongoose-im commented Jul 29, 2022

small_tests_25 / small_tests / f38d746
Reports root / small


small_tests_24 / small_tests / f38d746
Reports root / small


dynamic_domains_pgsql_mnesia_24 / pgsql_mnesia / f38d746
Reports root/ big
OK: 3122 / Failed: 0 / User-skipped: 88 / Auto-skipped: 0


ldap_mnesia_24 / ldap_mnesia / f38d746
Reports root/ big
OK: 1739 / Failed: 0 / User-skipped: 460 / Auto-skipped: 0


dynamic_domains_mysql_redis_25 / mysql_redis / f38d746
Reports root/ big
OK: 3105 / Failed: 0 / User-skipped: 105 / Auto-skipped: 0


pgsql_mnesia_24 / pgsql_mnesia / f38d746
Reports root/ big
OK: 3496 / Failed: 0 / User-skipped: 97 / Auto-skipped: 0


dynamic_domains_mssql_mnesia_25 / odbc_mssql_mnesia / f38d746
Reports root/ big
OK: 3122 / Failed: 0 / User-skipped: 88 / Auto-skipped: 0


ldap_mnesia_25 / ldap_mnesia / f38d746
Reports root/ big
OK: 1739 / Failed: 0 / User-skipped: 460 / Auto-skipped: 0


internal_mnesia_25 / internal_mnesia / f38d746
Reports root/ big
OK: 1845 / Failed: 0 / User-skipped: 354 / Auto-skipped: 0


pgsql_mnesia_25 / pgsql_mnesia / f38d746
Reports root/ big
OK: 3496 / Failed: 0 / User-skipped: 97 / Auto-skipped: 0


riak_mnesia_24 / riak_mnesia / f38d746
Reports root/ big
OK: 1994 / Failed: 0 / User-skipped: 350 / Auto-skipped: 0


mysql_redis_25 / mysql_redis / f38d746
Reports root/ big
OK: 3491 / Failed: 0 / User-skipped: 102 / Auto-skipped: 0


dynamic_domains_pgsql_mnesia_25 / pgsql_mnesia / f38d746
Reports root/ big
OK: 3122 / Failed: 0 / User-skipped: 88 / Auto-skipped: 0


mssql_mnesia_25 / odbc_mssql_mnesia / f38d746
Reports root/ big
OK: 3496 / Failed: 0 / User-skipped: 97 / Auto-skipped: 0


elasticsearch_and_cassandra_25 / elasticsearch_and_cassandra_mnesia / f38d746
Reports root/ big
OK: 2134 / Failed: 1 / User-skipped: 355 / Auto-skipped: 0

graphql_roster_SUITE:user_roster:user_invite_accept_and_cancel_subscription
{error,
  {timeout_when_waiting_for_stanza,
    [{escalus_client,wait_for_stanza,
       [{client,
          <<"bob_user_invite_accept_and_cancel_subscription_912@localhost/res1">>,
          escalus_tcp,<0.18909.0>,
          [{event_manager,<0.18907.0>},
           {server,<<"localhost">>},
           {username,
             <<"bOb_user_invite_accept_and_cancel_subscription_912">>},
           {resource,<<"res1">>}],
          [{event_client,
             [{event_manager,<0.18907.0>},
            {server,<<"localhost">>},
            {username,
              <<"bOb_user_invite_accept_and_cancel_subscription_912">>},
            {resource,<<"res1">>}]},
           {resource,<<"res1">>},
           {username,
             <<"bob_user_invite_accept_and_cancel_subscription_912">>},
           {server,<<"localhost">>},
           {host,<<"localhost">>},
           {port,5222},
           {auth,{escalus_auth,auth_plain}},
           {wspath,undefined},
           {username,
             <<"bOb_user_invite_accept_and_cancel_subscription_912">>},
           {server,<<"localhost">>},
           {password,<<"makrolika">>},
           {stream_id,<<"cd6522193143271e">>}]},
        1],
       [{file,
          "/home/circleci/project/big_tests/_build/default/lib/escalus/src/escalus_client.erl"},
        {line,136}]},
     {graphql_roster_SUITE,
       user_invite_accept_and_cancel_subscription_story,3,
       [{file,
          "/home/circleci/project/big_tests/tests/graphql_roster_SUITE...

Report log


elasticsearch_and_cassandra_25 / elasticsearch_and_cassandra_mnesia / f38d746
Reports root/ big
OK: 2135 / Failed: 0 / User-skipped: 355 / Auto-skipped: 0

@Premwoik Premwoik marked this pull request as ready for review July 29, 2022 21:45
Copy link
Contributor

@JanuszJakubiec JanuszJakubiec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, I have added minor comments.

Copy link
Member

@chrzaszcz chrzaszcz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good in general, I added a few comments.

priv/graphql/schemas/admin/inbox.gql Outdated Show resolved Hide resolved
priv/graphql/schemas/admin/last.gql Show resolved Hide resolved
priv/graphql/schemas/user/inbox.gql Show resolved Hide resolved
src/graphql/directive/mongoose_graphql_directive.erl Outdated Show resolved Hide resolved
test/mongoose_graphql_SUITE_data/directives_schema.gql Outdated Show resolved Hide resolved
src/graphql/directive/mongoose_graphql_directive_use.erl Outdated Show resolved Hide resolved
src/graphql/directive/mongoose_graphql_directive_use.erl Outdated Show resolved Hide resolved
Field#schema_field{resolve = Fun}
end;
{error, not_found} ->
Field
Copy link
Member

@chrzaszcz chrzaszcz Aug 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to return something like "Invalid domain or host type"? For me it looks counter-intuitive that we skip the check as if everything was fine.

Copy link
Contributor Author

@Premwoik Premwoik Aug 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about it and decided not to return an error here because we check domain/host type again in the API modules. The error returned from API modules is more precise because we know there if the domain or host type is nonexistent.

If we return an error here, then we stop testing the error returned from API modules because we override it. If this is not a problem, then I can return an error here. Tests will require small adjustments because the error message will change.

Copy link
Contributor Author

@Premwoik Premwoik Aug 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to create a follow-up ticket to add the use directive to other categories. I think we can add returning an error there if we decide to do it. What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is ok for now

src/graphql/directive/mongoose_graphql_directive_use.erl Outdated Show resolved Hide resolved
@mongoose-im

This comment was marked as outdated.

Turns out that introspection fields' schema is not a schema_field() type.
@mongoose-im
Copy link
Collaborator

mongoose-im commented Aug 8, 2022

small_tests_24 / small_tests / 4f80f7f
Reports root / small


small_tests_25 / small_tests / 4f80f7f
Reports root / small


dynamic_domains_pgsql_mnesia_24 / pgsql_mnesia / 4f80f7f
Reports root/ big
OK: 3340 / Failed: 0 / User-skipped: 88 / Auto-skipped: 0


ldap_mnesia_24 / ldap_mnesia / 4f80f7f
Reports root/ big
OK: 1897 / Failed: 0 / User-skipped: 513 / Auto-skipped: 0


dynamic_domains_pgsql_mnesia_25 / pgsql_mnesia / 4f80f7f
Reports root/ big
OK: 3340 / Failed: 0 / User-skipped: 88 / Auto-skipped: 0


dynamic_domains_mysql_redis_25 / mysql_redis / 4f80f7f
Reports root/ big
OK: 3323 / Failed: 0 / User-skipped: 105 / Auto-skipped: 0


ldap_mnesia_25 / ldap_mnesia / 4f80f7f
Reports root/ big
OK: 1897 / Failed: 0 / User-skipped: 513 / Auto-skipped: 0


internal_mnesia_25 / internal_mnesia / 4f80f7f
Reports root/ big
OK: 2018 / Failed: 0 / User-skipped: 392 / Auto-skipped: 0


pgsql_mnesia_24 / pgsql_mnesia / 4f80f7f
Reports root/ big
OK: 3714 / Failed: 0 / User-skipped: 97 / Auto-skipped: 0


elasticsearch_and_cassandra_25 / elasticsearch_and_cassandra_mnesia / 4f80f7f
Reports root/ big
OK: 2314 / Failed: 0 / User-skipped: 387 / Auto-skipped: 0


pgsql_mnesia_25 / pgsql_mnesia / 4f80f7f
Reports root/ big
OK: 3714 / Failed: 0 / User-skipped: 97 / Auto-skipped: 0


mysql_redis_25 / mysql_redis / 4f80f7f
Reports root/ big
OK: 3708 / Failed: 1 / User-skipped: 102 / Auto-skipped: 0

bosh_SUITE:essential:accept_higher_hold_value
{error,
  {{assertEqual,
     [{module,bosh_SUITE},
      {line,251},
      {expression,"get_bosh_sessions ( )"},
      {expected,[]},
      {value,
        [{bosh_session,<<"080aee48127221702109b868f21d1eea8edc25f8">>,
           <9154.6124.0>}]}]},
   [{bosh_SUITE,accept_higher_hold_value,1,
      [{file,"/home/circleci/project/big_tests/tests/bosh_SUITE.erl"},
       {line,251}]},
    {test_server,ts_tc,3,[{file,"test_server.erl"},{line,1782}]},
    {test_server,run_test_case_eval1,6,
      [{file,"test_server.erl"},{line,1291}]},
    {test_server,run_test_case_eval,9,
      [{file,"test_server.erl"},{line,1223}]}]}}

Report log


mssql_mnesia_25 / odbc_mssql_mnesia / 4f80f7f
Reports root/ big
OK: 3714 / Failed: 0 / User-skipped: 97 / Auto-skipped: 0


riak_mnesia_24 / riak_mnesia / 4f80f7f
Reports root/ big
OK: 2175 / Failed: 0 / User-skipped: 380 / Auto-skipped: 0


mysql_redis_25 / mysql_redis / 4f80f7f
Reports root/ big
OK: 3709 / Failed: 0 / User-skipped: 102 / Auto-skipped: 0

Copy link
Member

@chrzaszcz chrzaszcz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

@chrzaszcz chrzaszcz merged commit 5a18d5a into master Aug 10, 2022
@chrzaszcz chrzaszcz deleted the graphql-check-loaded-modules branch August 10, 2022 06:41
@chrzaszcz chrzaszcz added this to the 6.0.0 milestone Dec 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants