Class: GraphqlGrpc::TypeLibrary
- Inherits:
-
Object
- Object
- GraphqlGrpc::TypeLibrary
- Defined in:
- lib/graphql_grpc/type_library.rb
Direct Known Subclasses
Class Method Summary collapse
-
.descriptor_for(klass_str) ⇒ Object
generated_klass - a class created by the ‘proto’ compiler; maps to a Descriptor in the generated pool.
- .ruby_class_to_dotted(klass_str) ⇒ Object
- .ruby_class_to_underscore(klass_str) ⇒ Object
Instance Method Summary collapse
- #build_descriptors(some_types) ⇒ Object
- #descriptor_for(klass_str) ⇒ Object
-
#initialize(top_level_types) ⇒ TypeLibrary
constructor
A new instance of TypeLibrary.
- #to_schema_types ⇒ Object
- #type_prefix ⇒ Object
- #types ⇒ Object
Constructor Details
#initialize(top_level_types) ⇒ TypeLibrary
Returns a new instance of TypeLibrary.
151 152 153 |
# File 'lib/graphql_grpc/type_library.rb', line 151 def initialize(top_level_types) build_descriptors(top_level_types) end |
Class Method Details
.descriptor_for(klass_str) ⇒ Object
generated_klass - a class created by the ‘proto’ compiler; maps to a Descriptor in the generated pool.
183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/graphql_grpc/type_library.rb', line 183 def self.descriptor_for(klass_str) klass_str = klass_str.to_s # If given a ruby class reference, convert to "java package" string # Pull the Google::Protobuf::Descriptor out of the pool and return it # with the name Google::Protobuf::DescriptorPool.generated_pool.lookup( ruby_class_to_underscore(klass_str) ) || Google::Protobuf::DescriptorPool.generated_pool.lookup( ruby_class_to_dotted(klass_str) ) end |
.ruby_class_to_dotted(klass_str) ⇒ Object
207 208 209 |
# File 'lib/graphql_grpc/type_library.rb', line 207 def self.ruby_class_to_dotted(klass_str) klass_str.gsub('::', '.') end |
.ruby_class_to_underscore(klass_str) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/graphql_grpc/type_library.rb', line 195 def self.ruby_class_to_underscore(klass_str) if klass_str.to_s.include?('::') java_name = klass_str.to_s.split('::') camel_case = java_name.pop java_package = java_name.map(&:underscore) # Put the name back together (java_package + [camel_case]).join('.') else klass_str end end |
Instance Method Details
#build_descriptors(some_types) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/graphql_grpc/type_library.rb', line 155 def build_descriptors(some_types) # Keep track of known types to avoid infinite loops when there # are circular dependencies between gRPC types @descriptors ||= {} some_types.each do |java_class_name| next unless @descriptors[java_class_name].nil? # Store a reference to this type descriptor = descriptor_for(java_class_name) @descriptors[java_class_name] ||= descriptor # Recurse build_descriptors(descriptor.sub_types) if descriptor.respond_to?(:sub_types) # Some types aren't found from #sub_types; iterate #entries of # type :message to get those. if descriptor.respond_to?(:entries) build_descriptors( descriptor.entries .select { |e| e.try(:type) == :message } .map(&:submsg_name) .uniq ) end end end |
#descriptor_for(klass_str) ⇒ Object
211 212 213 |
# File 'lib/graphql_grpc/type_library.rb', line 211 def descriptor_for(klass_str) TypeLibrary.descriptor_for(klass_str) end |
#to_schema_types ⇒ Object
223 224 225 226 227 |
# File 'lib/graphql_grpc/type_library.rb', line 223 def to_schema_types @descriptors.values.compact.map do |t| t.to_gql_type(type_prefix) end.sort.uniq.join("\n") end |
#type_prefix ⇒ Object
219 220 221 |
# File 'lib/graphql_grpc/type_library.rb', line 219 def type_prefix '' end |
#types ⇒ Object
215 216 217 |
# File 'lib/graphql_grpc/type_library.rb', line 215 def types @descriptors end |