Class: URI::GQL

Inherits:
Generic
  • Object
show all
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 Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#class_nameObject (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

#nameObject (readonly)

Returns the value of attribute name.



23
24
25
# File 'lib/rails/graphql/uri.rb', line 23

def name
  @name
end

#paramsObject (readonly)

Returns the value of attribute params.



23
24
25
# File 'lib/rails/graphql/uri.rb', line 23

def params
  @params
end

#scopeObject (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

Create a new URI::GQL from components with argument check.

Using a hash:

URI::GQL.build(app: 'bcx', model_name: 'Person', model_id: '1', params: { key: 'value' })

Using an array, the arguments must be in order [app, model_name, model_id, params]:

URI::GQL.build(['bcx', 'Person', '1', key: 'value'])


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

Shorthand to build a URI::GQL from an object and optional scope and params.

URI::GQL.create(GraphQL::Directive::DeprecatedDirective)


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

Returns:

  • (Boolean)


90
91
92
# File 'lib/rails/graphql/uri.rb', line 90

def instantiate?
  !query.nil?
end

#namespaceObject

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_sObject

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