graphql 1.13.6 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (216) hide show
  1. checksums.yaml +4 -4
  2. data/lib/generators/graphql/core.rb +0 -7
  3. data/lib/generators/graphql/enum_generator.rb +4 -10
  4. data/lib/generators/graphql/field_extractor.rb +31 -0
  5. data/lib/generators/graphql/input_generator.rb +50 -0
  6. data/lib/generators/graphql/install/mutation_root_generator.rb +34 -0
  7. data/lib/generators/graphql/{templates → install/templates}/base_mutation.erb +0 -0
  8. data/lib/generators/graphql/{templates → install/templates}/mutation_type.erb +0 -0
  9. data/lib/generators/graphql/install_generator.rb +1 -1
  10. data/lib/generators/graphql/interface_generator.rb +7 -7
  11. data/lib/generators/graphql/mutation_create_generator.rb +22 -0
  12. data/lib/generators/graphql/mutation_delete_generator.rb +22 -0
  13. data/lib/generators/graphql/mutation_generator.rb +5 -30
  14. data/lib/generators/graphql/mutation_update_generator.rb +22 -0
  15. data/lib/generators/graphql/object_generator.rb +8 -37
  16. data/lib/generators/graphql/orm_mutations_base.rb +40 -0
  17. data/lib/generators/graphql/scalar_generator.rb +4 -2
  18. data/lib/generators/graphql/templates/enum.erb +5 -1
  19. data/lib/generators/graphql/templates/input.erb +9 -0
  20. data/lib/generators/graphql/templates/interface.erb +4 -2
  21. data/lib/generators/graphql/templates/mutation.erb +1 -1
  22. data/lib/generators/graphql/templates/mutation_create.erb +20 -0
  23. data/lib/generators/graphql/templates/mutation_delete.erb +20 -0
  24. data/lib/generators/graphql/templates/mutation_update.erb +21 -0
  25. data/lib/generators/graphql/templates/object.erb +4 -2
  26. data/lib/generators/graphql/templates/scalar.erb +3 -1
  27. data/lib/generators/graphql/templates/union.erb +4 -2
  28. data/lib/generators/graphql/type_generator.rb +46 -9
  29. data/lib/generators/graphql/union_generator.rb +5 -5
  30. data/lib/graphql/analysis/ast/query_complexity.rb +1 -1
  31. data/lib/graphql/analysis/ast/query_depth.rb +0 -1
  32. data/lib/graphql/analysis/ast/visitor.rb +3 -2
  33. data/lib/graphql/analysis/ast.rb +0 -10
  34. data/lib/graphql/analysis.rb +0 -7
  35. data/lib/graphql/backtrace/table.rb +0 -18
  36. data/lib/graphql/backtrace/tracer.rb +1 -2
  37. data/lib/graphql/backtrace.rb +2 -8
  38. data/lib/graphql/dataloader/source.rb +2 -2
  39. data/lib/graphql/dig.rb +1 -1
  40. data/lib/graphql/execution/errors.rb +1 -9
  41. data/lib/graphql/execution/interpreter/runtime.rb +2 -9
  42. data/lib/graphql/execution/interpreter.rb +0 -22
  43. data/lib/graphql/execution/lazy.rb +1 -1
  44. data/lib/graphql/execution/lookahead.rb +6 -13
  45. data/lib/graphql/execution/multiplex.rb +50 -107
  46. data/lib/graphql/execution.rb +11 -3
  47. data/lib/graphql/introspection/dynamic_fields.rb +3 -8
  48. data/lib/graphql/introspection/entry_points.rb +2 -15
  49. data/lib/graphql/introspection.rb +4 -4
  50. data/lib/graphql/language/document_from_schema_definition.rb +0 -17
  51. data/lib/graphql/pagination/connections.rb +2 -28
  52. data/lib/graphql/query/context.rb +1 -185
  53. data/lib/graphql/query/literal_input.rb +8 -13
  54. data/lib/graphql/query/validation_pipeline.rb +8 -37
  55. data/lib/graphql/query/variables.rb +22 -18
  56. data/lib/graphql/query.rb +5 -35
  57. data/lib/graphql/railtie.rb +0 -104
  58. data/lib/graphql/relay.rb +0 -15
  59. data/lib/graphql/rubocop/graphql/default_required_true.rb +4 -4
  60. data/lib/graphql/schema/addition.rb +1 -8
  61. data/lib/graphql/schema/argument.rb +18 -25
  62. data/lib/graphql/schema/build_from_definition.rb +0 -1
  63. data/lib/graphql/schema/directive.rb +4 -21
  64. data/lib/graphql/schema/enum.rb +1 -18
  65. data/lib/graphql/schema/enum_value.rb +0 -22
  66. data/lib/graphql/schema/field.rb +22 -194
  67. data/lib/graphql/schema/input_object.rb +10 -57
  68. data/lib/graphql/schema/interface.rb +0 -25
  69. data/lib/graphql/schema/introspection_system.rb +3 -8
  70. data/lib/graphql/schema/late_bound_type.rb +2 -2
  71. data/lib/graphql/schema/list.rb +0 -6
  72. data/lib/graphql/schema/loader.rb +0 -1
  73. data/lib/graphql/schema/member/base_dsl_methods.rb +0 -5
  74. data/lib/graphql/schema/member/build_type.rb +4 -6
  75. data/lib/graphql/schema/member/has_arguments.rb +14 -18
  76. data/lib/graphql/schema/member/has_fields.rb +2 -2
  77. data/lib/graphql/schema/member/has_interfaces.rb +0 -2
  78. data/lib/graphql/schema/member.rb +0 -6
  79. data/lib/graphql/schema/mutation.rb +0 -9
  80. data/lib/graphql/schema/non_null.rb +1 -7
  81. data/lib/graphql/schema/object.rb +0 -40
  82. data/lib/graphql/schema/relay_classic_mutation.rb +17 -28
  83. data/lib/graphql/schema/scalar.rb +0 -15
  84. data/lib/graphql/schema/union.rb +0 -16
  85. data/lib/graphql/schema/warden.rb +1 -1
  86. data/lib/graphql/schema/wrapper.rb +0 -5
  87. data/lib/graphql/schema.rb +107 -949
  88. data/lib/graphql/static_validation/base_visitor.rb +4 -21
  89. data/lib/graphql/static_validation/rules/directives_are_in_valid_locations.rb +12 -12
  90. data/lib/graphql/static_validation/validator.rb +2 -24
  91. data/lib/graphql/static_validation.rb +0 -2
  92. data/lib/graphql/subscriptions/default_subscription_resolve_extension.rb +38 -1
  93. data/lib/graphql/subscriptions/event.rb +1 -1
  94. data/lib/graphql/subscriptions/instrumentation.rb +0 -51
  95. data/lib/graphql/subscriptions.rb +4 -13
  96. data/lib/graphql/tracing/platform_tracing.rb +0 -23
  97. data/lib/graphql/tracing.rb +0 -1
  98. data/lib/graphql/types/iso_8601_date_time.rb +8 -1
  99. data/lib/graphql/types/relay/connection_behaviors.rb +4 -7
  100. data/lib/graphql/types/relay/default_relay.rb +0 -10
  101. data/lib/graphql/types/relay/node_behaviors.rb +5 -1
  102. data/lib/graphql/types/relay.rb +0 -2
  103. data/lib/graphql/version.rb +1 -1
  104. data/lib/graphql.rb +12 -65
  105. metadata +15 -129
  106. data/lib/graphql/analysis/analyze_query.rb +0 -98
  107. data/lib/graphql/analysis/field_usage.rb +0 -45
  108. data/lib/graphql/analysis/max_query_complexity.rb +0 -26
  109. data/lib/graphql/analysis/max_query_depth.rb +0 -26
  110. data/lib/graphql/analysis/query_complexity.rb +0 -88
  111. data/lib/graphql/analysis/query_depth.rb +0 -43
  112. data/lib/graphql/analysis/reducer_state.rb +0 -48
  113. data/lib/graphql/argument.rb +0 -131
  114. data/lib/graphql/authorization.rb +0 -82
  115. data/lib/graphql/backtrace/legacy_tracer.rb +0 -56
  116. data/lib/graphql/backwards_compatibility.rb +0 -61
  117. data/lib/graphql/base_type.rb +0 -232
  118. data/lib/graphql/boolean_type.rb +0 -2
  119. data/lib/graphql/compatibility/execution_specification/counter_schema.rb +0 -53
  120. data/lib/graphql/compatibility/execution_specification/specification_schema.rb +0 -200
  121. data/lib/graphql/compatibility/execution_specification.rb +0 -436
  122. data/lib/graphql/compatibility/lazy_execution_specification/lazy_schema.rb +0 -111
  123. data/lib/graphql/compatibility/lazy_execution_specification.rb +0 -215
  124. data/lib/graphql/compatibility/query_parser_specification/parse_error_specification.rb +0 -87
  125. data/lib/graphql/compatibility/query_parser_specification/query_assertions.rb +0 -79
  126. data/lib/graphql/compatibility/query_parser_specification.rb +0 -266
  127. data/lib/graphql/compatibility/schema_parser_specification.rb +0 -682
  128. data/lib/graphql/compatibility.rb +0 -5
  129. data/lib/graphql/define/assign_argument.rb +0 -12
  130. data/lib/graphql/define/assign_connection.rb +0 -13
  131. data/lib/graphql/define/assign_enum_value.rb +0 -18
  132. data/lib/graphql/define/assign_global_id_field.rb +0 -11
  133. data/lib/graphql/define/assign_mutation_function.rb +0 -34
  134. data/lib/graphql/define/assign_object_field.rb +0 -42
  135. data/lib/graphql/define/defined_object_proxy.rb +0 -53
  136. data/lib/graphql/define/instance_definable.rb +0 -240
  137. data/lib/graphql/define/no_definition_error.rb +0 -7
  138. data/lib/graphql/define/non_null_with_bang.rb +0 -16
  139. data/lib/graphql/define/type_definer.rb +0 -31
  140. data/lib/graphql/define.rb +0 -31
  141. data/lib/graphql/deprecated_dsl.rb +0 -55
  142. data/lib/graphql/directive/deprecated_directive.rb +0 -2
  143. data/lib/graphql/directive/include_directive.rb +0 -2
  144. data/lib/graphql/directive/skip_directive.rb +0 -2
  145. data/lib/graphql/directive.rb +0 -107
  146. data/lib/graphql/enum_type.rb +0 -133
  147. data/lib/graphql/execution/execute.rb +0 -333
  148. data/lib/graphql/execution/flatten.rb +0 -40
  149. data/lib/graphql/execution/typecast.rb +0 -50
  150. data/lib/graphql/field/resolve.rb +0 -59
  151. data/lib/graphql/field.rb +0 -226
  152. data/lib/graphql/float_type.rb +0 -2
  153. data/lib/graphql/function.rb +0 -128
  154. data/lib/graphql/id_type.rb +0 -2
  155. data/lib/graphql/input_object_type.rb +0 -138
  156. data/lib/graphql/int_type.rb +0 -2
  157. data/lib/graphql/interface_type.rb +0 -72
  158. data/lib/graphql/internal_representation/document.rb +0 -27
  159. data/lib/graphql/internal_representation/node.rb +0 -206
  160. data/lib/graphql/internal_representation/print.rb +0 -51
  161. data/lib/graphql/internal_representation/rewrite.rb +0 -184
  162. data/lib/graphql/internal_representation/scope.rb +0 -88
  163. data/lib/graphql/internal_representation/visit.rb +0 -36
  164. data/lib/graphql/internal_representation.rb +0 -7
  165. data/lib/graphql/list_type.rb +0 -80
  166. data/lib/graphql/non_null_type.rb +0 -71
  167. data/lib/graphql/object_type.rb +0 -130
  168. data/lib/graphql/query/arguments.rb +0 -189
  169. data/lib/graphql/query/arguments_cache.rb +0 -24
  170. data/lib/graphql/query/executor.rb +0 -52
  171. data/lib/graphql/query/serial_execution/field_resolution.rb +0 -92
  172. data/lib/graphql/query/serial_execution/operation_resolution.rb +0 -19
  173. data/lib/graphql/query/serial_execution/selection_resolution.rb +0 -23
  174. data/lib/graphql/query/serial_execution/value_resolution.rb +0 -87
  175. data/lib/graphql/query/serial_execution.rb +0 -40
  176. data/lib/graphql/relay/array_connection.rb +0 -83
  177. data/lib/graphql/relay/base_connection.rb +0 -189
  178. data/lib/graphql/relay/connection_instrumentation.rb +0 -54
  179. data/lib/graphql/relay/connection_resolve.rb +0 -43
  180. data/lib/graphql/relay/connection_type.rb +0 -41
  181. data/lib/graphql/relay/edge.rb +0 -27
  182. data/lib/graphql/relay/edge_type.rb +0 -19
  183. data/lib/graphql/relay/edges_instrumentation.rb +0 -39
  184. data/lib/graphql/relay/global_id_resolve.rb +0 -18
  185. data/lib/graphql/relay/mongo_relation_connection.rb +0 -50
  186. data/lib/graphql/relay/mutation/instrumentation.rb +0 -23
  187. data/lib/graphql/relay/mutation/resolve.rb +0 -56
  188. data/lib/graphql/relay/mutation/result.rb +0 -38
  189. data/lib/graphql/relay/mutation.rb +0 -106
  190. data/lib/graphql/relay/node.rb +0 -39
  191. data/lib/graphql/relay/page_info.rb +0 -7
  192. data/lib/graphql/relay/relation_connection.rb +0 -188
  193. data/lib/graphql/relay/type_extensions.rb +0 -32
  194. data/lib/graphql/scalar_type.rb +0 -91
  195. data/lib/graphql/schema/catchall_middleware.rb +0 -35
  196. data/lib/graphql/schema/default_parse_error.rb +0 -10
  197. data/lib/graphql/schema/default_type_error.rb +0 -17
  198. data/lib/graphql/schema/member/accepts_definition.rb +0 -164
  199. data/lib/graphql/schema/member/cached_graphql_definition.rb +0 -58
  200. data/lib/graphql/schema/member/instrumentation.rb +0 -131
  201. data/lib/graphql/schema/middleware_chain.rb +0 -82
  202. data/lib/graphql/schema/possible_types.rb +0 -44
  203. data/lib/graphql/schema/rescue_middleware.rb +0 -60
  204. data/lib/graphql/schema/timeout_middleware.rb +0 -88
  205. data/lib/graphql/schema/traversal.rb +0 -228
  206. data/lib/graphql/schema/validation.rb +0 -313
  207. data/lib/graphql/static_validation/default_visitor.rb +0 -15
  208. data/lib/graphql/static_validation/no_validate_visitor.rb +0 -10
  209. data/lib/graphql/string_type.rb +0 -2
  210. data/lib/graphql/subscriptions/subscription_root.rb +0 -76
  211. data/lib/graphql/tracing/skylight_tracing.rb +0 -70
  212. data/lib/graphql/types/relay/node_field.rb +0 -24
  213. data/lib/graphql/types/relay/nodes_field.rb +0 -43
  214. data/lib/graphql/union_type.rb +0 -115
  215. data/lib/graphql/upgrader/member.rb +0 -937
  216. data/lib/graphql/upgrader/schema.rb +0 -38
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d4cb09369d39b11ac4ebb242db3da4bc008a850cdd78c88816143cdfd84addd9
4
- data.tar.gz: 4c5fc1be26724efda2dfc4e0f6ae0ad0ff9593b8708cf38ce3ec3a2ffae759d7
3
+ metadata.gz: c54b2619d5d111575839ba3e5d34794d993cb37a0b9aee4d41347551bb6be846
4
+ data.tar.gz: f6d53678c268767478a2a86ec34c045ab8744f4ea6f97deeebed3708df634340
5
5
  SHA512:
6
- metadata.gz: ea28714d8d623e7591325672a08d031f03877f3744fdc18554ac0be2f2e3149dc2f99ee209f3b91d2d3f82564617d71eee5675a32ea1f60d0d75cc7cdffcf1b3
7
- data.tar.gz: 492bd5f0312bb01a79175fdf87388e2ffd51a3bbe2d8a485a78e30f89634fd1a5ec28fbb1626ea775b38cd1984a3abfa2cc7ba58bd6189165fc91f923a668e30
6
+ metadata.gz: c7ccf09ae0a94e0cdce47f66555681391ac29af9cb544b662e8f9cd127905cda5cf5923dc64500dcab15899c7c51d5d2458ae78b95cb40ca47b1ce792e35f52c
7
+ data.tar.gz: ba6d04f4fc107fe9e2daaf70bd06288f85a6ead011764ab1b975e1796e97fa8c289cc08186bbf23c6020670a585c1a48c22d2acbd28ed10c6ec09b97f07a2a47
@@ -25,13 +25,6 @@ module Graphql
25
25
  end
26
26
  end
27
27
 
28
- def create_mutation_root_type
29
- create_dir("#{options[:directory]}/mutations")
30
- template("base_mutation.erb", "#{options[:directory]}/mutations/base_mutation.rb", { skip: true })
31
- template("mutation_type.erb", "#{options[:directory]}/types/mutation_type.rb", { skip: true })
32
- insert_root_type('mutation', 'MutationType')
33
- end
34
-
35
28
  def schema_file_path
36
29
  "#{options[:directory]}/#{schema_name.underscore}.rb"
37
30
  end
@@ -13,20 +13,14 @@ module Graphql
13
13
  desc "Create a GraphQL::EnumType with the given name and values"
14
14
  source_root File.expand_path('../templates', __FILE__)
15
15
 
16
- argument :values,
17
- type: :array,
18
- default: [],
19
- banner: "value{:ruby_value} value{:ruby_value} ...",
20
- desc: "Values for this enum (if present, ruby_value will be inserted verbatim)"
16
+ private
21
17
 
22
- def create_type_file
23
- template "enum.erb", "#{options[:directory]}/types/#{type_file_name}.rb"
18
+ def graphql_type
19
+ "enum"
24
20
  end
25
21
 
26
- private
27
-
28
22
  def prepared_values
29
- values.map { |v| v.split(":", 2) }
23
+ custom_fields.map { |v| v.split(":", 2) }
30
24
  end
31
25
  end
32
26
  end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+ require 'rails/generators/base'
3
+
4
+ module Graphql
5
+ module Generators
6
+ module FieldExtractor
7
+ def fields
8
+ columns = []
9
+ columns += (klass&.columns&.map { |c| generate_column_string(c) } || [])
10
+ columns + custom_fields
11
+ end
12
+
13
+ def generate_column_string(column)
14
+ name = column.name
15
+ required = column.null ? "" : "!"
16
+ type = column_type_string(column)
17
+ "#{name}:#{required}#{type}"
18
+ end
19
+
20
+ def column_type_string(column)
21
+ column.name == "id" ? "ID" : column.type.to_s.camelize
22
+ end
23
+
24
+ def klass
25
+ @klass ||= Module.const_get(name.camelize)
26
+ rescue NameError
27
+ @klass = nil
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+ require 'generators/graphql/type_generator'
3
+ require 'generators/graphql/field_extractor'
4
+
5
+ module Graphql
6
+ module Generators
7
+ # Generate an input type by name,
8
+ # with the specified fields.
9
+ #
10
+ # ```
11
+ # rails g graphql:object PostType name:string!
12
+ # ```
13
+ class InputGenerator < TypeGeneratorBase
14
+ desc "Create a GraphQL::InputObjectType with the given name and fields"
15
+ source_root File.expand_path('../templates', __FILE__)
16
+ include FieldExtractor
17
+
18
+ def self.normalize_type_expression(type_expression, mode:, null: true)
19
+ case type_expression.camelize
20
+ when "Text", "Citext"
21
+ ["String", null]
22
+ when "Decimal"
23
+ ["Float", null]
24
+ when "DateTime", "Datetime"
25
+ ["GraphQL::Types::ISO8601DateTime", null]
26
+ when "Date"
27
+ ["GraphQL::Types::ISO8601Date", null]
28
+ when "Json", "Jsonb", "Hstore"
29
+ ["GraphQL::Types::JSON", null]
30
+ else
31
+ super
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def graphql_type
38
+ "input"
39
+ end
40
+
41
+ def type_ruby_name
42
+ super.gsub(/Type\z/, "InputType")
43
+ end
44
+
45
+ def type_file_name
46
+ super.gsub(/_type\z/, "_input_type")
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators/base"
4
+ require_relative "../core"
5
+
6
+ module Graphql
7
+ module Generators
8
+ module Install
9
+ class MutationRootGenerator < Rails::Generators::Base
10
+ include Core
11
+
12
+ desc "Create mutation base type, mutation root tipe, and adds the latter to the schema"
13
+ source_root File.expand_path('../templates', __FILE__)
14
+
15
+ class_option :schema,
16
+ type: :string,
17
+ default: nil,
18
+ desc: "Name for the schema constant (default: {app_name}Schema)"
19
+
20
+ class_option :skip_keeps,
21
+ type: :boolean,
22
+ default: false,
23
+ desc: "Skip .keep files for source control"
24
+
25
+ def generate
26
+ create_dir("#{options[:directory]}/mutations")
27
+ template("base_mutation.erb", "#{options[:directory]}/mutations/base_mutation.rb", { skip: true })
28
+ template("mutation_type.erb", "#{options[:directory]}/types/mutation_type.rb", { skip: true })
29
+ insert_root_type('mutation', 'MutationType')
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -109,7 +109,7 @@ module Graphql
109
109
  template("query_type.erb", "#{options[:directory]}/types/query_type.rb")
110
110
  insert_root_type('query', 'QueryType')
111
111
 
112
- create_mutation_root_type unless options.skip_mutation_root_type?
112
+ invoke "graphql:install:mutation_root" unless options.skip_mutation_root_type?
113
113
 
114
114
  template("graphql_controller.erb", "app/controllers/graphql_controller.rb")
115
115
  route('post "/graphql", to: "graphql#execute"')
@@ -13,14 +13,14 @@ module Graphql
13
13
  desc "Create a GraphQL::InterfaceType with the given name and fields"
14
14
  source_root File.expand_path('../templates', __FILE__)
15
15
 
16
- argument :fields,
17
- type: :array,
18
- default: [],
19
- banner: "name:type name:type ...",
20
- desc: "Fields for this interface (type may be expressed as Ruby or GraphQL)"
16
+ private
21
17
 
22
- def create_type_file
23
- template "interface.erb", "#{options[:directory]}/types/#{type_file_name}.rb"
18
+ def graphql_type
19
+ "interface"
20
+ end
21
+
22
+ def fields
23
+ custom_fields
24
24
  end
25
25
  end
26
26
  end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+ require_relative 'orm_mutations_base'
3
+
4
+ module Graphql
5
+ module Generators
6
+ # TODO: What other options should be supported?
7
+ #
8
+ # @example Generate a `GraphQL::Schema::RelayClassicMutation` by name
9
+ # rails g graphql:mutation CreatePostMutation
10
+ class MutationCreateGenerator < OrmMutationsBase
11
+
12
+ desc "Scaffold a Relay Classic ORM create mutation for the given model class"
13
+ source_root File.expand_path('../templates', __FILE__)
14
+
15
+ private
16
+
17
+ def operation_type
18
+ "create"
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+ require_relative 'orm_mutations_base'
3
+
4
+ module Graphql
5
+ module Generators
6
+ # TODO: What other options should be supported?
7
+ #
8
+ # @example Generate a `GraphQL::Schema::RelayClassicMutation` by name
9
+ # rails g graphql:mutation CreatePostMutation
10
+ class MutationDeleteGenerator < OrmMutationsBase
11
+
12
+ desc "Scaffold a Relay Classic ORM delete mutation for the given model class"
13
+ source_root File.expand_path('../templates', __FILE__)
14
+
15
+ private
16
+
17
+ def operation_type
18
+ "delete"
19
+ end
20
+ end
21
+ end
22
+ end
@@ -9,47 +9,22 @@ module Graphql
9
9
  #
10
10
  # @example Generate a `GraphQL::Schema::RelayClassicMutation` by name
11
11
  # rails g graphql:mutation CreatePostMutation
12
- class MutationGenerator < Rails::Generators::Base
12
+ class MutationGenerator < Rails::Generators::NamedBase
13
13
  include Core
14
14
 
15
15
  desc "Create a Relay Classic mutation by name"
16
16
  source_root File.expand_path('../templates', __FILE__)
17
17
 
18
- argument :name, type: :string
19
-
20
- def initialize(args, *options) # :nodoc:
21
- # Unfreeze name in case it's given as a frozen string
22
- args[0] = args[0].dup if args[0].is_a?(String) && args[0].frozen?
23
- super
24
-
25
- assign_names!(name)
26
- end
27
-
28
- attr_reader :file_name, :mutation_name, :field_name
29
-
30
18
  def create_mutation_file
31
- unless @behavior == :revoke
32
- create_mutation_root_type
33
- else
34
- log :gsub, "#{options[:directory]}/types/mutation_type.rb"
35
- end
36
-
37
- template "mutation.erb", "#{options[:directory]}/mutations/#{file_name}.rb"
19
+ template "mutation.erb", File.join(options[:directory], "/mutations/", class_path, "#{file_name}.rb")
38
20
 
39
21
  sentinel = /class .*MutationType\s*<\s*[^\s]+?\n/m
40
22
  in_root do
41
- gsub_file "#{options[:directory]}/types/mutation_type.rb", / \# TODO\: Add Mutations as fields\s*\n/m, ""
42
- inject_into_file "#{options[:directory]}/types/mutation_type.rb", " field :#{field_name}, mutation: Mutations::#{mutation_name}\n", after: sentinel, verbose: false, force: false
23
+ path = "#{options[:directory]}/types/mutation_type.rb"
24
+ invoke "graphql:install:mutation_root" unless File.exist?(path)
25
+ inject_into_file "#{options[:directory]}/types/mutation_type.rb", " field :#{file_name}, mutation: Mutations::#{class_name}\n", after: sentinel, verbose: false, force: false
43
26
  end
44
27
  end
45
-
46
- private
47
-
48
- def assign_names!(name)
49
- @field_name = name.camelize.underscore
50
- @mutation_name = name.camelize(:upper)
51
- @file_name = name.camelize.underscore
52
- end
53
28
  end
54
29
  end
55
30
  end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+ require_relative 'orm_mutations_base'
3
+
4
+ module Graphql
5
+ module Generators
6
+ # TODO: What other options should be supported?
7
+ #
8
+ # @example Generate a `GraphQL::Schema::RelayClassicMutation` by name
9
+ # rails g graphql:mutation CreatePostMutation
10
+ class MutationUpdateGenerator < OrmMutationsBase
11
+
12
+ desc "Scaffold a Relay Classic ORM update mutation for the given model class"
13
+ source_root File.expand_path('../templates', __FILE__)
14
+
15
+ private
16
+
17
+ def operation_type
18
+ "update"
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  require 'generators/graphql/type_generator'
3
+ require 'generators/graphql/field_extractor'
3
4
 
4
5
  module Graphql
5
6
  module Generators
@@ -15,31 +16,16 @@ module Graphql
15
16
  desc "Create a GraphQL::ObjectType with the given name and fields." \
16
17
  "If the given type name matches an existing ActiveRecord model, the generated type will automatically include fields for the models database columns."
17
18
  source_root File.expand_path('../templates', __FILE__)
18
-
19
- argument :custom_fields,
20
- type: :array,
21
- default: [],
22
- banner: "name:type name:type ...",
23
- desc: "Fields for this object (type may be expressed as Ruby or GraphQL)"
19
+ include FieldExtractor
24
20
 
25
21
  class_option :node,
26
22
  type: :boolean,
27
23
  default: false,
28
24
  desc: "Include the Relay Node interface"
29
25
 
30
- def create_type_file
31
- template "object.erb", "#{options[:directory]}/types/#{type_file_name}.rb"
32
- end
33
-
34
- def fields
35
- columns = []
36
- columns += klass.columns.map { |c| generate_column_string(c) } if class_exists?
37
- columns + custom_fields
38
- end
39
-
40
26
  def self.normalize_type_expression(type_expression, mode:, null: true)
41
- case type_expression
42
- when "Text"
27
+ case type_expression.camelize
28
+ when "Text", "Citext"
43
29
  ["String", null]
44
30
  when "Decimal"
45
31
  ["Float", null]
@@ -47,6 +33,8 @@ module Graphql
47
33
  ["GraphQL::Types::ISO8601DateTime", null]
48
34
  when "Date"
49
35
  ["GraphQL::Types::ISO8601Date", null]
36
+ when "Json", "Jsonb", "Hstore"
37
+ ["GraphQL::Types::JSON", null]
50
38
  else
51
39
  super
52
40
  end
@@ -54,25 +42,8 @@ module Graphql
54
42
 
55
43
  private
56
44
 
57
- def generate_column_string(column)
58
- name = column.name
59
- required = column.null ? "" : "!"
60
- type = column_type_string(column)
61
- "#{name}:#{required}#{type}"
62
- end
63
-
64
- def column_type_string(column)
65
- column.name == "id" ? "ID" : column.type.to_s.camelize
66
- end
67
-
68
- def class_exists?
69
- klass.is_a?(Class) && klass.ancestors.include?(ActiveRecord::Base)
70
- rescue NameError
71
- return false
72
- end
73
-
74
- def klass
75
- @klass ||= Module.const_get(type_name.camelize)
45
+ def graphql_type
46
+ "object"
76
47
  end
77
48
  end
78
49
  end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+ require 'rails/generators'
3
+ require 'rails/generators/named_base'
4
+ require_relative 'core'
5
+
6
+ module Graphql
7
+ module Generators
8
+ # TODO: What other options should be supported?
9
+ #
10
+ # @example Generate a `GraphQL::Schema::RelayClassicMutation` by name
11
+ # rails g graphql:mutation CreatePostMutation
12
+ class OrmMutationsBase < Rails::Generators::NamedBase
13
+ include Core
14
+ include Rails::Generators::ResourceHelpers
15
+
16
+ desc "Create a Relay Classic mutation by name"
17
+
18
+ class_option :orm, banner: "NAME", type: :string, required: true,
19
+ desc: "ORM to generate the controller for"
20
+
21
+ class_option 'namespaced_types',
22
+ type: :boolean,
23
+ required: false,
24
+ default: false,
25
+ banner: "Namespaced",
26
+ desc: "If the generated types will be namespaced"
27
+
28
+ def create_mutation_file
29
+ template "mutation_#{operation_type}.erb", File.join(options[:directory], "/mutations/", class_path, "#{file_name}_#{operation_type}.rb")
30
+
31
+ sentinel = /class .*MutationType\s*<\s*[^\s]+?\n/m
32
+ in_root do
33
+ path = "#{options[:directory]}/types/mutation_type.rb"
34
+ invoke "graphql:install:mutation_root" unless File.exist?(path)
35
+ inject_into_file "#{options[:directory]}/types/mutation_type.rb", " field :#{file_name}_#{operation_type}, mutation: Mutations::#{class_name}#{operation_type.classify}\n", after: sentinel, verbose: false, force: false
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -12,8 +12,10 @@ module Graphql
12
12
  desc "Create a GraphQL::ScalarType with the given name"
13
13
  source_root File.expand_path('../templates', __FILE__)
14
14
 
15
- def create_type_file
16
- template "scalar.erb", "#{options[:directory]}/types/#{type_file_name}.rb"
15
+ private
16
+
17
+ def graphql_type
18
+ "scalar"
17
19
  end
18
20
  end
19
21
  end
@@ -1,6 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  <% module_namespacing_when_supported do -%>
2
4
  module Types
3
- class <%= type_ruby_name.split('::')[-1] %> < Types::BaseEnum
5
+ class <%= ruby_class_name %> < Types::BaseEnum
6
+ description "<%= human_name %> enum"
7
+
4
8
  <% prepared_values.each do |v| %> value "<%= v[0] %>"<%= v.length > 1 ? ", value: #{v[1]}" : "" %>
5
9
  <% end %> end
6
10
  end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ <% module_namespacing_when_supported do -%>
4
+ module Types
5
+ class <%= ruby_class_name %> < Types::BaseInputObject
6
+ <% normalized_fields.each do |f| %> <%= f.to_input_argument %>
7
+ <% end %> end
8
+ end
9
+ <% end -%>
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  <% module_namespacing_when_supported do -%>
2
4
  module Types
3
- module <%= type_ruby_name.split('::')[-1] %>
5
+ module <%= ruby_class_name %>
4
6
  include Types::BaseInterface
5
- <% normalized_fields.each do |f| %> <%= f.to_ruby %>
7
+ <% normalized_fields.each do |f| %> <%= f.to_object_field %>
6
8
  <% end %> end
7
9
  end
8
10
  <% end -%>
@@ -1,6 +1,6 @@
1
1
  <% module_namespacing_when_supported do -%>
2
2
  module Mutations
3
- class <%= mutation_name %> < BaseMutation
3
+ class <%= class_name %> < BaseMutation
4
4
  # TODO: define return fields
5
5
  # field :post, Types::PostType, null: false
6
6
 
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ <% module_namespacing_when_supported do -%>
4
+ module Mutations
5
+ class <%= class_name %>Create < BaseMutation
6
+ description "Creates a new <%= file_name %>"
7
+
8
+ field :<%= file_name %>, Types::<%= options[:namespaced_types] ? 'Objects::' : '' %><%= class_name %>Type, null: false
9
+
10
+ argument :<%= file_name %>_input, Types::<%= options[:namespaced_types] ? 'Inputs::' : '' %><%= class_name %>InputType, required: true
11
+
12
+ def resolve(<%= file_name %>_input:)
13
+ <%= singular_table_name %> = ::<%= orm_class.build(class_name, "**#{file_name}_input") %>
14
+ raise GraphQL::ExecutionError.new "Error creating <%= file_name %>", extensions: <%= singular_table_name %>.errors.to_hash unless <%= orm_instance.save %>
15
+
16
+ { <%= file_name %>: <%= singular_table_name %> }
17
+ end
18
+ end
19
+ end
20
+ <% end -%>
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ <% module_namespacing_when_supported do -%>
4
+ module Mutations
5
+ class <%= class_name %>Delete < BaseMutation
6
+ description "Deletes a <%= file_name %> by ID"
7
+
8
+ field :<%= file_name %>, Types::<%= options[:namespaced_types] ? 'Objects::' : '' %><%= class_name %>Type, null: false
9
+
10
+ argument :id, ID, required: true
11
+
12
+ def resolve(id:)
13
+ <%= singular_table_name %> = ::<%= orm_class.find(class_name, "id") %>
14
+ raise GraphQL::ExecutionError.new "Error deleting <%= file_name %>", extensions: <%= singular_table_name %>.errors.to_hash unless <%= orm_instance.destroy %>
15
+
16
+ { <%= file_name %>: <%= singular_table_name %> }
17
+ end
18
+ end
19
+ end
20
+ <% end -%>
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ <% module_namespacing_when_supported do -%>
4
+ module Mutations
5
+ class <%= class_name %>Update < BaseMutation
6
+ description "Updates a <%= file_name %> by id"
7
+
8
+ field :<%= file_name %>, Types::<%= options[:namespaced_types] ? 'Objects::' : '' %><%= class_name %>Type, null: false
9
+
10
+ argument :id, ID, required: true
11
+ argument :<%= file_name %>_input, Types::<%= options[:namespaced_types] ? 'Inputs::' : '' %><%= class_name %>InputType, required: true
12
+
13
+ def resolve(id:, <%= file_name %>_input:)
14
+ <%= singular_table_name %> = ::<%= orm_class.find(class_name, "id") %>
15
+ raise GraphQL::ExecutionError.new "Error updating <%= file_name %>", extensions: <%= singular_table_name %>.errors.to_hash unless <%= orm_instance.update("**#{file_name}_input") %>
16
+
17
+ { <%= file_name %>: <%= singular_table_name %> }
18
+ end
19
+ end
20
+ end
21
+ <% end -%>
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  <% module_namespacing_when_supported do -%>
2
4
  module Types
3
- class <%= type_ruby_name.split('::')[-1] %> < Types::BaseObject
5
+ class <%= ruby_class_name %> < Types::BaseObject
4
6
  <% if options.node %> implements GraphQL::Types::Relay::Node
5
- <% end %><% normalized_fields.each do |f| %> <%= f.to_ruby %>
7
+ <% end %><% normalized_fields.each do |f| %> <%= f.to_object_field %>
6
8
  <% end %> end
7
9
  end
8
10
  <% end -%>
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  <% module_namespacing_when_supported do -%>
2
4
  module Types
3
- class <%= type_ruby_name.split('::')[-1] %> < Types::BaseScalar
5
+ class <%= ruby_class_name %> < Types::BaseScalar
4
6
  def self.coerce_input(input_value, context)
5
7
  # Override this to prepare a client-provided GraphQL value for your Ruby code
6
8
  input_value
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  <% module_namespacing_when_supported do -%>
2
4
  module Types
3
- class <%= type_ruby_name.split('::')[-1] %> < Types::BaseUnion
4
- <% if possible_types.any? %> possible_types <%= normalized_possible_types.join(", ") %>
5
+ class <%= ruby_class_name %> < Types::BaseUnion
6
+ <% if custom_fields.any? %> possible_types <%= normalized_possible_types.join(", ") %>
5
7
  <% end %> end
6
8
  end
7
9
  <% end -%>