diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-03-16 18:18:33 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-03-16 18:18:33 +0000 |
commit | f64a639bcfa1fc2bc89ca7db268f594306edfd7c (patch) | |
tree | a2c3c2ebcc3b45e596949db485d6ed18ffaacfa1 /app/graphql/types/global_id_type.rb | |
parent | bfbc3e0d6583ea1a91f627528bedc3d65ba4b10f (diff) | |
download | gitlab-ce-f64a639bcfa1fc2bc89ca7db268f594306edfd7c.tar.gz |
Add latest changes from gitlab-org/gitlab@13-10-stable-eev13.10.0-rc40
Diffstat (limited to 'app/graphql/types/global_id_type.rb')
-rw-r--r-- | app/graphql/types/global_id_type.rb | 42 |
1 files changed, 27 insertions, 15 deletions
diff --git a/app/graphql/types/global_id_type.rb b/app/graphql/types/global_id_type.rb index ed28c3ffd7e..750bd1bfe8d 100644 --- a/app/graphql/types/global_id_type.rb +++ b/app/graphql/types/global_id_type.rb @@ -19,7 +19,14 @@ end module Types class GlobalIDType < BaseScalar graphql_name 'GlobalID' - description 'A global identifier' + description <<~DESC + A global identifier. + + A global identifier represents an object uniquely across the application. + An example of such an identifier is "gid://gitlab/User/1". + + Global identifiers are encoded as strings. + DESC # @param value [GID] # @return [String] @@ -46,35 +53,40 @@ module Types @id_types[model_class] ||= Class.new(self) do graphql_name "#{model_class.name.gsub(/::/, '')}ID" - description "Identifier of #{model_class.name}." + description <<~MD + A `#{graphql_name}` is a global ID. It is encoded as a string. + + An example `#{graphql_name}` is: `"#{::Gitlab::GlobalId.build(model_name: model_class.name, id: 1)}"`. + MD - self.define_singleton_method(:to_s) do + define_singleton_method(:to_s) do graphql_name end - self.define_singleton_method(:inspect) do + define_singleton_method(:inspect) do graphql_name end - self.define_singleton_method(:coerce_result) do |gid, ctx| + define_singleton_method(:coerce_result) do |gid, ctx| global_id = ::Gitlab::GlobalId.as_global_id(gid, model_name: model_class.name) - if suitable?(global_id) - global_id.to_s - else - raise GraphQL::CoercionError, "Expected a #{model_class.name} ID, got #{global_id}" - end + next global_id.to_s if suitable?(global_id) + + raise GraphQL::CoercionError, "Expected a #{model_class.name} ID, got #{global_id}" end - self.define_singleton_method(:suitable?) do |gid| - gid&.model_class&.ancestors&.include?(model_class) + define_singleton_method(:suitable?) do |gid| + next false if gid.nil? + + gid.model_name.safe_constantize.present? && + gid.model_class.ancestors.include?(model_class) end - self.define_singleton_method(:coerce_input) do |string, ctx| + define_singleton_method(:coerce_input) do |string, ctx| gid = super(string, ctx) - raise GraphQL::CoercionError, "#{string.inspect} does not represent an instance of #{model_class.name}" unless suitable?(gid) + next gid if suitable?(gid) - gid + raise GraphQL::CoercionError, "#{string.inspect} does not represent an instance of #{model_class.name}" end end end |