Class: GraphqlRails::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql_rails/router.rb,
lib/graphql_rails/router/route.rb,
lib/graphql_rails/router/event_route.rb,
lib/graphql_rails/router/query_route.rb,
lib/graphql_rails/router/mutation_route.rb,
lib/graphql_rails/router/schema_builder.rb,
lib/graphql_rails/router/plain_cursor_encoder.rb,
lib/graphql_rails/router/resource_routes_builder.rb,
lib/graphql_rails/router/build_schema_action_type.rb

Overview

graphql router that mimics Rails.application.routes

Defined Under Namespace

Modules: PlainCursorEncoder Classes: BuildSchemaActionType, EventRoute, MutationRoute, QueryRoute, ResourceRoutesBuilder, Route, SchemaBuilder

Constant Summary collapse

RAW_ACTION_NAMES =
%i[
  use rescue_from query_analyzer instrument cursor_encoder default_max_page_size
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(module_name: '', group_names: [], scope_names: []) ⇒ Router

Returns a new instance of Router.



26
27
28
29
30
31
32
33
# File 'lib/graphql_rails/router.rb', line 26

def initialize(module_name: '', group_names: [], scope_names: [])
  @scope_names = scope_names
  @module_name = module_name
  @group_names = group_names
  @routes ||= Set.new
  @raw_graphql_actions ||= []
  @graphql_schema = {}
end

Instance Attribute Details

#namespace_nameObject (readonly)

Returns the value of attribute namespace_name.



24
25
26
# File 'lib/graphql_rails/router.rb', line 24

def namespace_name
  @namespace_name
end

#raw_graphql_actionsObject (readonly)

Returns the value of attribute raw_graphql_actions.



24
25
26
# File 'lib/graphql_rails/router.rb', line 24

def raw_graphql_actions
  @raw_graphql_actions
end

#routesObject (readonly)

Returns the value of attribute routes.



24
25
26
# File 'lib/graphql_rails/router.rb', line 24

def routes
  @routes
end

#scope_namesObject (readonly)

Returns the value of attribute scope_names.



24
25
26
# File 'lib/graphql_rails/router.rb', line 24

def scope_names
  @scope_names
end

Class Method Details

.draw(&block) ⇒ Object



18
19
20
21
22
# File 'lib/graphql_rails/router.rb', line 18

def self.draw(&block)
  new.tap do |router|
    router.instance_eval(&block)
  end
end

Instance Method Details

#event(name, **options) ⇒ Object



66
67
68
# File 'lib/graphql_rails/router.rb', line 66

def event(name, **options)
  routes << build_route(EventRoute, name, **options)
end

#graphql_schema(group = nil) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/graphql_rails/router.rb', line 76

def graphql_schema(group = nil)
  @graphql_schema[group&.to_sym] ||= SchemaBuilder.new(
    queries: routes.select(&:query?),
    mutations: routes.select(&:mutation?),
    events: routes.select(&:event?),
    raw_actions: raw_graphql_actions,
    group: group
  ).call
end

#group(*group_names, &block) ⇒ Object



35
36
37
38
39
# File 'lib/graphql_rails/router.rb', line 35

def group(*group_names, &block)
  scoped_router = router_with(group_names: group_names)
  scoped_router.instance_eval(&block)
  routes.merge(scoped_router.routes)
end

#mutation(name, **options) ⇒ Object



62
63
64
# File 'lib/graphql_rails/router.rb', line 62

def mutation(name, **options)
  routes << build_route(MutationRoute, name, **options)
end

#namespace(namespace_name, &block) ⇒ Object



47
48
49
# File 'lib/graphql_rails/router.rb', line 47

def namespace(namespace_name, &block)
  scope(path: namespace_name, module: namespace_name, &block)
end

#query(name, **options) ⇒ Object



58
59
60
# File 'lib/graphql_rails/router.rb', line 58

def query(name, **options)
  routes << build_route(QueryRoute, name, **options)
end

#reload_schemaObject



86
87
88
# File 'lib/graphql_rails/router.rb', line 86

def reload_schema
  @graphql_schema.clear
end

#resources(name, **options, &block) ⇒ Object



51
52
53
54
55
56
# File 'lib/graphql_rails/router.rb', line 51

def resources(name, **options, &block)
  builder_options = full_route_options(options)
  routes_builder = ResourceRoutesBuilder.new(name, **builder_options)
  routes_builder.instance_eval(&block) if block
  routes.merge(routes_builder.routes)
end

#scope(new_scope_name = nil, **options, &block) ⇒ Object



41
42
43
44
45
# File 'lib/graphql_rails/router.rb', line 41

def scope(new_scope_name = nil, **options, &block)
  scoped_router = router_with_scope_params(new_scope_name, **options)
  scoped_router.instance_eval(&block)
  routes.merge(scoped_router.routes)
end