Class: URI::GQL
- Inherits:
-
Generic
- Object
- Generic
- URI::GQL
- Defined in:
- lib/rails/graphql/uri.rb
Overview
URI::GLQ encodes objects from a GraphQL server as an URI. It has the components: All components except version and params are required.
The URI format looks like “glq://namespace/class_name/gql_name”. There are some cases where the name will be further scoped like fields in a query.
Params use the power of Rack::Utils.parse_nested_query to make sure that nested elements can be correctly parsed. In the presence of params, even with just a simple ? with no actual params, it indicates an instance of the object.
TODO: Implement version
Constant Summary collapse
- COMPONENT =
%i[scheme namespace class_name scope name params].freeze
Instance Attribute Summary collapse
-
#class_name ⇒ Object
readonly
Returns the value of attribute class_name.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
Class Method Summary collapse
-
.build(args) ⇒ Object
Create a new URI::GQL from components with argument check.
-
.create(object, scope = nil, params = nil) ⇒ Object
Shorthand to build a URI::GQL from an object and optional scope and params.
-
.parse(uri) ⇒ Object
Create a new URI::GQL by parsing a gid string with argument check.
Instance Method Summary collapse
-
#instantiate? ⇒ Boolean
Check if the object should be instantiated.
-
#namespace ⇒ Object
Make sure to convert dashes into underscore.
-
#to_s ⇒ Object
Implement #to_s to avoid no implicit conversion of nil into string when path is nil.
Instance Attribute Details
#class_name ⇒ Object (readonly)
Returns the value of attribute class_name.
23 24 25 |
# File 'lib/rails/graphql/uri.rb', line 23 def class_name @class_name end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
23 24 25 |
# File 'lib/rails/graphql/uri.rb', line 23 def name @name end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
23 24 25 |
# File 'lib/rails/graphql/uri.rb', line 23 def params @params end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
23 24 25 |
# File 'lib/rails/graphql/uri.rb', line 23 def scope @scope end |
Class Method Details
.build(args) ⇒ Object
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/rails/graphql/uri.rb', line 72 def build(args) parts = Util.make_components_hash(self, args) parts[:host] = parts[:namespace].to_s.tr('_', '-') parts[:path] = [parts[:class_name], parts[:scope], parts[:name]].compact.join('/') parts[:path].prepend('/') parts[:query] = parts[:params].to_param unless parts[:params].nil? super parts end |
.create(object, scope = nil, params = nil) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rails/graphql/uri.rb', line 44 def create(object, scope = nil, params = nil) namespace = Rails::GraphQL.enumerate(object.namespaces).first || :base klass = object.gid_base_class klass = klass.class unless klass.is_a?(Module) xargs = { namespace: namespace, scope: scope, params: params } xargs[:name] = object.gql_name unless klass == Rails::GraphQL::Schema xargs[:class_name] = case when klass <= Rails::GraphQL::Schema then 'Schema' when klass <= Rails::GraphQL::Source then 'Schema' when klass <= Rails::GraphQL::Directive then 'Directive' when klass.superclass == Rails::GraphQL::Type then 'Type' else klass.gql_name end build(xargs) end |
.parse(uri) ⇒ Object
Create a new URI::GQL by parsing a gid string with argument check.
URI::GQL.parse 'glq://base/Directive/deprecated'
This differs from URI() and URI.parse which do not check arguments.
URI('gql://bcx') # => URI::GQL instance
URI.parse('gql://bcx') # => URI::GQL instance
URI::GQL.parse('gql://bcx/') # => raises URI::InvalidComponentError
35 36 37 38 |
# File 'lib/rails/graphql/uri.rb', line 35 def parse(uri) generic_components = URI.split(uri) << nil << true # nil parser, true arg_check new(*generic_components) end |
Instance Method Details
#instantiate? ⇒ Boolean
Check if the object should be instantiated
90 91 92 |
# File 'lib/rails/graphql/uri.rb', line 90 def instantiate? !query.nil? end |
#namespace ⇒ Object
Make sure to convert dashes into underscore
85 86 87 |
# File 'lib/rails/graphql/uri.rb', line 85 def namespace host.tr('-', '_').to_sym end |
#to_s ⇒ Object
Implement #to_s to avoid no implicit conversion of nil into string when path is nil
95 96 97 |
# File 'lib/rails/graphql/uri.rb', line 95 def to_s +"gql://#{host}#{path}#{'?' + query if query}" end |