graphql_rails 0.6.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. checksums.yaml +4 -4
  2. data/.hound.yml +1 -0
  3. data/.rubocop.yml +3 -3
  4. data/.ruby-version +1 -1
  5. data/.travis.yml +2 -2
  6. data/CHANGELOG.md +38 -0
  7. data/Gemfile +3 -2
  8. data/Gemfile.lock +181 -71
  9. data/docs/README.md +48 -9
  10. data/docs/_sidebar.md +5 -0
  11. data/docs/components/controller.md +294 -8
  12. data/docs/components/decorator.md +69 -0
  13. data/docs/components/model.md +349 -11
  14. data/docs/components/routes.md +43 -1
  15. data/docs/getting_started/quick_start.md +9 -2
  16. data/docs/index.html +1 -1
  17. data/docs/logging_and_monitoring/logging_and_monitoring.md +35 -0
  18. data/docs/other_tools/query_runner.md +49 -0
  19. data/docs/other_tools/schema_dump.md +29 -0
  20. data/docs/testing/testing.md +3 -1
  21. data/graphql_rails.gemspec +5 -4
  22. data/lib/generators/graphql_rails/install_generator.rb +50 -0
  23. data/lib/generators/graphql_rails/templates/example_users_controller.erb +19 -0
  24. data/lib/generators/graphql_rails/templates/graphql_application_controller.erb +8 -0
  25. data/lib/generators/graphql_rails/templates/graphql_controller.erb +20 -0
  26. data/lib/generators/graphql_rails/templates/graphql_router.erb +19 -0
  27. data/lib/generators/graphql_rails/templates/graphql_router_spec.erb +21 -0
  28. data/lib/graphql_rails.rb +7 -1
  29. data/lib/graphql_rails/attributes.rb +13 -0
  30. data/lib/graphql_rails/{attribute → attributes}/attributable.rb +25 -20
  31. data/lib/graphql_rails/attributes/attribute.rb +94 -0
  32. data/lib/graphql_rails/{attribute → attributes}/attribute_name_parser.rb +2 -2
  33. data/lib/graphql_rails/attributes/input_attribute.rb +65 -0
  34. data/lib/graphql_rails/attributes/input_type_parser.rb +62 -0
  35. data/lib/graphql_rails/attributes/type_name_info.rb +38 -0
  36. data/lib/graphql_rails/attributes/type_parseable.rb +128 -0
  37. data/lib/graphql_rails/attributes/type_parser.rb +121 -0
  38. data/lib/graphql_rails/concerns/service.rb +19 -0
  39. data/lib/graphql_rails/controller.rb +42 -21
  40. data/lib/graphql_rails/controller/action.rb +12 -67
  41. data/lib/graphql_rails/controller/action_configuration.rb +71 -31
  42. data/lib/graphql_rails/controller/build_controller_action_resolver.rb +52 -0
  43. data/lib/graphql_rails/controller/build_controller_action_resolver/controller_action_resolver.rb +28 -0
  44. data/lib/graphql_rails/controller/configuration.rb +56 -4
  45. data/lib/graphql_rails/controller/log_controller_action.rb +71 -0
  46. data/lib/graphql_rails/controller/request.rb +29 -8
  47. data/lib/graphql_rails/controller/request/format_errors.rb +58 -0
  48. data/lib/graphql_rails/decorator.rb +41 -0
  49. data/lib/graphql_rails/decorator/relation_decorator.rb +75 -0
  50. data/lib/graphql_rails/errors/custom_execution_error.rb +22 -0
  51. data/lib/graphql_rails/errors/execution_error.rb +8 -7
  52. data/lib/graphql_rails/errors/system_error.rb +14 -0
  53. data/lib/graphql_rails/errors/validation_error.rb +1 -5
  54. data/lib/graphql_rails/input_configurable.rb +47 -0
  55. data/lib/graphql_rails/integrations.rb +19 -0
  56. data/lib/graphql_rails/integrations/lograge.rb +39 -0
  57. data/lib/graphql_rails/integrations/sentry.rb +34 -0
  58. data/lib/graphql_rails/model.rb +26 -4
  59. data/lib/graphql_rails/model/add_fields_to_graphql_type.rb +45 -0
  60. data/lib/graphql_rails/model/build_connection_type.rb +52 -0
  61. data/lib/graphql_rails/model/{configuration → build_connection_type}/count_items.rb +5 -5
  62. data/lib/graphql_rails/model/build_enum_type.rb +68 -0
  63. data/lib/graphql_rails/model/{graphql_input_type_builder.rb → build_graphql_input_type.rb} +10 -2
  64. data/lib/graphql_rails/model/call_graphql_model_method.rb +72 -0
  65. data/lib/graphql_rails/model/configurable.rb +6 -2
  66. data/lib/graphql_rails/model/configuration.rb +34 -19
  67. data/lib/graphql_rails/model/find_or_build_graphql_type.rb +64 -0
  68. data/lib/graphql_rails/model/find_or_build_graphql_type_class.rb +46 -0
  69. data/lib/graphql_rails/model/input.rb +25 -11
  70. data/lib/graphql_rails/query_runner.rb +68 -0
  71. data/lib/graphql_rails/railtie.rb +10 -0
  72. data/lib/graphql_rails/router.rb +40 -13
  73. data/lib/graphql_rails/router/resource_routes_builder.rb +19 -11
  74. data/lib/graphql_rails/router/route.rb +21 -6
  75. data/lib/graphql_rails/router/schema_builder.rb +36 -11
  76. data/lib/graphql_rails/rspec_controller_helpers.rb +6 -4
  77. data/lib/graphql_rails/tasks/dump_graphql_schema.rb +57 -0
  78. data/lib/graphql_rails/tasks/schema.rake +14 -0
  79. data/lib/graphql_rails/version.rb +1 -1
  80. metadata +78 -26
  81. data/README.md +0 -194
  82. data/lib/graphql_rails/attribute.rb +0 -28
  83. data/lib/graphql_rails/attribute/type_parser.rb +0 -115
  84. data/lib/graphql_rails/controller/controller_function.rb +0 -50
  85. data/lib/graphql_rails/controller/format_results.rb +0 -36
  86. data/lib/graphql_rails/model/graphql_type_builder.rb +0 -33
  87. data/lib/graphql_rails/model/input_attribute.rb +0 -47
@@ -3,7 +3,11 @@
3
3
  module GraphqlRails
4
4
  module Model
5
5
  # stores information about model specific config, like attributes and types
6
- class GraphqlInputTypeBuilder
6
+ class BuildGraphqlInputType
7
+ require 'graphql_rails/concerns/service'
8
+
9
+ include ::GraphqlRails::Service
10
+
7
11
  def initialize(name:, description: nil, attributes:)
8
12
  @name = name
9
13
  @attributes = attributes
@@ -20,7 +24,11 @@ module GraphqlRails
20
24
  description(type_description)
21
25
 
22
26
  type_attributes.each_value do |type_attribute|
23
- argument(*type_attribute.input_argument_args)
27
+ argument(*type_attribute.input_argument_args, **type_attribute.input_argument_options)
28
+ end
29
+
30
+ def self.inspect
31
+ "#{GraphQL::Schema::InputObject}(#{graphql_name})"
24
32
  end
25
33
  end
26
34
  end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphqlRails
4
+ module Model
5
+ # Executes model method and adds additional meta data if needed
6
+ class CallGraphqlModelMethod
7
+ require 'graphql_rails/concerns/service'
8
+
9
+ include ::GraphqlRails::Service
10
+
11
+ PAGINATION_KEYS = %i[before after first last].freeze
12
+
13
+ def initialize(model:, method_keyword_arguments:, graphql_context:, attribute_config:)
14
+ @model = model
15
+ @method_keyword_arguments = method_keyword_arguments
16
+ @graphql_context = graphql_context
17
+ @attribute_config = attribute_config
18
+ end
19
+
20
+ def call
21
+ with_graphql_context do
22
+ run_method
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ attr_reader :model, :attribute_config, :graphql_context, :method_keyword_arguments
29
+
30
+ def run_method
31
+ if custom_keyword_arguments.empty?
32
+ model.send(method_name)
33
+ else
34
+ formatted_arguments = formatted_method_input(custom_keyword_arguments)
35
+ model.send(method_name, **formatted_arguments)
36
+ end
37
+ end
38
+
39
+ def formatted_method_input(keyword_arguments)
40
+ keyword_arguments.transform_values do |input_argument|
41
+ formatted_method_input_argument(input_argument)
42
+ end
43
+ end
44
+
45
+ def formatted_method_input_argument(argument)
46
+ return argument.to_h if argument.is_a?(GraphQL::Schema::InputObject)
47
+
48
+ argument
49
+ end
50
+
51
+ def method_name
52
+ attribute_config.property
53
+ end
54
+
55
+ def paginated?
56
+ attribute_config.paginated?
57
+ end
58
+
59
+ def custom_keyword_arguments
60
+ return method_keyword_arguments unless paginated?
61
+
62
+ method_keyword_arguments.except(*PAGINATION_KEYS)
63
+ end
64
+
65
+ def with_graphql_context
66
+ return yield unless model.respond_to?(:with_graphql_context)
67
+
68
+ model.with_graphql_context(graphql_context) { yield }
69
+ end
70
+ end
71
+ end
72
+ end
@@ -9,11 +9,15 @@ module GraphqlRails
9
9
  @attributes ||= {}
10
10
  end
11
11
 
12
- def name(type_name = nil)
13
- @name = type_name if type_name
12
+ def name(graphql_name = nil)
13
+ @name = graphql_name if graphql_name
14
14
  @name || default_name
15
15
  end
16
16
 
17
+ def type_name
18
+ @type_name ||= "#{name.camelize}Type#{SecureRandom.hex}"
19
+ end
20
+
17
21
  def description(new_description = nil)
18
22
  @description = new_description if new_description
19
23
  @description
@@ -1,10 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'graphql_rails/attribute'
4
- require 'graphql_rails/model/graphql_type_builder'
3
+ require 'graphql_rails/attributes'
4
+ require 'graphql_rails/model/find_or_build_graphql_type'
5
+ require 'graphql_rails/model/build_enum_type'
5
6
  require 'graphql_rails/model/input'
6
7
  require 'graphql_rails/model/configurable'
7
- require 'graphql_rails/model/configuration/count_items'
8
+ require 'graphql_rails/model/build_connection_type'
8
9
 
9
10
  module GraphqlRails
10
11
  module Model
@@ -16,13 +17,28 @@ module GraphqlRails
16
17
  @model_class = model_class
17
18
  end
18
19
 
19
- def attribute(attribute_name, type: nil, **attribute_options)
20
- attributes[attribute_name.to_s] = \
21
- Attribute.new(
22
- attribute_name,
23
- type,
24
- attribute_options
25
- )
20
+ def initialize_copy(other)
21
+ super
22
+ @connection_type = nil
23
+ @graphql_type = nil
24
+ @input = other.instance_variable_get(:@input)&.transform_values(&:dup)
25
+ @attributes = other.instance_variable_get(:@attributes)&.transform_values(&:dup)
26
+ end
27
+
28
+ def attribute(attribute_name, **attribute_options)
29
+ key = attribute_name.to_s
30
+
31
+ attributes[key] ||= Attributes::Attribute.new(attribute_name)
32
+
33
+ attributes[key].tap do |attribute|
34
+ attribute_options.each do |method_name, args|
35
+ send_args = [method_name]
36
+ send_args << args if attribute.method(method_name).parameters.present?
37
+ attribute.public_send(*send_args)
38
+ end
39
+
40
+ yield(attribute) if block_given?
41
+ end
26
42
  end
27
43
 
28
44
  def input(input_name = nil)
@@ -35,22 +51,21 @@ module GraphqlRails
35
51
  end
36
52
 
37
53
  @input.fetch(name) do
38
- raise("GraphQL input with name #{input_name.inspect} is not defined for #{model_class}")
54
+ raise("GraphQL input with name #{input_name.inspect} is not defined for #{model_class.name}")
39
55
  end
40
56
  end
41
57
 
42
58
  def graphql_type
43
- @graphql_type ||= GraphqlTypeBuilder.new(
44
- name: name, description: description, attributes: attributes
45
- ).call
59
+ @graphql_type ||= FindOrBuildGraphqlType.call(
60
+ name: name,
61
+ description: description,
62
+ attributes: attributes,
63
+ type_name: type_name
64
+ )
46
65
  end
47
66
 
48
67
  def connection_type
49
- @connection_type ||= begin
50
- graphql_type.define_connection do
51
- field :total, types.Int, resolve: CountItems
52
- end
53
- end
68
+ @connection_type ||= BuildConnectionType.call(graphql_type)
54
69
  end
55
70
 
56
71
  private
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphqlRails
4
+ module Model
5
+ # stores information about model specific config, like attributes and types
6
+ class FindOrBuildGraphqlType
7
+ require 'graphql_rails/concerns/service'
8
+ require 'graphql_rails/model/find_or_build_graphql_type_class'
9
+ require 'graphql_rails/model/add_fields_to_graphql_type'
10
+
11
+ include ::GraphqlRails::Service
12
+
13
+ def initialize(name:, description:, attributes:, type_name:)
14
+ @name = name
15
+ @description = description
16
+ @attributes = attributes
17
+ @type_name = type_name
18
+ end
19
+
20
+ def call
21
+ klass.tap { add_fields_to_graphql_type if new_class? }
22
+ end
23
+
24
+ private
25
+
26
+ attr_reader :name, :description, :attributes, :type_name
27
+
28
+ delegate :klass, :new_class?, to: :type_class_finder
29
+
30
+ def type_class_finder
31
+ @type_class_finder ||= FindOrBuildGraphqlTypeClass.new(
32
+ name: name,
33
+ type_name: type_name,
34
+ description: description
35
+ )
36
+ end
37
+
38
+ def add_fields_to_graphql_type
39
+ AddFieldsToGraphqlType.call(klass: klass, attributes: attributes.values.select(&:scalar_type?))
40
+
41
+ attributes.values.reject(&:scalar_type?).tap do |dynamic_attributes|
42
+ find_or_build_dynamic_graphql_types(dynamic_attributes) do |name, description, attributes, type_name|
43
+ self.class.call(
44
+ name: name, description: description,
45
+ attributes: attributes, type_name: type_name
46
+ )
47
+ end
48
+ AddFieldsToGraphqlType.call(klass: klass, attributes: dynamic_attributes)
49
+ end
50
+ end
51
+
52
+ def find_or_build_dynamic_graphql_types(dynamic_attributes)
53
+ dynamic_attributes.each do |attribute|
54
+ yield(
55
+ attribute.graphql_model.graphql.name,
56
+ attribute.graphql_model.graphql.description,
57
+ attribute.graphql_model.graphql.attributes,
58
+ attribute.graphql_model.graphql.type_name
59
+ )
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphqlRails
4
+ module Model
5
+ # Initializes class to define graphql type and fields.
6
+ class FindOrBuildGraphqlTypeClass
7
+ require 'graphql_rails/concerns/service'
8
+
9
+ include ::GraphqlRails::Service
10
+
11
+ def initialize(name:, type_name:, description: nil)
12
+ @name = name
13
+ @type_name = type_name
14
+ @description = description
15
+ @new_class = false
16
+ end
17
+
18
+ def klass
19
+ @klass ||= Object.const_defined?(type_name) && Object.const_get(type_name) || build_graphql_type_klass
20
+ end
21
+
22
+ def new_class?
23
+ new_class
24
+ end
25
+
26
+ private
27
+
28
+ attr_accessor :new_class
29
+ attr_reader :name, :type_name, :description
30
+
31
+ def build_graphql_type_klass
32
+ graphql_type_name = name
33
+ graphql_type_description = description
34
+
35
+ graphql_type_klass = Class.new(GraphQL::Schema::Object) do
36
+ graphql_name(graphql_type_name)
37
+ description(graphql_type_description)
38
+ end
39
+
40
+ self.new_class = true
41
+
42
+ Object.const_set(type_name, graphql_type_klass)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'graphql_rails/model/graphql_input_type_builder'
3
+ require 'graphql_rails/model/build_graphql_input_type'
4
4
  require 'graphql_rails/model/configurable'
5
5
 
6
6
  module GraphqlRails
@@ -14,19 +14,23 @@ module GraphqlRails
14
14
  @input_name_suffix = input_name_suffix
15
15
  end
16
16
 
17
+ def initialize_copy(other)
18
+ super
19
+ @attributes = other.instance_variable_get(:@attributes)&.transform_values(&:dup)
20
+ end
21
+
17
22
  def graphql_input_type
18
- @graphql_input_type ||= GraphqlInputTypeBuilder.new(
23
+ @graphql_input_type ||= BuildGraphqlInputType.call(
19
24
  name: name, description: description, attributes: attributes
20
- ).call
25
+ )
21
26
  end
22
27
 
23
- def attribute(attribute_name, type: nil, **attribute_options)
24
- attributes[attribute_name.to_s] = \
25
- InputAttribute.new(
26
- attribute_name,
27
- type,
28
- attribute_options
29
- )
28
+ def attribute(attribute_name, type: nil, enum: nil, **attribute_options)
29
+ input_type = attribute_type(attribute_name, type: type, enum: enum, **attribute_options)
30
+
31
+ attributes[attribute_name.to_s] = Attributes::InputAttribute.new(
32
+ attribute_name, type: input_type, **attribute_options
33
+ )
30
34
  end
31
35
 
32
36
  private
@@ -35,10 +39,20 @@ module GraphqlRails
35
39
 
36
40
  def default_name
37
41
  @default_name ||= begin
38
- suffix = input_name_suffix ? input_name_suffix.to_s.tableize : ''
42
+ suffix = input_name_suffix ? input_name_suffix.to_s.camelize : ''
39
43
  "#{model_class.name.split('::').last}#{suffix}Input"
40
44
  end
41
45
  end
46
+
47
+ def attribute_type(attribute_name, type:, enum:, description: nil, **_other)
48
+ return type unless enum
49
+
50
+ BuildEnumType.call(
51
+ "#{name}_#{attribute_name}_enum",
52
+ allowed_values: enum,
53
+ description: description
54
+ )
55
+ end
42
56
  end
43
57
  end
44
58
  end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphqlRails
4
+ # executes GraphQL queries and returns json
5
+ class QueryRunner
6
+ require 'graphql_rails/router'
7
+ require 'graphql_rails/concerns/service'
8
+
9
+ include ::GraphqlRails::Service
10
+
11
+ def initialize(group: nil, params:, schema: nil, router: nil, **schema_options)
12
+ @group = group
13
+ @graphql_schema = schema
14
+ @params = params
15
+ @schema_options = schema_options
16
+ @router = router
17
+ end
18
+
19
+ def call
20
+ graphql_schema.execute(
21
+ params[:query],
22
+ variables: variables,
23
+ operation_name: params[:operationName],
24
+ **schema_options
25
+ )
26
+ end
27
+
28
+ private
29
+
30
+ attr_reader :schema_options, :params, :group
31
+
32
+ def variables
33
+ ensure_hash(params[:variables])
34
+ end
35
+
36
+ def graphql_schema
37
+ @graphql_schema ||= router_schema
38
+ end
39
+
40
+ def router
41
+ @router ||= ::GraphqlRouter
42
+ end
43
+
44
+ def router_schema
45
+ router.graphql_schema(group)
46
+ end
47
+
48
+ def ensure_hash(ambiguous_param)
49
+ if ambiguous_param.blank?
50
+ {}
51
+ elsif ambiguous_param.is_a?(String)
52
+ ensure_hash(JSON.parse(ambiguous_param))
53
+ elsif kind_of_hash?(ambiguous_param)
54
+ ambiguous_param
55
+ else
56
+ raise ArgumentError, "Unexpected parameter: #{ambiguous_param.inspect}"
57
+ end
58
+ end
59
+
60
+ def kind_of_hash?(object)
61
+ return true if object.is_a?(Hash)
62
+
63
+ defined?(ActionController) &&
64
+ defined?(ActionController::Parameters) &&
65
+ object.is_a?(ActionController::Parameters)
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphqlRails
4
+ # Used to load rake tasks in RoR app
5
+ class Railtie < Rails::Railtie
6
+ rake_tasks do
7
+ load 'graphql_rails/tasks/schema.rake'
8
+ end
9
+ end
10
+ end