Class: Stardust::GraphQL::Field

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Field.



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

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

Instance Method Details

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



31
32
33
34
35
36
37
38
# File 'lib/stardust/graphql/field.rb', line 31

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



13
14
15
# File 'lib/stardust/graphql/field.rb', line 13

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

#authorized?(obj, ctx) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
# File 'lib/stardust/graphql/field.rb', line 17

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



27
28
29
# File 'lib/stardust/graphql/field.rb', line 27

def resolve(&block)
  @resolve = block
end