graphql_rails 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Gemfile.lock +2 -2
- data/README.md +76 -8
- data/graphql_rails.gemspec +1 -1
- data/lib/graphql_rails/attribute.rb +7 -42
- data/lib/graphql_rails/attribute/attribute_type_parser.rb +99 -0
- data/lib/graphql_rails/controller.rb +17 -5
- data/lib/graphql_rails/controller/action.rb +93 -0
- data/lib/graphql_rails/controller/action_configuration.rb +1 -2
- data/lib/graphql_rails/controller/controller_function.rb +7 -6
- data/lib/graphql_rails/controller/request.rb +3 -3
- data/lib/graphql_rails/model.rb +1 -1
- data/lib/graphql_rails/model/configuration.rb +14 -50
- data/lib/graphql_rails/model/graphql_type_builder.rb +37 -0
- data/lib/graphql_rails/router.rb +36 -19
- data/lib/graphql_rails/router/{mutation_action.rb → mutation_route.rb} +2 -2
- data/lib/graphql_rails/router/{query_action.rb → query_route.rb} +2 -2
- data/lib/graphql_rails/router/resource_routes_builder.rb +82 -0
- data/lib/graphql_rails/router/route.rb +40 -0
- data/lib/graphql_rails/router/schema_builder.rb +9 -5
- data/lib/graphql_rails/rspec_controller_helpers.rb +94 -0
- data/lib/graphql_rails/version.rb +1 -1
- metadata +14 -29
- data/lib/graphiti.rb +0 -10
- data/lib/graphiti/attribute.rb +0 -86
- data/lib/graphiti/controller.rb +0 -54
- data/lib/graphiti/controller/action_configuration.rb +0 -46
- data/lib/graphiti/controller/configuration.rb +0 -32
- data/lib/graphiti/controller/controller_function.rb +0 -41
- data/lib/graphiti/controller/request.rb +0 -40
- data/lib/graphiti/errors/execution_error.rb +0 -18
- data/lib/graphiti/errors/validation_error.rb +0 -26
- data/lib/graphiti/model.rb +0 -33
- data/lib/graphiti/model/configuration.rb +0 -81
- data/lib/graphiti/router.rb +0 -65
- data/lib/graphiti/router/action.rb +0 -21
- data/lib/graphiti/router/mutation_action.rb +0 -18
- data/lib/graphiti/router/query_action.rb +0 -18
- data/lib/graphiti/router/resource_actions_builder.rb +0 -82
- data/lib/graphiti/router/schema_builder.rb +0 -37
- data/lib/graphiti/version.rb +0 -5
- data/lib/graphql_rails/controller/action_path_parser.rb +0 -75
- data/lib/graphql_rails/router/action.rb +0 -21
- data/lib/graphql_rails/router/resource_actions_builder.rb +0 -82
@@ -1,37 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module GraphqlRails
|
4
|
-
class Router
|
5
|
-
# builds GraphQL::Schema based on previously defined grahiti data
|
6
|
-
class SchemaBuilder
|
7
|
-
attr_reader :queries, :mutations
|
8
|
-
|
9
|
-
def initialize(queries:, mutations:)
|
10
|
-
@queries = queries
|
11
|
-
@mutations = mutations
|
12
|
-
end
|
13
|
-
|
14
|
-
def call
|
15
|
-
query_type = build_type('Query', queries)
|
16
|
-
mutation_type = build_type('Mutation', mutations)
|
17
|
-
|
18
|
-
GraphQL::Schema.define do
|
19
|
-
query(query_type)
|
20
|
-
mutation(mutation_type)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
private
|
25
|
-
|
26
|
-
def build_type(type_name, actions)
|
27
|
-
GraphQL::ObjectType.define do
|
28
|
-
name type_name
|
29
|
-
|
30
|
-
actions.each do |action|
|
31
|
-
field action.name, action.options
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
data/lib/graphiti/version.rb
DELETED
@@ -1,75 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'active_support/core_ext/string/inflections'
|
4
|
-
require_relative 'request'
|
5
|
-
|
6
|
-
module GraphqlRails
|
7
|
-
class Controller
|
8
|
-
# graphql resolver which redirects actions to appropriate controller and controller action
|
9
|
-
class ActionPathParser
|
10
|
-
# accepts path of given format "controller_name#action"
|
11
|
-
def initialize(action_path, **options)
|
12
|
-
@action_path = action_path
|
13
|
-
@module_name = options[:module] || ''
|
14
|
-
end
|
15
|
-
|
16
|
-
def return_type
|
17
|
-
return_type = action.return_type || default_type
|
18
|
-
|
19
|
-
if action.can_return_nil?
|
20
|
-
return_type
|
21
|
-
else
|
22
|
-
return_type.to_non_null_type
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def arguments
|
27
|
-
action.attributes.values
|
28
|
-
end
|
29
|
-
|
30
|
-
def controller
|
31
|
-
@controller ||= "#{namespaced_controller_name}_controller".classify.constantize
|
32
|
-
end
|
33
|
-
|
34
|
-
def action_name
|
35
|
-
@action_name ||= action_path.split('#').last
|
36
|
-
end
|
37
|
-
|
38
|
-
private
|
39
|
-
|
40
|
-
attr_reader :action_path, :module_name
|
41
|
-
|
42
|
-
def action
|
43
|
-
controller.controller_configuration.action(action_name)
|
44
|
-
end
|
45
|
-
|
46
|
-
def namespaced_controller_name
|
47
|
-
[module_name, controller_name].reject(&:empty?).join('/')
|
48
|
-
end
|
49
|
-
|
50
|
-
def controller_name
|
51
|
-
@controller_name ||= action_path.split('#').first
|
52
|
-
end
|
53
|
-
|
54
|
-
def action_model
|
55
|
-
model_path = namespaced_controller_name.singularize.classify.split('::')
|
56
|
-
model_name = model_path.pop
|
57
|
-
|
58
|
-
while model_path.any?
|
59
|
-
begin
|
60
|
-
return [model_path, model_name].join('::').constantize
|
61
|
-
rescue NameError => err
|
62
|
-
raise unless err.message.match?(/uninitialized constant/)
|
63
|
-
model_path.pop
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
model_name.constantize
|
68
|
-
end
|
69
|
-
|
70
|
-
def default_type
|
71
|
-
action_model.graphql.graphql_type
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative '../controller/controller_function'
|
4
|
-
|
5
|
-
module GraphqlRails
|
6
|
-
class Router
|
7
|
-
# Generic class for any type graphql action. Should not be used directly
|
8
|
-
class Action
|
9
|
-
attr_reader :name, :controller_action_path
|
10
|
-
|
11
|
-
def initialize(name, to:, **options)
|
12
|
-
@name = name.to_s.camelize(:lower)
|
13
|
-
@controller_action_path = [options[:module].to_s, to].reject(&:empty?).join('/')
|
14
|
-
end
|
15
|
-
|
16
|
-
def options
|
17
|
-
{ function: Controller::ControllerFunction.build(controller_action_path) }
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
@@ -1,82 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'query_action'
|
4
|
-
require_relative 'mutation_action'
|
5
|
-
|
6
|
-
module GraphqlRails
|
7
|
-
class Router
|
8
|
-
# Generates graphql actions based on resource name and options
|
9
|
-
class ResourceActionsBuilder
|
10
|
-
AVAILABLE_ACTIONS = %i[show index create update destroy].freeze
|
11
|
-
|
12
|
-
def initialize(name, only: nil, except: [], **options)
|
13
|
-
@name = name.to_s
|
14
|
-
|
15
|
-
@options = options
|
16
|
-
@autogenerated_action_names = initial_action_names(only, except, AVAILABLE_ACTIONS)
|
17
|
-
end
|
18
|
-
|
19
|
-
def actions
|
20
|
-
@actions ||= initial_actions
|
21
|
-
end
|
22
|
-
|
23
|
-
def query(*args)
|
24
|
-
actions << build_query(*args)
|
25
|
-
end
|
26
|
-
|
27
|
-
def mutation(*args)
|
28
|
-
actions << build_mutation(*args)
|
29
|
-
end
|
30
|
-
|
31
|
-
private
|
32
|
-
|
33
|
-
attr_reader :autogenerated_action_names, :name, :options
|
34
|
-
|
35
|
-
def initial_actions
|
36
|
-
actions = initial_query_actions
|
37
|
-
actions << build_mutation(:create, on: :member) if autogenerated_action_names.include?(:create)
|
38
|
-
actions << build_mutation(:update, on: :member) if autogenerated_action_names.include?(:update)
|
39
|
-
actions << build_mutation(:destroy, on: :member) if autogenerated_action_names.include?(:destroy)
|
40
|
-
actions
|
41
|
-
end
|
42
|
-
|
43
|
-
def initial_query_actions
|
44
|
-
actions = Set.new
|
45
|
-
|
46
|
-
if autogenerated_action_names.include?(:show)
|
47
|
-
actions << build_action(QueryAction, 'show', to: "#{name}#show", prefix: '', on: :member)
|
48
|
-
end
|
49
|
-
|
50
|
-
if autogenerated_action_names.include?(:index)
|
51
|
-
actions << build_action(QueryAction, 'index', to: "#{name}#index", prefix: '', on: :collection)
|
52
|
-
end
|
53
|
-
|
54
|
-
actions
|
55
|
-
end
|
56
|
-
|
57
|
-
def build_mutation(*args)
|
58
|
-
build_action(MutationAction, *args)
|
59
|
-
end
|
60
|
-
|
61
|
-
def build_query(*args)
|
62
|
-
build_action(QueryAction, *args)
|
63
|
-
end
|
64
|
-
|
65
|
-
def build_action(builder, action, prefix: action, on: :member, **custom_options)
|
66
|
-
action_options = options.merge(custom_options)
|
67
|
-
action_name = [prefix, resource_name(on)].reject(&:empty?).join('_')
|
68
|
-
builder.new(action_name, to: "#{name}##{action}", **action_options)
|
69
|
-
end
|
70
|
-
|
71
|
-
def initial_action_names(only, except, available)
|
72
|
-
alowed_actions = Array(only || available) & available
|
73
|
-
only_actions = alowed_actions.map(&:to_sym) - Array(except).map(&:to_sym)
|
74
|
-
Set.new(only_actions)
|
75
|
-
end
|
76
|
-
|
77
|
-
def resource_name(type)
|
78
|
-
type.to_sym == :member ? name.singularize : name
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|