debian-mirror-gitlab/lib/gitlab/graphql/present/field_extension.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

39 lines
1.3 KiB
Ruby
Raw Permalink Normal View History

2021-04-17 20:07:23 +05:30
# frozen_string_literal: true
module Gitlab
module Graphql
module Present
class FieldExtension < ::GraphQL::Schema::FieldExtension
SAFE_CONTEXT_KEYS = %i[current_user].freeze
def resolve(object:, arguments:, context:)
attrs = safe_context_values(context)
# We need to handle the object being either a Schema::Object or an
# inner Schema::Object#object. This depends on whether the field
# has a @resolver_proc or not.
if object.is_a?(::Types::BaseObject)
2021-06-08 01:23:25 +05:30
type = field.owner.kind.abstract? ? object.class : field.owner
object.present(type, attrs)
2021-04-17 20:07:23 +05:30
yield(object, arguments)
else
# This is the legacy code-path, hit if the field has a @resolver_proc
# TODO: remove this when resolve procs are removed from the
# graphql-ruby library, and all field instrumentation is removed.
# See: https://github.com/rmosolgo/graphql-ruby/issues/3385
2022-07-23 23:45:48 +05:30
# See: https://gitlab.com/gitlab-org/gitlab/-/issues/363131
2021-04-17 20:07:23 +05:30
presented = field.owner.try(:present, object, attrs) || object
yield(presented, arguments)
end
end
private
def safe_context_values(context)
context.to_h.slice(*SAFE_CONTEXT_KEYS)
end
end
end
end
end