lookup_resolver.rb 649 B

123456789101112131415161718192021222324252627282930
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Gql::Resolvers::LookupResolver < GraphQL::Schema::Resolver
  3. include Gql::Types::Concerns::HasPunditAuthorization
  4. description "resolver for Rails' has_one relationship"
  5. def resolve
  6. raw_value = object.send(method_name)
  7. reflection_target
  8. &.lookup(id: raw_value)
  9. &.name
  10. end
  11. private
  12. def method_name
  13. @method_name ||= field.foreign_key || field.original_name
  14. end
  15. def reflection_target
  16. object
  17. .class
  18. .reflections
  19. .find { |_, elem| elem.foreign_key == method_name.to_s }
  20. &.last
  21. &.klass
  22. end
  23. end