diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-01-08 21:08:08 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-01-08 21:08:08 +0000 |
commit | e0b84f4ba4b44c8ecf00be97843c40df2550b74c (patch) | |
tree | 7cecae4276358dd46d7fa15f84068f7b4c626089 /app/graphql/gitlab_schema.rb | |
parent | 73391dcc368ef846c2960c1d0ef5e64ca78e1bee (diff) | |
download | gitlab-ce-e0b84f4ba4b44c8ecf00be97843c40df2550b74c.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/graphql/gitlab_schema.rb')
-rw-r--r-- | app/graphql/gitlab_schema.rb | 56 |
1 files changed, 46 insertions, 10 deletions
diff --git a/app/graphql/gitlab_schema.rb b/app/graphql/gitlab_schema.rb index ccbb1d56030..ea5776534d5 100644 --- a/app/graphql/gitlab_schema.rb +++ b/app/graphql/gitlab_schema.rb @@ -57,13 +57,55 @@ class GitlabSchema < GraphQL::Schema object.to_global_id end + # Find an object by looking it up from its global ID, passed as a string. + # + # This is the composition of 'parse_gid' and 'find_by_gid', see these + # methods for further documentation. def object_from_id(global_id, ctx = {}) + gid = parse_gid(global_id, ctx) + + find_by_gid(gid) + end + + # Find an object by looking it up from its 'GlobalID'. + # + # * For `ApplicationRecord`s, this is equivalent to + # `global_id.model_class.find(gid.model_id)`, but more efficient. + # * For classes that implement `.lazy_find(global_id)`, this class method + # will be called. + # * All other classes will use `GlobalID#find` + def find_by_gid(gid) + if gid.model_class < ApplicationRecord + Gitlab::Graphql::Loaders::BatchModelLoader.new(gid.model_class, gid.model_id).find + elsif gid.model_class.respond_to?(:lazy_find) + gid.model_class.lazy_find(gid.model_id) + else + gid.find + end + end + + # Parse a string to a GlobalID, raising ArgumentError if there are problems + # with it. + # + # Problems that may occur: + # * it may not be syntactically valid + # * it may not match the expected type (see below) + # + # Options: + # * :expected_type [Class] - the type of object this GlobalID should refer to. + # + # e.g. + # + # ``` + # gid = GitlabSchema.parse_gid(my_string, expected_type: ::Project) + # project_id = gid.model_id + # gid.model_class == ::Project + # ``` + def parse_gid(global_id, ctx = {}) expected_type = ctx[:expected_type] gid = GlobalID.parse(global_id) - unless gid - raise Gitlab::Graphql::Errors::ArgumentError, "#{global_id} is not a valid GitLab id." - end + raise Gitlab::Graphql::Errors::ArgumentError, "#{global_id} is not a valid GitLab id." unless gid if expected_type && !gid.model_class.ancestors.include?(expected_type) vars = { global_id: global_id, expected_type: expected_type } @@ -71,13 +113,7 @@ class GitlabSchema < GraphQL::Schema raise Gitlab::Graphql::Errors::ArgumentError, msg end - if gid.model_class < ApplicationRecord - Gitlab::Graphql::Loaders::BatchModelLoader.new(gid.model_class, gid.model_id).find - elsif gid.model_class.respond_to?(:lazy_find) - gid.model_class.lazy_find(gid.model_id) - else - gid.find - end + gid end private |