Class: Tina4::GraphQLType

Inherits:
Object
  • Object
show all
Defined in:
lib/tina4/graphql.rb

Overview

─── Type System ──────────────────────────────────────────────────────

Constant Summary collapse

SCALARS =
%w[String Int Float Boolean ID].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, kind = :object, fields: {}, of_type: nil, description: nil) ⇒ GraphQLType

kind: :scalar, :object, :list, :non_null, :input_object, :enum



18
19
20
21
22
23
24
# File 'lib/tina4/graphql.rb', line 18

def initialize(name, kind = :object, fields: {}, of_type: nil, description: nil)
  @name = name
  @kind = kind.to_sym
  @fields = fields        # { field_name => { type:, args:, resolve:, description: } }
  @of_type = of_type      # wrapped type for list / non_null
  @description = description
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



15
16
17
# File 'lib/tina4/graphql.rb', line 15

def description
  @description
end

#fieldsObject (readonly)

Returns the value of attribute fields.



15
16
17
# File 'lib/tina4/graphql.rb', line 15

def fields
  @fields
end

#kindObject (readonly)

Returns the value of attribute kind.



15
16
17
# File 'lib/tina4/graphql.rb', line 15

def kind
  @kind
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/tina4/graphql.rb', line 15

def name
  @name
end

Class Method Details

.parse(type_str) ⇒ Object

Parse a type string like “String”, “String!”, “[String]”, “[Int!]!”



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/tina4/graphql.rb', line 43

def self.parse(type_str)
  type_str = type_str.to_s.strip
  if type_str.end_with?("!")
    inner = parse(type_str[0..-2])
    new(type_str, :non_null, of_type: inner)
  elsif type_str.start_with?("[") && type_str.end_with?("]")
    inner = parse(type_str[1..-2])
    new(type_str, :list, of_type: inner)
  elsif SCALARS.include?(type_str)
    new(type_str, :scalar)
  else
    new(type_str, :object)
  end
end

Instance Method Details

#list?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/tina4/graphql.rb', line 30

def list?
  @kind == :list
end

#non_null?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/tina4/graphql.rb', line 34

def non_null?
  @kind == :non_null
end

#of_typeObject



38
39
40
# File 'lib/tina4/graphql.rb', line 38

def of_type
  @of_type
end

#scalar?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/tina4/graphql.rb', line 26

def scalar?
  @kind == :scalar || SCALARS.include?(@name)
end