graphiti 2.0.0.beta.3 → 2.0.0.beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (274) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +30 -86
  3. data/.github/workflows/docs.yml +57 -0
  4. data/.github/workflows/release.yml +8 -8
  5. data/.gitignore +7 -0
  6. data/.npmrc +9 -0
  7. data/.standard.yml +4 -1
  8. data/Appraisals +33 -32
  9. data/CHANGELOG.md +48 -0
  10. data/README.md +13 -2
  11. data/UPGRADING.md +2 -58
  12. data/docs/concepts/backends-and-models.md +122 -0
  13. data/docs/concepts/endpoints.md +183 -0
  14. data/docs/concepts/links.md +212 -0
  15. data/docs/concepts/overview.md +80 -0
  16. data/docs/concepts/persisting.md +376 -0
  17. data/docs/concepts/relationships.md +527 -0
  18. data/docs/concepts/resources.md +677 -0
  19. data/docs/getting-started/first-api.md +289 -0
  20. data/docs/getting-started/installation.md +185 -0
  21. data/docs/intro.md +307 -0
  22. data/docs/js/authentication.md +63 -0
  23. data/docs/js/ddau.md +20 -0
  24. data/docs/js/extra-params.md +41 -0
  25. data/docs/js/index.md +112 -0
  26. data/docs/js/installation.md +120 -0
  27. data/docs/js/middleware.md +72 -0
  28. data/docs/js/models.md +202 -0
  29. data/docs/js/reads.md +494 -0
  30. data/docs/js/state-syncing.md +100 -0
  31. data/docs/js/writes.md +373 -0
  32. data/docs/reference/vandal.md +63 -0
  33. data/docs/reference/why.md +13 -0
  34. data/docs/topics/authorization.md +155 -0
  35. data/docs/topics/caching.md +55 -0
  36. data/docs/topics/customizing-sideloads.md +156 -0
  37. data/docs/topics/debugging.md +216 -0
  38. data/docs/topics/error-handling.md +210 -0
  39. data/docs/topics/etags.md +46 -0
  40. data/docs/topics/hopping-relationships.md +149 -0
  41. data/docs/topics/json-attributes.md +77 -0
  42. data/docs/topics/openstruct-models.md +50 -0
  43. data/docs/topics/remote-resources.md +291 -0
  44. data/docs/topics/testing.md +894 -0
  45. data/docs/topics/without-activerecord.md +324 -0
  46. data/docs/tutorial/index.md +58 -0
  47. data/docs/tutorial/step_0.md +107 -0
  48. data/docs/tutorial/step_1.md +199 -0
  49. data/docs/tutorial/step_2.md +312 -0
  50. data/docs/tutorial/step_3.md +142 -0
  51. data/docs/tutorial/step_4.md +135 -0
  52. data/docs/tutorial/step_5.md +69 -0
  53. data/docs/tutorial/step_6.md +82 -0
  54. data/docs/tutorial/step_7.md +205 -0
  55. data/docs/tutorial/step_8.md +128 -0
  56. data/docs/tutorial/step_9.md +171 -0
  57. data/docs/upgrading.md +260 -0
  58. data/gemfiles/rails_7_1.gemfile +4 -3
  59. data/gemfiles/{rails_7_2_graphiti_rails.gemfile → rails_7_2.gemfile} +3 -3
  60. data/gemfiles/{rails_8_1_graphiti_rails.gemfile → rails_8_0.gemfile} +3 -3
  61. data/gemfiles/{rails_8_0_graphiti_rails.gemfile → rails_8_1.gemfile} +3 -3
  62. data/graphiti.gemspec +7 -23
  63. data/{deprecated_generators → lib/generators}/graphiti/api_test_generator.rb +7 -1
  64. data/{deprecated_generators → lib/generators}/graphiti/generator_mixin.rb +14 -1
  65. data/{deprecated_generators → lib/generators}/graphiti/install_generator.rb +19 -13
  66. data/{deprecated_generators → lib/generators}/graphiti/resource_generator.rb +68 -7
  67. data/{deprecated_generators → lib/generators}/graphiti/templates/controller.rb.erb +1 -1
  68. data/{deprecated_generators → lib/generators}/graphiti/templates/index_request_spec.rb.erb +1 -1
  69. data/{deprecated_generators → lib/generators}/graphiti/templates/resource_reads_spec.rb.erb +6 -6
  70. data/{deprecated_generators → lib/generators}/graphiti/templates/show_request_spec.rb.erb +1 -1
  71. data/lib/graphiti/configuration.rb +2 -2
  72. data/lib/graphiti/error_serializers/conflict_request.rb +19 -0
  73. data/lib/graphiti/error_serializers/deprecated_constants.rb +48 -0
  74. data/lib/graphiti/error_serializers/invalid_request.rb +56 -0
  75. data/lib/graphiti/error_serializers/validation.rb +143 -0
  76. data/lib/graphiti/errors.rb +4 -4
  77. data/lib/graphiti/query.rb +1 -1
  78. data/lib/graphiti/rails/context.rb +33 -0
  79. data/lib/graphiti/rails/controller.rb +41 -0
  80. data/lib/graphiti/rails/debugging.rb +18 -0
  81. data/lib/graphiti/rails/exception_handlers.rb +77 -0
  82. data/lib/graphiti/rails/railtie.rb +139 -0
  83. data/lib/graphiti/rails/rake_helpers.rb +42 -0
  84. data/lib/graphiti/rails/responders.rb +21 -0
  85. data/lib/graphiti/rails/test_helpers.rb +22 -0
  86. data/lib/graphiti/rails.rb +47 -29
  87. data/lib/graphiti/resource/configuration.rb +1 -0
  88. data/lib/graphiti/resource/interface.rb +2 -2
  89. data/lib/graphiti/resource/remote.rb +2 -2
  90. data/lib/graphiti/resource/sideloading.rb +1 -1
  91. data/lib/graphiti/resource.rb +13 -1
  92. data/lib/graphiti/responders.rb +7 -20
  93. data/lib/graphiti/schema.rb +5 -1
  94. data/lib/graphiti/schema_diff.rb +4 -0
  95. data/lib/graphiti/scope.rb +45 -37
  96. data/lib/graphiti/serializer.rb +6 -0
  97. data/lib/graphiti/sideload/belongs_to.rb +38 -5
  98. data/lib/graphiti/sideload/polymorphic_belongs_to.rb +27 -23
  99. data/lib/graphiti/sideload.rb +54 -35
  100. data/lib/graphiti/spec_helpers/errors.rb +73 -0
  101. data/lib/graphiti/spec_helpers/errors_proxy.rb +75 -0
  102. data/lib/graphiti/spec_helpers/helpers.rb +107 -0
  103. data/lib/graphiti/spec_helpers/matchers.rb +196 -0
  104. data/lib/graphiti/spec_helpers/node.rb +88 -0
  105. data/lib/graphiti/spec_helpers/rspec.rb +148 -0
  106. data/lib/graphiti/spec_helpers.rb +54 -0
  107. data/lib/graphiti/util/include_params.rb +2 -2
  108. data/lib/graphiti/util/serializer_relationships.rb +41 -5
  109. data/lib/graphiti/version.rb +1 -1
  110. data/lib/graphiti-rails.rb +11 -0
  111. data/lib/graphiti.rb +34 -10
  112. data/lib/graphiti_errors.rb +11 -0
  113. data/lib/graphiti_spec_helpers/rspec.rb +3 -0
  114. data/lib/graphiti_spec_helpers.rb +11 -0
  115. data/lib/tasks/graphiti.rake +29 -0
  116. data/package-lock.json +6199 -0
  117. data/package.json +5 -4
  118. data/website/.gitignore +20 -0
  119. data/website/README.md +43 -0
  120. data/website/docusaurus.config.js +197 -0
  121. data/website/package-lock.json +19474 -0
  122. data/website/package.json +46 -0
  123. data/website/sidebars.js +82 -0
  124. data/website/src/css/custom.css +85 -0
  125. data/website/src/pages/markdown-page.mdx +7 -0
  126. data/website/static/.nojekyll +0 -0
  127. data/website/static/1.13/2019/03/31/graphiti-1-0.html +205 -0
  128. data/website/static/1.13/2019/05/08/graphiti-1-1.html +212 -0
  129. data/website/static/1.13/2019/05/20/graphiti-1-2.html +214 -0
  130. data/website/static/1.13/2019/10/14/tutorial.html +198 -0
  131. data/website/static/1.13/CNAME +1 -0
  132. data/website/static/1.13/README.md +16 -0
  133. data/website/static/1.13/assets/css/syntax.css +60 -0
  134. data/website/static/1.13/assets/favicons/android-chrome-192x192.png +0 -0
  135. data/website/static/1.13/assets/favicons/android-chrome-256x256.png +0 -0
  136. data/website/static/1.13/assets/favicons/apple-touch-icon.png +0 -0
  137. data/website/static/1.13/assets/favicons/browserconfig.xml +9 -0
  138. data/website/static/1.13/assets/favicons/favicon-16x16.png +0 -0
  139. data/website/static/1.13/assets/favicons/favicon-32x32.png +0 -0
  140. data/website/static/1.13/assets/favicons/favicon.ico +0 -0
  141. data/website/static/1.13/assets/favicons/mstile-150x150.png +0 -0
  142. data/website/static/1.13/assets/favicons/safari-pinned-tab.svg +1 -0
  143. data/website/static/1.13/assets/favicons/site.webmanifest +19 -0
  144. data/website/static/1.13/assets/img/backend.gif +0 -0
  145. data/website/static/1.13/assets/img/conformity.png +0 -0
  146. data/website/static/1.13/assets/img/error_payload.png +0 -0
  147. data/website/static/1.13/assets/img/fancy-cushion.png +0 -0
  148. data/website/static/1.13/assets/img/gh.png +0 -0
  149. data/website/static/1.13/assets/img/home-bg.jpg +0 -0
  150. data/website/static/1.13/assets/img/lifecycle.gif +0 -0
  151. data/website/static/1.13/assets/img/logo-500.png +0 -0
  152. data/website/static/1.13/assets/img/logo.png +0 -0
  153. data/website/static/1.13/assets/img/love-graffiti.jpg +0 -0
  154. data/website/static/1.13/assets/img/meta_total_count.png +0 -0
  155. data/website/static/1.13/assets/img/persist.jpg +0 -0
  156. data/website/static/1.13/assets/img/resource.gif +0 -0
  157. data/website/static/1.13/assets/img/rest-graffiti.jpg +0 -0
  158. data/website/static/1.13/assets/img/rest1.gif +0 -0
  159. data/website/static/1.13/assets/img/rest2.gif +0 -0
  160. data/website/static/1.13/assets/img/rest3.gif +0 -0
  161. data/website/static/1.13/assets/img/rethink-rest-graffiti.jpg +0 -0
  162. data/website/static/1.13/assets/img/sunrise.png +0 -0
  163. data/website/static/1.13/assets/img/why.png +0 -0
  164. data/website/static/1.13/assets/js/highlight.pack.js +2 -0
  165. data/website/static/1.13/assets/main.css +15518 -0
  166. data/website/static/1.13/assets/main.css.map +1 -0
  167. data/website/static/1.13/bin/bundle +109 -0
  168. data/website/static/1.13/bin/jekyll +27 -0
  169. data/website/static/1.13/bin/kramdown +27 -0
  170. data/website/static/1.13/bin/listen +27 -0
  171. data/website/static/1.13/bin/rake +27 -0
  172. data/website/static/1.13/bin/rougify +27 -0
  173. data/website/static/1.13/bin/safe_yaml +27 -0
  174. data/website/static/1.13/bin/sass +27 -0
  175. data/website/static/1.13/bin/sass-convert +27 -0
  176. data/website/static/1.13/bin/scss +27 -0
  177. data/website/static/1.13/blog.html +259 -0
  178. data/website/static/1.13/cheatsheet.html +316 -0
  179. data/website/static/1.13/cookbooks/authorization.md +0 -0
  180. data/website/static/1.13/cookbooks/caching.md +0 -0
  181. data/website/static/1.13/cookbooks/customizing-sideloads.html +325 -0
  182. data/website/static/1.13/cookbooks/etags.md +0 -0
  183. data/website/static/1.13/cookbooks/hopping-relationships.html +324 -0
  184. data/website/static/1.13/cookbooks/json_attributes.md +0 -0
  185. data/website/static/1.13/cookbooks/openstruct-models.md +0 -0
  186. data/website/static/1.13/cookbooks/remote-resources.md +0 -0
  187. data/website/static/1.13/cookbooks/without-activerecord.html +510 -0
  188. data/website/static/1.13/features.html +249 -0
  189. data/website/static/1.13/feed.xml +106 -0
  190. data/website/static/1.13/guides/concepts/backends-and-models.html +467 -0
  191. data/website/static/1.13/guides/concepts/debugging.html +440 -0
  192. data/website/static/1.13/guides/concepts/endpoints.html +432 -0
  193. data/website/static/1.13/guides/concepts/error-handling.html +396 -0
  194. data/website/static/1.13/guides/concepts/links.html +501 -0
  195. data/website/static/1.13/guides/concepts/remote-resources.html +536 -0
  196. data/website/static/1.13/guides/concepts/resources.html +2176 -0
  197. data/website/static/1.13/guides/concepts/testing.html +1469 -0
  198. data/website/static/1.13/guides/getting-started/installation.html +420 -0
  199. data/website/static/1.13/guides/graphiti-rails-migration.html +242 -0
  200. data/website/static/1.13/guides/index.html +269 -0
  201. data/website/static/1.13/guides/overview.html +325 -0
  202. data/website/static/1.13/guides/upgrading-2-0.html +193 -0
  203. data/website/static/1.13/guides/upgrading.html +314 -0
  204. data/website/static/1.13/guides/vandal.html +282 -0
  205. data/website/static/1.13/guides/why.html +1121 -0
  206. data/website/static/1.13/index.html +72 -0
  207. data/website/static/1.13/js/authentication.html +295 -0
  208. data/website/static/1.13/js/ddau.html +238 -0
  209. data/website/static/1.13/js/extra-params.html +270 -0
  210. data/website/static/1.13/js/index.html +321 -0
  211. data/website/static/1.13/js/installation.html +637 -0
  212. data/website/static/1.13/js/introduction.html +257 -0
  213. data/website/static/1.13/js/middleware.html +318 -0
  214. data/website/static/1.13/js/reads/fieldsets.html +271 -0
  215. data/website/static/1.13/js/reads/filtering.html +289 -0
  216. data/website/static/1.13/js/reads/includes.html +260 -0
  217. data/website/static/1.13/js/reads/index.html +497 -0
  218. data/website/static/1.13/js/reads/nested-queries.html +353 -0
  219. data/website/static/1.13/js/reads/pagination.html +260 -0
  220. data/website/static/1.13/js/reads/sorting.html +265 -0
  221. data/website/static/1.13/js/reads/statistics.html +289 -0
  222. data/website/static/1.13/js/state-syncing.html +340 -0
  223. data/website/static/1.13/js/writes/deferred.html +296 -0
  224. data/website/static/1.13/js/writes/dirty-tracking.html +399 -0
  225. data/website/static/1.13/js/writes/index.html +391 -0
  226. data/website/static/1.13/js/writes/nested.html +330 -0
  227. data/website/static/1.13/js/writes/validations.html +272 -0
  228. data/website/static/1.13/quickstart.html +660 -0
  229. data/website/static/1.13/template +161 -0
  230. data/website/static/1.13/tutorial/index.html +250 -0
  231. data/website/static/1.13/tutorial/step_0.html +292 -0
  232. data/website/static/1.13/tutorial/step_1.html +517 -0
  233. data/website/static/1.13/tutorial/step_2.html +481 -0
  234. data/website/static/1.13/tutorial/step_3.html +323 -0
  235. data/website/static/1.13/tutorial/step_4.html +318 -0
  236. data/website/static/1.13/tutorial/step_5.html +265 -0
  237. data/website/static/1.13/tutorial/step_6.html +276 -0
  238. data/website/static/1.13/tutorial/step_7.html +390 -0
  239. data/website/static/1.13/tutorial/step_8.html +316 -0
  240. data/website/static/1.13/tutorial/step_9.html +365 -0
  241. data/website/static/CNAME +1 -0
  242. data/website/static/assets/img/error_payload.png +0 -0
  243. data/website/static/assets/img/legacy/legacy-0378a3bb39.png +0 -0
  244. data/website/static/assets/img/legacy/legacy-05bbd3e5fd.png +0 -0
  245. data/website/static/assets/img/legacy/legacy-07aa104495.png +0 -0
  246. data/website/static/assets/img/legacy/legacy-0c75a16b3a.gif +0 -0
  247. data/website/static/assets/img/legacy/legacy-3076df6209.png +0 -0
  248. data/website/static/assets/img/legacy/legacy-7f6889bc89.png +0 -0
  249. data/website/static/assets/img/legacy/legacy-a2cc4363c3.png +0 -0
  250. data/website/static/assets/img/legacy/legacy-f67cfa89ab.png +0 -0
  251. data/website/static/assets/img/meta_total_count.png +0 -0
  252. data/website/static/img/docusaurus-social-card.jpg +0 -0
  253. data/website/static/img/docusaurus.png +0 -0
  254. data/website/static/img/favicon.ico +0 -0
  255. data/website/static/img/logo.png +0 -0
  256. data/website/static/img/logo.svg +1 -0
  257. data/website/static/img/undraw_docusaurus_mountain.svg +171 -0
  258. data/website/static/img/undraw_docusaurus_react.svg +170 -0
  259. data/website/static/img/undraw_docusaurus_tree.svg +40 -0
  260. metadata +251 -54
  261. data/gemfiles/rails_6.gemfile +0 -18
  262. data/gemfiles/rails_6_graphiti_rails.gemfile +0 -19
  263. data/gemfiles/rails_7.gemfile +0 -18
  264. data/gemfiles/rails_7_1_graphiti_rails.gemfile +0 -19
  265. data/gemfiles/rails_7_graphiti_rails.gemfile +0 -19
  266. data/lib/graphiti/deprecated_tasks.rb +0 -49
  267. data/lib/graphiti/railtie.rb +0 -121
  268. /data/{deprecated_generators → lib/generators}/graphiti/resource_test_generator.rb +0 -0
  269. /data/{deprecated_generators → lib/generators}/graphiti/templates/application_resource.rb.erb +0 -0
  270. /data/{deprecated_generators → lib/generators}/graphiti/templates/create_request_spec.rb.erb +0 -0
  271. /data/{deprecated_generators → lib/generators}/graphiti/templates/destroy_request_spec.rb.erb +0 -0
  272. /data/{deprecated_generators → lib/generators}/graphiti/templates/resource.rb.erb +0 -0
  273. /data/{deprecated_generators → lib/generators}/graphiti/templates/resource_writes_spec.rb.erb +0 -0
  274. /data/{deprecated_generators → lib/generators}/graphiti/templates/update_request_spec.rb.erb +0 -0
data/docs/upgrading.md ADDED
@@ -0,0 +1,260 @@
1
+ ---
2
+ title: 'Upgrading to Graphiti 2.0'
3
+ slug: /upgrading
4
+ ---
5
+
6
+ # Upgrading to Graphiti 2.0
7
+
8
+ Graphiti 2.0 requires **Ruby 3.2+** and **ActiveSupport 7.1+**. Rails is not a dependency, but if you use it, 7.1+. Ruby 3.1 and earlier are past end of life, and Rails 6.1 and 7.0 do not support Ruby 3.2. Apps that cannot move yet should stay on the 1.x branch, which remains open for hotfixes.
9
+
10
+ ## What you have to change {#what-you-have-to-change}
11
+
12
+ Four things, and three of them fail loudly if you skip them.
13
+
14
+ **1. Drop three gems.** `graphiti-rails`, `graphiti_spec_helpers` and `graphiti_errors` are now part of `graphiti` itself.
15
+
16
+ ```diff title="Gemfile"
17
+ + gem "graphiti", "~> 2.0.0.beta" # follows the betas and picks up 2.0 final when it ships
18
+ - gem "graphiti-rails"
19
+ - gem "graphiti_spec_helpers"
20
+ - gem "graphiti_errors"
21
+ ```
22
+
23
+ Graphiti raises at load if one is still installed, because they ship files that collide with Graphiti's own, so leaving them in place means load order decides which copy you get.
24
+
25
+ **2. Include the Rails integration in your controllers.**
26
+
27
+ ```ruby
28
+ class ApplicationController < ActionController::Base
29
+ include Graphiti::Rails::Controller
30
+ end
31
+ ```
32
+
33
+ If the controller already has `include Graphiti::Rails`, replace it with `include Graphiti::Rails::Controller`.
34
+
35
+ <details>
36
+ <summary>What the include actually brings, and what a controller without it loses</summary>
37
+
38
+ Until 2.0, Graphiti added itself to **every** controller in the application: an `around_action` wrapping each request in a Graphiti context, another wrapping it in the debugger, and a catch-all exception handler, on Devise controllers, admin controllers, HTML pages, everything.
39
+
40
+ `Graphiti::Rails::Controller` now bundles all of it, and including it is required. Including it in `ApplicationController` matches 1.x behavior. Including it in an API base class scopes it and leaves the rest of the app alone. A controller without it gets no Graphiti context, no debugger, and none of Graphiti's exception handlers, so if a resource action sees an empty `Graphiti.context`, this include is what is missing.
41
+
42
+ The class-level DSL travels with it, which is the one failure you see before a request is ever served:
43
+
44
+ ```ruby
45
+ class PostsController < ApplicationController
46
+ self.sideload_allowlist = {index: [:comments]} # NoMethodError without the include
47
+ end
48
+ ```
49
+
50
+ `sideload_allowlist` comes from `Graphiti::Context`, so a controller that never includes `Graphiti::Rails::Controller` raises `NoMethodError` while the class body is being loaded. Watch for base classes that were given `Graphiti::Rails::Responders` alone. Responders declares formats and nothing else, and does not carry the context.
51
+
52
+ `Graphiti::Rails::Responders` is separate and most apps do not need it. It exists for the [`responders`](https://github.com/heartcombo/responders) gem's `respond_with`, and depends on that gem, which is why it is not part of `Graphiti::Rails::Controller`.
53
+
54
+ </details>
55
+
56
+ **3. Update `around_persistence` hooks**, if you have any.
57
+
58
+ They now receive the already-assigned model where they used to receive the attributes hash, so a hook doing `attributes[:tenant_id] = current_tenant.id` raises. Move that to `before_attributes`, or set it on the model.
59
+
60
+ <details>
61
+ <summary>Before and after, and what else moved with it</summary>
62
+
63
+ Attributes are now assigned to the model once, up front, before the persistence hooks run, which is what lets `build` and `find` hand you the model before anything is written. See the [lifecycle hooks guide](/concepts/persisting#persistence-lifecycle-hooks) for what that enables.
64
+
65
+ That changes one hook.
66
+
67
+ #### around_persistence receives the model, not the attributes hash
68
+
69
+ It now wraps the save of an already-assigned model, and gets that model:
70
+
71
+ ```ruby
72
+ # 1.x
73
+ def do_around_persistence(attributes)
74
+ attributes[:tenant_id] = current_tenant.id
75
+ model = yield
76
+ model.log_saved!
77
+ end
78
+
79
+ # 2.0
80
+ def do_around_persistence(model)
81
+ model.tenant_id = current_tenant.id # last chance to touch the model before save, inside the transaction
82
+ saved = yield
83
+ saved.log_saved!
84
+ end
85
+ ```
86
+
87
+ To migrate, move attribute-hash modifications to `before_attributes` (which still receives the mutable hash, before assignment), or set the value on the model as above. Hooks that only wrap their yield, such as transactions, timing and post-save side effects, need no changes. Graphiti 1.x releases warn at runtime when a hook would be affected.
88
+
89
+ `before/around/after_attributes` and `before/around/after_save` are unchanged. Custom `create`/`update` adapter overrides keep their 1.x signatures.
90
+
91
+ #### Fine print
92
+
93
+ - If you inspect the model before saving, the attributes callbacks run at inspection time (in your controller, outside the save transaction). On the plain `save` path they run inside the transaction, at the same point as 1.x.
94
+ - Writable guards judge persisted state: a guard asking for the model gets a fresh build/find, never the current request's unsaved changes. A payload cannot influence its own authorization.
95
+ - Sideposted child models are still built and assigned during save, and `data` exposes the pre-assigned root model only.
96
+
97
+ </details>
98
+
99
+ **4. Wrap specs that assert on error payloads.**
100
+
101
+ ```ruby
102
+ RSpec.configure do |config|
103
+ config.include Graphiti::Rails::TestHelpers, type: :request
104
+ end
105
+
106
+ it "renders a 404" do
107
+ handle_request_exceptions { get "/posts/999" }
108
+
109
+ expect(response.status).to eq(404)
110
+ end
111
+ ```
112
+
113
+ Exceptions now propagate untouched in tests rather than rendering, so a spec expecting a 404 body sees the exception raised instead. This is the one that breaks the suite that would otherwise have told you the app was fine.
114
+
115
+ <details>
116
+ <summary>Why it has to be a request spec</summary>
117
+
118
+ Exceptions propagate untouched in tests as of 2.0. A spec that asserts on a rendered error payload sees the exception raised instead, so wrap the request in `handle_request_exceptions`. The setup is [step 4 of the migration](/upgrading#what-you-have-to-change).
119
+
120
+ It has to be a request spec. Exceptions are rendered in Rack middleware, which controller specs bypass, so the same assertion in a controller spec never sees a rendered payload no matter how it is wrapped.
121
+
122
+ `handle_request_exceptions` replaces `GraphitiErrors.enable!` and `.disable!`, which toggled rendering globally. Wrapping the request scopes it to the example instead.
123
+
124
+ </details>
125
+
126
+ ## Behavior changes to be aware of {#behavior-changes}
127
+
128
+ Nothing to do here. These change what a client receives or when a callback runs, and none of them warns you, because none of them is a rename.
129
+
130
+ <details>
131
+ <summary>A `belongs_to` renders resource linkage in every payload, where 1.x sent only a link</summary>
132
+
133
+ A `belongs_to` now includes resource linkage in the payload by default, where 1.x sent only a link:
134
+
135
+ ```json
136
+ "employee": { "data": { "type": "employees", "id": "1" }, "links": { "related": "..." } }
137
+ ```
138
+
139
+ The id comes from the foreign key already on the parent, so this costs no extra queries, and clients can resolve the relationship against data they already hold instead of following the link. `has_many` is unchanged, since answering there means a query per record.
140
+
141
+ Not every `belongs_to` qualifies. A `scope` or `params` block, a `base_scope`, a polymorphic or remote target or a custom `primary_key` all mean the foreign key is not the related id, and those keep loading the association, so they stay opt-in as in 1.x.
142
+
143
+ Relationships are marked `linkage: true` in `schema.json`, and the schema check reports a relationship that stops including it.
144
+
145
+ To go back to the old payload for one relationship:
146
+
147
+ ```ruby
148
+ belongs_to :employee, always_include_resource_ids: false
149
+ ```
150
+
151
+ Or for the whole API, on the resource everything inherits from. The same setting with `true` is the 2.0 replacement for the `Sideload::BelongsTo` monkey patch that [#167](https://github.com/graphiti-api/graphiti/issues/167) has been recommending, and it now covers every relationship type rather than only `belongs_to`:
152
+
153
+ ```ruby
154
+ class ApplicationResource < Graphiti::Resource
155
+ self.abstract_class = true
156
+
157
+ self.always_include_resource_ids_by_default = false
158
+ end
159
+ ```
160
+
161
+ How linkage is configured, and when a `belongs_to` cannot use its foreign key, is covered in [Customizing Relationships](/concepts/relationships#customizing-relationships).
162
+
163
+ </details>
164
+
165
+ <details>
166
+ <summary>`ConflictRequest` renders `code: "conflict"` at 409, where `graphiti-rails` surfaced it as a 500</summary>
167
+
168
+ Graphiti 1.x shipped two exception systems, `graphiti_errors` in core and `rescue_registry` in `graphiti-rails`, and both loaded in every Rails app. `rescue_registry` is now the only one, and installs automatically as a dependency.
169
+
170
+ Graphiti registers handlers for `InvalidRequest` (400), `ConflictRequest` (409), `RecordNotFound` (404), `RemoteWrite` (400) and `SingularSideload` (400), plus a fallback that renders anything else as JSON:API. Register your own on any controller:
171
+
172
+ ```ruby
173
+ register_exception MyApp::Forbidden, status: 403
174
+ register_exception MyApp::Throttled, status: 429, handler: MyApp::ThrottleHandler
175
+ ```
176
+
177
+ `register_exception` comes from `rescue_registry`, which adds it to every controller, so you do not need `Graphiti::Rails::Controller` to register your own exceptions or to have them rendered. What the include adds is Graphiti's own registrations above, plus the fallback that renders anything unregistered as JSON:API.
178
+
179
+ Only formats in `config.graphiti.handled_exception_formats` (default `[:jsonapi]`) are rendered by Graphiti. Everything else falls through to Rails.
180
+
181
+ If you subclassed `GraphitiErrors::ExceptionHandler`, note the interface changed with the gem: it is now `build_payload` / `formatted_response` / `status_code`, not `error_payload` / `status_code(error)`.
182
+
183
+ Registering and customizing handlers is covered in [Error Handling](/topics/error-handling).
184
+
185
+ **Conflicts now report as conflicts.** `Graphiti::Errors::ConflictRequest`, raised when a `PATCH` payload's id does not match the URL, used to render a 409 whose body said `code: "bad_request"`, `title: "Request Error"`. It now says `code: "conflict"`, `title: "Conflict Error"`. Under `graphiti-rails` this exception had no registered handler at all and surfaced as a 500, so for most apps this payload is new rather than changed.
186
+
187
+ </details>
188
+
189
+ <details>
190
+ <summary>`Node#respond_to?` answers `true` for any attribute present in the payload</summary>
191
+
192
+ `Node#respond_to?` is now a proper `respond_to_missing?`, so `node.respond_to?(:first_name)` returns `true` for attributes present in the payload where it used to return `false`. Nothing to do unless a spec asserted on the old `false`.
193
+
194
+ The node helpers are covered in [#jsonapi_data](/topics/testing#jsonapi-data).
195
+
196
+ </details>
197
+
198
+ <details>
199
+ <summary>Attributes are assigned before the persistence hooks run, so inspecting a model first moves the attributes callbacks outside the save transaction</summary>
200
+
201
+ If you inspect the model before saving, the attributes callbacks run at inspection time, in your controller and outside the save transaction. On the plain `save` path they run inside the transaction, at the same point as 1.x.
202
+
203
+ The hooks and their order are covered in [Persistence Lifecycle Hooks](/concepts/persisting#persistence-lifecycle-hooks).
204
+
205
+ </details>
206
+
207
+ ## Deprecations you should fix {#deprecations-you-should-fix}
208
+
209
+ Every name below still works, warns, and will be removed in the next major. They're easy fixes though, so why not now?
210
+
211
+ | 1.x | 2.0 |
212
+ | --- | --- |
213
+ | `require "graphiti_spec_helpers/rspec"` | `require "graphiti/spec_helpers/rspec"` |
214
+ | `GraphitiSpecHelpers::RSpec` / `::Sugar` / `::Errors::*` | `Graphiti::SpecHelpers::*` |
215
+ | `require "graphiti-rails"` | remove / no longer needed |
216
+ | `include Graphiti::Rails` | `include Graphiti::Rails::Controller`|
217
+ | `include Graphiti::Responders` | `include Graphiti::Rails::Responders` |
218
+ | `jsonapi_context` | `graphiti_context` |
219
+ | `GraphitiErrors::Validation::Serializer` | `Graphiti::ErrorSerializers::Validation` |
220
+ | `GraphitiErrors::InvalidRequest::Serializer` | `Graphiti::ErrorSerializers::InvalidRequest` |
221
+ | `GraphitiErrors::ConflictRequest::Serializer` | `Graphiti::ErrorSerializers::ConflictRequest` |
222
+ | rspec shared contexts `"resource testing"`, `"remote api"` | `"graphiti resource testing"`, `"graphiti remote api"` |
223
+ | `GraphitiContextProxy` | `Graphiti::SpecHelpers::ContextProxy` |
224
+ | `context_namespace` | `current_action` |
225
+ | `Graphiti::Rails::DEPRECATOR` | `Graphiti::DEPRECATOR` (the old name still resolves) |
226
+ | `require "graphiti_errors"`, `require "graphiti/responders"` | remove / no longer needed |
227
+
228
+ `RSpec.describe PostResource, type: :resource` still picks up the resource-testing context automatically. That has not changed.
229
+
230
+ ## Removed outright {#removed-outright}
231
+
232
+ | 1.x | 2.0 |
233
+ | --- | --- |
234
+ | `include GraphitiErrors` | `register_exception` is available on every controller |
235
+ | `GraphitiErrors::ExceptionHandler` | subclass `Graphiti::Rails::ExceptionHandler` |
236
+ | `GraphitiErrors.enable!` / `.disable!` | `handle_request_exceptions` |
237
+
238
+ ## Without Rails {#without-rails}
239
+
240
+ <details>
241
+ <summary>Using the error serializers and exception handling outside Rails</summary>
242
+
243
+ The serializers move but keep working: `Graphiti::ErrorSerializers::Validation`, `::InvalidRequest` and `::ConflictRequest` load with core and need no Rails.
244
+
245
+ `GraphitiErrors::ExceptionHandler`, which turned any exception into a JSON:API errors payload, is replaced by `RescueRegistry::ExceptionHandler`, a runtime dependency now, and usable outside Rails:
246
+
247
+ ```ruby
248
+ require "rack" # or RescueRegistry::ExceptionHandler raises NameError on Rack
249
+ require "rescue_registry"
250
+
251
+ handler = RescueRegistry::ExceptionHandler.new(exception, status: 404)
252
+ handler.build_payload # => {errors: [{code: :not_found, status: "404", ...}]}
253
+ handler.formatted_response(:json) # => [404, "{\"errors\":[...]}", :json]
254
+ ```
255
+
256
+ `register_exception` and the rendering are Rails-only, but rescue_registry ships `RescueRegistry::ShowExceptions`, a Rack middleware for exactly this case. See its README.
257
+
258
+ `GraphitiErrors.logger` has no replacement. `Graphiti.logger` is the nearest thing.
259
+
260
+ </details>
@@ -2,12 +2,13 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "rails", "~> 7.1"
5
+ gem "rails", "~> 7.1.0"
6
6
  gem "rspec-rails"
7
- gem "sqlite3", "~> 2.1"
8
- gem "database_cleaner"
7
+ gem "responders"
8
+ gem "sqlite3", "~> 1.4"
9
9
 
10
10
  group :test do
11
+ gem "database_cleaner"
11
12
  gem "pry"
12
13
  gem "pry-byebug", platform: [:mri]
13
14
  gem "appraisal"
@@ -2,13 +2,13 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "rails", "~> 7.2"
5
+ gem "rails", "~> 7.2.0"
6
6
  gem "rspec-rails"
7
+ gem "responders"
7
8
  gem "sqlite3", "~> 2.1"
8
- gem "database_cleaner"
9
- gem "graphiti-rails", "~> 0.4.0"
10
9
 
11
10
  group :test do
11
+ gem "database_cleaner"
12
12
  gem "pry"
13
13
  gem "pry-byebug", platform: [:mri]
14
14
  gem "appraisal"
@@ -2,13 +2,13 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "rails", "~> 8.1"
5
+ gem "rails", "~> 8.0.0"
6
6
  gem "rspec-rails"
7
+ gem "responders"
7
8
  gem "sqlite3", "~> 2.1"
8
- gem "database_cleaner"
9
- gem "graphiti-rails", "~> 0.4.0"
10
9
 
11
10
  group :test do
11
+ gem "database_cleaner"
12
12
  gem "pry"
13
13
  gem "pry-byebug", platform: [:mri]
14
14
  gem "appraisal"
@@ -2,13 +2,13 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "rails", "~> 8.0"
5
+ gem "rails", "~> 8.1.0"
6
6
  gem "rspec-rails"
7
+ gem "responders"
7
8
  gem "sqlite3", "~> 2.1"
8
- gem "database_cleaner"
9
- gem "graphiti-rails", "~> 0.4.0"
10
9
 
11
10
  group :test do
11
+ gem "database_cleaner"
12
12
  gem "pry"
13
13
  gem "pry-byebug", platform: [:mri]
14
14
  gem "appraisal"
data/graphiti.gemspec CHANGED
@@ -16,32 +16,14 @@ Gem::Specification.new do |spec|
16
16
  spec.bindir = "exe"
17
17
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
18
  spec.require_paths = ["lib"]
19
- spec.required_ruby_version = ">= 3.0"
20
-
21
- # TODO: remove once the 1.12 upgrade window has passed
22
- spec.post_install_message = <<~MSG
23
- Graphiti: relationship readable:/writable: guards are now enforced.
24
-
25
- Symbols, strings, and procs passed to a relationship's readable/writable
26
- guards were always interpreted as true and never actually called before this
27
- conditional relationship change. They are now evaluated per-request, where
28
- unreadable relationships are omitted from responses and includes, and unwritable
29
- relationships reject writes.
30
-
31
- To list every affected relationship in your app:
32
-
33
- bin/rails runner 'puts Graphiti.guarded_relationships'
34
-
35
- Audit these before deploying. Apps using schema.json will also see affected
36
- relationships flagged as "became guarded" by the schema check.
37
- MSG
19
+ spec.required_ruby_version = ">= 3.2"
38
20
 
39
21
  spec.add_dependency "jsonapi-serializable", "~> 0.3.0"
40
22
  spec.add_dependency "jsonapi-renderer", "~> 0.2", ">= 0.2.2"
41
23
  spec.add_dependency "dry-types", ">= 0.15.0", "< 2.0"
42
- spec.add_dependency "graphiti_errors", "~> 1.1.0"
24
+ spec.add_dependency "rescue_registry", "~> 1.0"
43
25
  spec.add_dependency "concurrent-ruby", ">= 1.2", "< 2.0"
44
- spec.add_dependency "activesupport", ">= 5.2"
26
+ spec.add_dependency "activesupport", ">= 7.1"
45
27
  # Bundled (no longer default) as of Ruby 3.5; graphiti uses OpenStruct in lib/
46
28
  spec.add_dependency "ostruct", ">= 0.5"
47
29
 
@@ -50,6 +32,8 @@ Gem::Specification.new do |spec|
50
32
  spec.add_development_dependency "bundler"
51
33
  spec.add_development_dependency "rake", ">= 10.0"
52
34
  spec.add_development_dependency "standard", "~> 1.4.0"
53
- spec.add_development_dependency "activemodel", ">= 5.2"
54
- spec.add_development_dependency "graphiti_spec_helpers", "1.0.beta.4"
35
+ spec.add_development_dependency "activemodel", ">= 7.1"
36
+ # Was reaching the suite transitively through graphiti_spec_helpers, which is
37
+ # now part of this gem.
38
+ spec.add_development_dependency "rspec", "~> 3.0"
55
39
  end
@@ -4,7 +4,7 @@ module Graphiti
4
4
  class ApiTestGenerator < ::Rails::Generators::Base
5
5
  include GeneratorMixin
6
6
 
7
- source_root File.expand_path("templates", __dir__)
7
+ source_root File.expand_path("../templates", __FILE__)
8
8
 
9
9
  argument :resource, type: :string
10
10
  class_option :actions,
@@ -13,6 +13,12 @@ module Graphiti
13
13
  aliases: ["--actions", "-a"],
14
14
  desc: 'Array of controller actions, e.g. "index show destroy"'
15
15
 
16
+ class_option :rawid,
17
+ type: :boolean,
18
+ default: false,
19
+ aliases: ["--rawid", "-r"],
20
+ desc: "Generate tests using rawid"
21
+
16
22
  desc "Generates rspec request specs at spec/api"
17
23
  def generate
18
24
  generate_api_specs
@@ -12,7 +12,6 @@ module Graphiti
12
12
  def api_namespace
13
13
  @api_namespace ||= begin
14
14
  ns = graphiti_config["namespace"]
15
- ns.delete_suffix("/")
16
15
 
17
16
  if ns.blank?
18
17
  ns = prompt \
@@ -42,5 +41,19 @@ module Graphiti
42
41
  config = graphiti_config.merge(attrs)
43
42
  File.open(".graphiticfg.yml", "w") { |f| f.write(config.to_yaml) }
44
43
  end
44
+
45
+ def id_or_rawid
46
+ @options["rawid"] ? "rawid" : "id"
47
+ end
48
+
49
+ def sort_raw_ids
50
+ return unless @options["rawid"]
51
+ ".sort"
52
+ end
53
+
54
+ def sort_raw_ids_descending
55
+ return unless @options["rawid"]
56
+ ".sort.reverse"
57
+ end
45
58
  end
46
59
  end
@@ -4,7 +4,7 @@ module Graphiti
4
4
  class InstallGenerator < ::Rails::Generators::Base
5
5
  include GeneratorMixin
6
6
 
7
- source_root File.expand_path("../templates", __FILE__)
7
+ source_root File.expand_path("templates", __dir__)
8
8
 
9
9
  class_option :'omit-comments',
10
10
  type: :boolean,
@@ -41,8 +41,20 @@ module Graphiti
41
41
  RUBY
42
42
  end
43
43
 
44
- inject_into_file "spec/rails_helper.rb", after: /RSpec.configure.+^end$/m do
45
- "\n\nGraphitiSpecHelpers::RSpec.schema!"
44
+ if defined?(RSpec)
45
+ inject_into_file "spec/rails_helper.rb", after: /RSpec.configure.+^end$/m do
46
+ <<~RUBY
47
+
48
+ require "graphiti/spec_helpers/rspec"
49
+
50
+ RSpec.configure do |config|
51
+ config.include Graphiti::SpecHelpers::RSpec
52
+ config.include Graphiti::SpecHelpers::Sugar
53
+ end
54
+
55
+ Graphiti::SpecHelpers::RSpec.schema!
56
+ RUBY
57
+ end
46
58
  end
47
59
 
48
60
  insert_into_file "config/routes.rb", after: "Rails.application.routes.draw do\n" do
@@ -70,16 +82,10 @@ module Graphiti
70
82
  end
71
83
 
72
84
  def app_controller_code
73
- str = ""
74
- str << " include Graphiti::Rails\n"
75
- str << " include Graphiti::Responders\n"
76
- str << "\n"
77
- str << " register_exception Graphiti::Errors::RecordNotFound,\n"
78
- str << " status: 404\n"
79
- str << "\n"
80
- str << " rescue_from Exception do |e|\n"
81
- str << " handle_exception(e)\n"
82
- str << " end\n"
85
+ str = +" include Graphiti::Rails::Controller\n"
86
+ if defined?(::Responders)
87
+ str << " include Graphiti::Rails::Responders\n"
88
+ end
83
89
  str
84
90
  end
85
91
  end
@@ -14,6 +14,12 @@ module Graphiti
14
14
  aliases: ["--omit-comments", "-c"],
15
15
  desc: "Generate without documentation comments"
16
16
 
17
+ class_option :rawid,
18
+ type: :boolean,
19
+ default: false,
20
+ aliases: ["--rawid", "-r"],
21
+ desc: "Generate tests using rawid"
22
+
17
23
  class_option :actions,
18
24
  type: :array,
19
25
  default: nil,
@@ -26,6 +32,12 @@ module Graphiti
26
32
  aliases: ["--model", "-m"],
27
33
  desc: "Specify to use attributes from a particular model"
28
34
 
35
+ # No short alias: -c is already --omit-comments.
36
+ class_option :controller,
37
+ banner: "Name",
38
+ type: :string,
39
+ desc: "Generate the controller under this name instead of the resource's, e.g. Api::V1::Posts"
40
+
29
41
  desc "This generator creates a resource file at app/resources, as well as corresponding controller/specs/route/etc"
30
42
  def generate_all
31
43
  generate_model
@@ -94,7 +106,10 @@ module Graphiti
94
106
  end
95
107
  if attributes_class.table_exists?
96
108
  attributes_class.columns.map do |c|
97
- OpenStruct.new({name: c.name.to_sym, type: c.type})
109
+ OpenStruct.new({
110
+ name: c.name.to_sym,
111
+ type: convert_column_type_to_graphiti_resource_type(c.type)
112
+ })
98
113
  end
99
114
  else
100
115
  raise "#{attributes_class} table must exist. Please run migrations."
@@ -113,11 +128,26 @@ module Graphiti
113
128
  end
114
129
 
115
130
  def responders?
116
- defined?(Responders)
131
+ defined?(::Responders)
132
+ end
133
+
134
+ # Api::V1::Posts, Api::V1::PostsController and api/v1/posts all name the
135
+ # same controller. Normalizing here means the class name, the file path and
136
+ # the route all come from one place.
137
+ def controller_class_name
138
+ @controller_class_name ||= begin
139
+ given = options[:controller]
140
+ base = given ? given.sub(/Controller\z/, "").camelize : model_klass.name
141
+ "#{base.pluralize}Controller"
142
+ end
143
+ end
144
+
145
+ def controller_path
146
+ controller_class_name.sub(/Controller\z/, "").underscore
117
147
  end
118
148
 
119
149
  def generate_controller
120
- to = File.join("app/controllers", class_path, "#{file_name.pluralize}_controller.rb")
150
+ to = File.join("app/controllers", "#{controller_path}_controller.rb")
121
151
  template("controller.rb.erb", to)
122
152
  end
123
153
 
@@ -132,10 +162,10 @@ module Graphiti
132
162
  end
133
163
 
134
164
  def generate_route
135
- # Rails 5.2 adds `plural_route_name`, fallback to `plural_table_name`
136
- plural_name = try(:plural_route_name) || plural_table_name
137
-
138
- code = "resources :#{plural_name}"
165
+ code = "resources :#{file_name.pluralize}"
166
+ # Rails would otherwise infer the controller from the route name, which
167
+ # is only right when the controller was named after the resource.
168
+ code << %(, controller: "#{controller_path}") if options[:controller]
139
169
  code << %(, only: [#{actions.map { |a| ":#{a}" }.join(", ")}]) if actions.length < 5
140
170
  code << "\n"
141
171
  inject_into_file "config/routes.rb", after: /ApplicationResource.*$\n/ do
@@ -146,12 +176,14 @@ module Graphiti
146
176
  def generate_resource_specs
147
177
  opts = {}
148
178
  opts[:actions] = @options[:actions] if @options[:actions]
179
+ opts[:rawid] = @options[:rawid] if @options[:rawid]
149
180
  invoke "graphiti:resource_test", [resource_klass], opts
150
181
  end
151
182
 
152
183
  def generate_api_specs
153
184
  opts = {}
154
185
  opts[:actions] = @options[:actions] if @options[:actions]
186
+ opts[:rawid] = @options[:rawid] if @options[:rawid]
155
187
  invoke "graphiti:api_test", [resource_klass], opts
156
188
  end
157
189
 
@@ -176,5 +208,34 @@ module Graphiti
176
208
  def type
177
209
  model_klass.name.underscore.pluralize
178
210
  end
211
+
212
+ def convert_column_type_to_graphiti_resource_type(type)
213
+ # TODO: Support database specific types.
214
+ case type
215
+ when :string, :text
216
+ :string
217
+ when :float, :decimal
218
+ :integer
219
+ when :integer, :bigint
220
+ :integer
221
+ when :datetime, :time
222
+ :datetime
223
+ when :date
224
+ :date
225
+ when :boolean
226
+ :boolean
227
+ when :numeric
228
+ # TODO: Return type.
229
+ type
230
+ when :primary_key
231
+ # TODO: Return type.
232
+ type
233
+ when :binary
234
+ # TODO: Return type.
235
+ type
236
+ else
237
+ type
238
+ end
239
+ end
179
240
  end
180
241
  end
@@ -1,5 +1,5 @@
1
1
  <% module_namespacing do -%>
2
- class <%= model_klass.name.pluralize %>Controller < ApplicationController
2
+ class <%= controller_class_name %> < ApplicationController
3
3
  <%- if actions?('index') -%>
4
4
  def index
5
5
  <%= file_name.pluralize %> = <%= resource_klass %>.all(params)
@@ -16,7 +16,7 @@ RSpec.describe "<%= type %>#index", type: :request do
16
16
  make_request
17
17
  expect(response.status).to eq(200), response.body
18
18
  expect(d.map(&:jsonapi_type).uniq).to match_array(['<%= type %>'])
19
- expect(d.map(&:id)).to match_array([<%= var %>1.id, <%= var %>2.id])
19
+ expect(d.map(&:<%= id_or_rawid %>)).to match_array([<%= var %>1.id, <%= var %>2.id])
20
20
  end
21
21
  end
22
22
  end