Class: GraphQL::ObjectType

Inherits:
Object
  • Object
show all
Extended by:
Definable
Includes:
NonNullWithBang
Defined in:
lib/graph_ql/types/object_type.rb

Instance Method Summary collapse

Methods included from Definable

attr_definable

Methods included from NonNullWithBang

#!

Constructor Details

#initialize(&block) ⇒ ObjectType

Returns a new instance of ObjectType.



6
7
8
9
10
# File 'lib/graph_ql/types/object_type.rb', line 6

def initialize(&block)
  self.fields = []
  self.interfaces = []
  instance_eval(&block)
end

Instance Method Details

#arg(type:, desc: "", default_value: nil) ⇒ Object



38
39
40
# File 'lib/graph_ql/types/object_type.rb', line 38

def arg(type:, desc: "", default_value: nil)
  GraphQL::InputValue.new(type: type, description: desc, default_value: default_value)
end

#field(type:, args: {}, property: nil, desc: "", deprecation_reason: nil) ⇒ Object



34
35
36
# File 'lib/graph_ql/types/object_type.rb', line 34

def field(type:, args: {}, property: nil, desc: "", deprecation_reason: nil)
  GraphQL::AccessField.new(type: type, arguments: args, property: property, description: desc, deprecation_reason: deprecation_reason)
end

#fields(new_fields = nil) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/graph_ql/types/object_type.rb', line 12

def fields(new_fields=nil)
  if new_fields
    self.fields = new_fields
  else
    @fields
  end
end

#fields=(new_fields) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/graph_ql/types/object_type.rb', line 20

def fields=(new_fields)
  stringified_fields = new_fields
    .reduce({}) { |memo, (key, value)| memo[key.to_s] = value; memo }
  # Set the name from its context on this type:
  stringified_fields.each {|k, v| v.respond_to?("name=") && v.name = k }
  stringified_fields["__typename"] = GraphQL::Field.new do |f|
    f.name "__typename"
    f.description "The name of this type"
    f.type -> { !GraphQL::STRING_TYPE }
    f.resolve -> (o, a, c) { self.name }
  end
  @fields = stringified_fields
end

#interfaces(new_interfaces = nil) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/graph_ql/types/object_type.rb', line 46

def interfaces(new_interfaces=nil)
  if new_interfaces.nil?
    @interfaces
  else
    @interfaces = new_interfaces
    new_interfaces.each {|i| i.possible_types << self }
  end
end

#kindObject



55
56
57
# File 'lib/graph_ql/types/object_type.rb', line 55

def kind
  GraphQL::TypeKinds::OBJECT
end

#to_sObject



59
60
61
# File 'lib/graph_ql/types/object_type.rb', line 59

def to_s
  name
end

#typeObject



42
43
44
# File 'lib/graph_ql/types/object_type.rb', line 42

def type
  @type ||= GraphQL::TypeDefiner.new
end