Module: SearchObject::Plugin::Graphql::ClassMethods

Defined in:
lib/search_object/plugin/graphql.rb

Constant Summary collapse

KEYS =
%i[type default description required].freeze

Instance Method Summary collapse

Instance Method Details

#accessible?(_context) ⇒ Boolean

NOTE(rstankov): Used for GraphQL::Schema::Resolver

Returns:

  • (Boolean)


127
128
129
# File 'lib/search_object/plugin/graphql.rb', line 127

def accessible?(_context)
  true
end

#argumentsObject

NOTE(rstankov): Used for GraphQL::Function



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/search_object/plugin/graphql.rb', line 80

def arguments
  (config[:arguments] || {}).inject({}) do |acc, (name, options)|
    argument = GraphQL::Argument.new
    argument.name = name.to_s
    argument.type = options.fetch(:type) { raise MissingTypeDefinitionError, name }
    argument.default_value = options[:default] if options.key? :default
    argument.description = options[:description] if options.key? :description

    acc[name] = argument
    acc
  end
end

#authorized?(_object, _context) ⇒ Boolean

NOTE(rstankov): Used for GraphQL::Schema::Resolver

Returns:

  • (Boolean)


132
133
134
# File 'lib/search_object/plugin/graphql.rb', line 132

def authorized?(_object, _context)
  true
end

#call(object, args, context) ⇒ Object

NOTE(rstankov): GraphQL::Function interface (deprecated in favour of GraphQL::Schema::Resolver) Documentation - graphql-ruby.org/guides



70
71
72
# File 'lib/search_object/plugin/graphql.rb', line 70

def call(object, args, context)
  new(filters: args.to_h, object: object, context: context).results
end

#complexity(value = :default) ⇒ Object



50
51
52
53
54
# File 'lib/search_object/plugin/graphql.rb', line 50

def complexity(value = :default)
  return config[:complexity] || 1 if value == :default

  config[:complexity] = value
end

#deprecation_reason(value = :default) ⇒ Object



62
63
64
65
66
# File 'lib/search_object/plugin/graphql.rb', line 62

def deprecation_reason(value = :default)
  return config[:deprecation_reason] if value == :default

  config[:deprecation_reason] = value
end

#description(value = :default) ⇒ Object



56
57
58
59
60
# File 'lib/search_object/plugin/graphql.rb', line 56

def description(value = :default)
  return config[:description] if value == :default

  config[:description] = value
end

#field_optionsObject

NOTE(rstankov): Used for GraphQL::Schema::Resolver



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/search_object/plugin/graphql.rb', line 94

def field_options
  {
    type: type,
    description: description,
    extras: [],
    resolver_method: :resolve_with_support,
    resolver_class: self,
    deprecation_reason: deprecation_reason,
    arguments: (config[:arguments] || {}).inject({}) do |acc, (name, options)|
      name = name.to_s.split('_').map(&:capitalize).join
      name[0] = name[0].downcase

      acc[name] = ::GraphQL::Schema::Argument.new(
        name: name.to_s,
        type: options.fetch(:type) { raise MissingTypeDefinitionError, name },
        description: options[:description],
        required: !!options[:required],
        default_value: options.fetch(:default) { ::GraphQL::Schema::Argument::NO_DEFAULT },
        owner: self
      )
      acc
    end,
    null: !!config[:null],
    complexity: complexity
  }
end

#option(name, options = {}, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/search_object/plugin/graphql.rb', line 30

def option(name, options = {}, &block)
  config[:arguments] ||= {}
  config[:arguments][name.to_s] = KEYS.inject({}) do |acc, key|
    acc[key] = options[key] if options.key?(key)
    acc
  end

  type = options.fetch(:type) { raise MissingTypeDefinitionError, name }
  options[:enum] = type.values.map { |value, enum_value| enum_value.value || value } if type.respond_to?(:values)

  super(name, options, &block)
end

#type(value = :default, null: true, &block) ⇒ Object



43
44
45
46
47
48
# File 'lib/search_object/plugin/graphql.rb', line 43

def type(value = :default, null: true, &block)
  return config[:type] if value == :default && !block_given?

  config[:type] = block_given? ? GraphQL::ObjectType.define(&block) : value
  config[:null] = null
end

#typesObject

NOTE(rstankov): Used for GraphQL::Function



75
76
77
# File 'lib/search_object/plugin/graphql.rb', line 75

def types
  GraphQL::Define::TypeDefiner.instance
end

#visible?(_context) ⇒ Boolean

NOTE(rstankov): Used for GraphQL::Schema::Resolver

Returns:

  • (Boolean)


122
123
124
# File 'lib/search_object/plugin/graphql.rb', line 122

def visible?(_context)
  true
end