class GraphqlRails::Router::ResourceRoutesBuilder

Generates graphql routes based on resource name and options

Constants

AVAILABLE_ROUTES

Attributes

autogenerated_action_names[R]
name[R]
options[R]

Public Class Methods

new(name, only: nil, except: [], **options) click to toggle source
# File lib/graphql_rails/router/resource_routes_builder.rb, line 12
def initialize(name, only: nil, except: [], **options)
  @name = name.to_s

  @options = options
  @autogenerated_action_names = initial_action_names(only, except, AVAILABLE_ROUTES)
end

Public Instance Methods

mutation(*args, **kwargs) click to toggle source
# File lib/graphql_rails/router/resource_routes_builder.rb, line 27
def mutation(*args, **kwargs)
  routes << build_mutation(*args, **kwargs)
end
query(*args, **kwargs) click to toggle source
# File lib/graphql_rails/router/resource_routes_builder.rb, line 23
def query(*args, **kwargs)
  routes << build_query(*args, **kwargs)
end
routes() click to toggle source
# File lib/graphql_rails/router/resource_routes_builder.rb, line 19
def routes
  @routes ||= initial_routes
end

Private Instance Methods

build_mutation(*args, **kwargs) click to toggle source
# File lib/graphql_rails/router/resource_routes_builder.rb, line 57
def build_mutation(*args, **kwargs)
  build_route(MutationRoute, *args, **kwargs)
end
build_query(*args, **kwargs) click to toggle source
# File lib/graphql_rails/router/resource_routes_builder.rb, line 61
def build_query(*args, **kwargs)
  build_route(QueryRoute, *args, **kwargs)
end
build_route(builder, action, prefix: action, suffix: false, on: :member, **custom_options) click to toggle source

rubocop:disable Metrics/ParameterLists

# File lib/graphql_rails/router/resource_routes_builder.rb, line 66
def build_route(builder, action, prefix: action, suffix: false, on: :member, **custom_options)
  if suffix == true
    suffix_name = action
    prefix = ''
  end

  action_options = options.merge(custom_options).merge(on: on)
  controller_method_name = action.to_s.underscore
  action_name = [prefix, resource_name(on), suffix_name].map(&:to_s).reject(&:empty?).join('_')
  builder.new(action_name, to: "#{name}##{controller_method_name}", **action_options)
end
initial_action_names(only, except, available) click to toggle source

rubocop:enable Metrics/ParameterLists

# File lib/graphql_rails/router/resource_routes_builder.rb, line 79
def initial_action_names(only, except, available)
  alowed_routes = Array(only || available) & available
  only_routes = alowed_routes.map(&:to_sym) - Array(except).map(&:to_sym)
  Set.new(only_routes)
end
initial_query_routes() click to toggle source
# File lib/graphql_rails/router/resource_routes_builder.rb, line 43
def initial_query_routes
  routes = Set.new

  if autogenerated_action_names.include?(:show)
    routes << build_route(QueryRoute, 'show', to: "#{name}#show", prefix: '', on: :member)
  end

  if autogenerated_action_names.include?(:index)
    routes << build_route(QueryRoute, 'index', to: "#{name}#index", prefix: '', on: :collection)
  end

  routes
end
initial_routes() click to toggle source
# File lib/graphql_rails/router/resource_routes_builder.rb, line 35
def initial_routes
  routes = initial_query_routes
  routes << build_mutation(:create, on: :member) if autogenerated_action_names.include?(:create)
  routes << build_mutation(:update, on: :member) if autogenerated_action_names.include?(:update)
  routes << build_mutation(:destroy, on: :member) if autogenerated_action_names.include?(:destroy)
  routes
end
resource_name(type) click to toggle source
# File lib/graphql_rails/router/resource_routes_builder.rb, line 85
def resource_name(type)
  type.to_sym == :member ? name.singularize : name
end