Class: Stardust::GraphQL::Field

Inherits:
GraphQL::Schema::Field
  • Object
show all
Defined in:
lib/stardust/graphql/field.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args, **kwargs, &block) ⇒ Field

Returns a new instance of Field.



5
6
7
# File 'lib/stardust/graphql/field.rb', line 5

def initialize(*args, **kwargs, &block)
  super(*args, **kwargs, &block)
end

Instance Method Details

#argument(name, type, description = nil, loads: nil, **kwargs) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/stardust/graphql/field.rb', line 27

def argument(name, type, description = nil, loads: nil, **kwargs)
  actual_type = Collector.lookup_type(type)
  if loads
    kwargs[:prepare] = ->(obj, ctx) { loads.find(obj) }
  end

  super(name, actual_type, description, **kwargs)
end

#authorize(proc, &block) ⇒ Object



9
10
11
# File 'lib/stardust/graphql/field.rb', line 9

def authorize(proc, &block)
  @authorize = block_given? ? block : proc
end

#authorized?(obj, ctx) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
# File 'lib/stardust/graphql/field.rb', line 13

def authorized?(obj, ctx)
  if @authorize.respond_to?(:call)
    unless @authorize.(obj, ctx)
      raise ::GraphQL::ExecutionError, "Not authorized"
    end
  else
    super(obj, ctx)
  end
end

#resolve(&block) ⇒ Object



23
24
25
# File 'lib/stardust/graphql/field.rb', line 23

def resolve(&block)
  @resolve = block
end