Module: GraphqlGrpc::DescriptorExt

Defined in:
lib/graphql_grpc/type_library.rb

Instance Method Summary collapse

Instance Method Details

#<=>(b) ⇒ Object



25
26
27
# File 'lib/graphql_grpc/type_library.rb', line 25

def <=>(b)
  name <=> b.name
end

#input_or_type(prefix) ⇒ Object

Decide whether this is a GraphQL ‘type’ or ‘input’



62
63
64
65
66
# File 'lib/graphql_grpc/type_library.rb', line 62

def input_or_type(prefix)
  return :input unless prefix.empty?

  :type
end

#sub_types(known_types = []) ⇒ Object

Return an array of all (recursive) types known within this type



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/graphql_grpc/type_library.rb', line 37

def sub_types(known_types=[])
  return known_types if known_types.include?(name)

  # Iterate through the Google::Protobuf::FieldDescriptor list
  entries.map do |fd|
    # fd.name = 'current_entity_to_update'
    # fd.number = 1
    # fd.label = :optional
    # fd.submsg_name = "com.foo.bar.Baz"
    # fd.subtype = #<Google::Protobuf::Descriptor:0x007fabb3947f08>
    if fd.subtype.class == Google::Protobuf::Descriptor
      [name, fd.subtype.sub_types(known_types + [name])].flatten
    else
      [name, fd.submsg_name]
    end
  end.flatten.compact.uniq
end

#to_gql_type(prefix = '') ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/graphql_grpc/type_library.rb', line 68

def to_gql_type(prefix = '')
  if entries.any?
  "  \#{input_or_type(prefix)} \#{prefix}\#{type_name} {\n\#{types(prefix).join(\"\\n  \")}\n  }\n"
  else
    # For now, treat empty types as scalars
    "scalar #{prefix}#{type_name}"
  end
end

#type_nameObject



55
56
57
# File 'lib/graphql_grpc/type_library.rb', line 55

def type_name
  name.split('::').last.split('.').last
end

#types(prefix) ⇒ Object



29
30
31
32
# File 'lib/graphql_grpc/type_library.rb', line 29

def types(prefix)
  # Iterate through the Google::Protobuf::FieldDescriptor list
  entries.sort.map { |fd| fd.to_gql_type(prefix) }
end