graphiti 2.0.0.beta.2 → 2.0.0.beta.4
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.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +30 -86
- data/.github/workflows/docs.yml +60 -0
- data/.github/workflows/release.yml +8 -8
- data/.gitignore +7 -0
- data/.npmrc +9 -0
- data/.standard.yml +4 -1
- data/Appraisals +33 -32
- data/CHANGELOG.md +41 -0
- data/README.md +13 -2
- data/UPGRADING.md +2 -68
- data/docs/concepts/backends-and-models.md +122 -0
- data/docs/concepts/endpoints.md +183 -0
- data/docs/concepts/links.md +212 -0
- data/docs/concepts/overview.md +80 -0
- data/docs/concepts/persisting.md +376 -0
- data/docs/concepts/relationships.md +527 -0
- data/docs/concepts/resources.md +677 -0
- data/docs/getting-started/first-api.md +289 -0
- data/docs/getting-started/installation.md +185 -0
- data/docs/intro.md +307 -0
- data/docs/js/authentication.md +63 -0
- data/docs/js/ddau.md +20 -0
- data/docs/js/extra-params.md +41 -0
- data/docs/js/index.md +112 -0
- data/docs/js/installation.md +120 -0
- data/docs/js/middleware.md +72 -0
- data/docs/js/models.md +202 -0
- data/docs/js/reads.md +494 -0
- data/docs/js/state-syncing.md +100 -0
- data/docs/js/writes.md +373 -0
- data/docs/reference/vandal.md +63 -0
- data/docs/reference/why.md +13 -0
- data/docs/topics/authorization.md +155 -0
- data/docs/topics/caching.md +55 -0
- data/docs/topics/customizing-sideloads.md +156 -0
- data/docs/topics/debugging.md +216 -0
- data/docs/topics/error-handling.md +210 -0
- data/docs/topics/etags.md +46 -0
- data/docs/topics/hopping-relationships.md +149 -0
- data/docs/topics/json-attributes.md +77 -0
- data/docs/topics/openstruct-models.md +50 -0
- data/docs/topics/remote-resources.md +291 -0
- data/docs/topics/testing.md +894 -0
- data/docs/topics/without-activerecord.md +324 -0
- data/docs/tutorial/index.md +58 -0
- data/docs/tutorial/step_0.md +107 -0
- data/docs/tutorial/step_1.md +199 -0
- data/docs/tutorial/step_2.md +312 -0
- data/docs/tutorial/step_3.md +142 -0
- data/docs/tutorial/step_4.md +135 -0
- data/docs/tutorial/step_5.md +69 -0
- data/docs/tutorial/step_6.md +82 -0
- data/docs/tutorial/step_7.md +205 -0
- data/docs/tutorial/step_8.md +128 -0
- data/docs/tutorial/step_9.md +171 -0
- data/docs/upgrading.md +265 -0
- data/gemfiles/rails_7_1.gemfile +4 -3
- data/gemfiles/{rails_7_2_graphiti_rails.gemfile → rails_7_2.gemfile} +3 -3
- data/gemfiles/{rails_8_1_graphiti_rails.gemfile → rails_8_0.gemfile} +3 -3
- data/gemfiles/{rails_8_0_graphiti_rails.gemfile → rails_8_1.gemfile} +3 -3
- data/graphiti.gemspec +7 -5
- data/{deprecated_generators → lib/generators}/graphiti/api_test_generator.rb +7 -1
- data/{deprecated_generators → lib/generators}/graphiti/generator_mixin.rb +14 -1
- data/{deprecated_generators → lib/generators}/graphiti/install_generator.rb +19 -13
- data/{deprecated_generators → lib/generators}/graphiti/resource_generator.rb +43 -6
- data/{deprecated_generators → lib/generators}/graphiti/templates/index_request_spec.rb.erb +1 -1
- data/{deprecated_generators → lib/generators}/graphiti/templates/resource_reads_spec.rb.erb +6 -6
- data/{deprecated_generators → lib/generators}/graphiti/templates/show_request_spec.rb.erb +1 -1
- data/lib/graphiti/configuration.rb +2 -2
- data/lib/graphiti/error_serializers/conflict_request.rb +19 -0
- data/lib/graphiti/error_serializers/deprecated_constants.rb +48 -0
- data/lib/graphiti/error_serializers/invalid_request.rb +56 -0
- data/lib/graphiti/error_serializers/validation.rb +143 -0
- data/lib/graphiti/errors.rb +4 -23
- data/lib/graphiti/query.rb +1 -1
- data/lib/graphiti/rails/context.rb +33 -0
- data/lib/graphiti/rails/controller.rb +41 -0
- data/lib/graphiti/rails/debugging.rb +18 -0
- data/lib/graphiti/rails/exception_handlers.rb +77 -0
- data/lib/graphiti/rails/railtie.rb +139 -0
- data/lib/graphiti/rails/responders.rb +21 -0
- data/lib/graphiti/rails/test_helpers.rb +22 -0
- data/lib/graphiti/rails.rb +47 -29
- data/lib/graphiti/resource/configuration.rb +1 -0
- data/lib/graphiti/resource/interface.rb +2 -2
- data/lib/graphiti/resource/persistence.rb +14 -2
- data/lib/graphiti/resource/remote.rb +2 -2
- data/lib/graphiti/resource/sideloading.rb +1 -1
- data/lib/graphiti/resource.rb +13 -1
- data/lib/graphiti/responders.rb +7 -20
- data/lib/graphiti/schema.rb +5 -1
- data/lib/graphiti/schema_diff.rb +4 -0
- data/lib/graphiti/scope.rb +45 -37
- data/lib/graphiti/serializer.rb +6 -0
- data/lib/graphiti/sideload/belongs_to.rb +38 -5
- data/lib/graphiti/sideload/polymorphic_belongs_to.rb +27 -23
- data/lib/graphiti/sideload.rb +54 -35
- data/lib/graphiti/spec_helpers/errors.rb +73 -0
- data/lib/graphiti/spec_helpers/errors_proxy.rb +75 -0
- data/lib/graphiti/spec_helpers/helpers.rb +107 -0
- data/lib/graphiti/spec_helpers/node.rb +88 -0
- data/lib/graphiti/spec_helpers/rspec.rb +147 -0
- data/lib/graphiti/spec_helpers.rb +53 -0
- data/lib/graphiti/util/include_params.rb +2 -2
- data/lib/graphiti/util/persistence.rb +10 -11
- data/lib/graphiti/util/serializer_relationships.rb +41 -5
- data/lib/graphiti/version.rb +1 -1
- data/lib/graphiti-rails.rb +11 -0
- data/lib/graphiti.rb +34 -10
- data/lib/graphiti_errors.rb +11 -0
- data/lib/graphiti_spec_helpers/rspec.rb +3 -0
- data/lib/graphiti_spec_helpers.rb +11 -0
- data/lib/{graphiti/deprecated_tasks.rb → tasks/graphiti.rake} +6 -1
- data/package-lock.json +6199 -0
- data/package.json +5 -4
- data/website/.gitignore +20 -0
- data/website/README.md +43 -0
- data/website/docusaurus.config.js +141 -0
- data/website/package-lock.json +19474 -0
- data/website/package.json +46 -0
- data/website/sidebars.js +82 -0
- data/website/src/css/custom.css +58 -0
- data/website/src/pages/markdown-page.mdx +7 -0
- data/website/static/.nojekyll +0 -0
- data/website/static/1.13/2019/03/31/graphiti-1-0.html +205 -0
- data/website/static/1.13/2019/05/08/graphiti-1-1.html +212 -0
- data/website/static/1.13/2019/05/20/graphiti-1-2.html +214 -0
- data/website/static/1.13/2019/10/14/tutorial.html +198 -0
- data/website/static/1.13/CNAME +1 -0
- data/website/static/1.13/README.md +16 -0
- data/website/static/1.13/assets/css/syntax.css +60 -0
- data/website/static/1.13/assets/favicons/android-chrome-192x192.png +0 -0
- data/website/static/1.13/assets/favicons/android-chrome-256x256.png +0 -0
- data/website/static/1.13/assets/favicons/apple-touch-icon.png +0 -0
- data/website/static/1.13/assets/favicons/browserconfig.xml +9 -0
- data/website/static/1.13/assets/favicons/favicon-16x16.png +0 -0
- data/website/static/1.13/assets/favicons/favicon-32x32.png +0 -0
- data/website/static/1.13/assets/favicons/favicon.ico +0 -0
- data/website/static/1.13/assets/favicons/mstile-150x150.png +0 -0
- data/website/static/1.13/assets/favicons/safari-pinned-tab.svg +1 -0
- data/website/static/1.13/assets/favicons/site.webmanifest +19 -0
- data/website/static/1.13/assets/img/backend.gif +0 -0
- data/website/static/1.13/assets/img/conformity.png +0 -0
- data/website/static/1.13/assets/img/error_payload.png +0 -0
- data/website/static/1.13/assets/img/gh.png +0 -0
- data/website/static/1.13/assets/img/lifecycle.gif +0 -0
- data/website/static/1.13/assets/img/logo-500.png +0 -0
- data/website/static/1.13/assets/img/logo.png +0 -0
- data/website/static/1.13/assets/img/love-graffiti.jpg +0 -0
- data/website/static/1.13/assets/img/meta_total_count.png +0 -0
- data/website/static/1.13/assets/img/persist.jpg +0 -0
- data/website/static/1.13/assets/img/resource.gif +0 -0
- data/website/static/1.13/assets/img/rest-graffiti.jpg +0 -0
- data/website/static/1.13/assets/img/rest1.gif +0 -0
- data/website/static/1.13/assets/img/rest2.gif +0 -0
- data/website/static/1.13/assets/img/rest3.gif +0 -0
- data/website/static/1.13/assets/img/rethink-rest-graffiti.jpg +0 -0
- data/website/static/1.13/assets/img/why.png +0 -0
- data/website/static/1.13/assets/js/highlight.pack.js +2 -0
- data/website/static/1.13/assets/main.css +15518 -0
- data/website/static/1.13/assets/main.css.map +1 -0
- data/website/static/1.13/bin/bundle +109 -0
- data/website/static/1.13/bin/jekyll +27 -0
- data/website/static/1.13/bin/kramdown +27 -0
- data/website/static/1.13/bin/listen +27 -0
- data/website/static/1.13/bin/rake +27 -0
- data/website/static/1.13/bin/rougify +27 -0
- data/website/static/1.13/bin/safe_yaml +27 -0
- data/website/static/1.13/bin/sass +27 -0
- data/website/static/1.13/bin/sass-convert +27 -0
- data/website/static/1.13/bin/scss +27 -0
- data/website/static/1.13/blog.html +259 -0
- data/website/static/1.13/cheatsheet.html +316 -0
- data/website/static/1.13/cookbooks/authorization.md +0 -0
- data/website/static/1.13/cookbooks/caching.md +0 -0
- data/website/static/1.13/cookbooks/customizing-sideloads.html +325 -0
- data/website/static/1.13/cookbooks/etags.md +0 -0
- data/website/static/1.13/cookbooks/hopping-relationships.html +324 -0
- data/website/static/1.13/cookbooks/json_attributes.md +0 -0
- data/website/static/1.13/cookbooks/openstruct-models.md +0 -0
- data/website/static/1.13/cookbooks/remote-resources.md +0 -0
- data/website/static/1.13/cookbooks/without-activerecord.html +510 -0
- data/website/static/1.13/features.html +249 -0
- data/website/static/1.13/feed.xml +106 -0
- data/website/static/1.13/guides/concepts/backends-and-models.html +467 -0
- data/website/static/1.13/guides/concepts/debugging.html +440 -0
- data/website/static/1.13/guides/concepts/endpoints.html +432 -0
- data/website/static/1.13/guides/concepts/error-handling.html +396 -0
- data/website/static/1.13/guides/concepts/links.html +501 -0
- data/website/static/1.13/guides/concepts/remote-resources.html +536 -0
- data/website/static/1.13/guides/concepts/resources.html +2176 -0
- data/website/static/1.13/guides/concepts/testing.html +1469 -0
- data/website/static/1.13/guides/getting-started/installation.html +420 -0
- data/website/static/1.13/guides/graphiti-rails-migration.html +242 -0
- data/website/static/1.13/guides/index.html +269 -0
- data/website/static/1.13/guides/overview.html +325 -0
- data/website/static/1.13/guides/upgrading-2-0.html +193 -0
- data/website/static/1.13/guides/upgrading.html +314 -0
- data/website/static/1.13/guides/vandal.html +282 -0
- data/website/static/1.13/guides/why.html +1121 -0
- data/website/static/1.13/index.html +72 -0
- data/website/static/1.13/js/authentication.html +295 -0
- data/website/static/1.13/js/ddau.html +238 -0
- data/website/static/1.13/js/extra-params.html +270 -0
- data/website/static/1.13/js/index.html +321 -0
- data/website/static/1.13/js/installation.html +637 -0
- data/website/static/1.13/js/introduction.html +257 -0
- data/website/static/1.13/js/middleware.html +318 -0
- data/website/static/1.13/js/reads/fieldsets.html +271 -0
- data/website/static/1.13/js/reads/filtering.html +289 -0
- data/website/static/1.13/js/reads/includes.html +260 -0
- data/website/static/1.13/js/reads/index.html +497 -0
- data/website/static/1.13/js/reads/nested-queries.html +353 -0
- data/website/static/1.13/js/reads/pagination.html +260 -0
- data/website/static/1.13/js/reads/sorting.html +265 -0
- data/website/static/1.13/js/reads/statistics.html +289 -0
- data/website/static/1.13/js/state-syncing.html +340 -0
- data/website/static/1.13/js/writes/deferred.html +296 -0
- data/website/static/1.13/js/writes/dirty-tracking.html +399 -0
- data/website/static/1.13/js/writes/index.html +391 -0
- data/website/static/1.13/js/writes/nested.html +330 -0
- data/website/static/1.13/js/writes/validations.html +272 -0
- data/website/static/1.13/quickstart.html +660 -0
- data/website/static/1.13/template +161 -0
- data/website/static/1.13/tutorial/index.html +250 -0
- data/website/static/1.13/tutorial/step_0.html +292 -0
- data/website/static/1.13/tutorial/step_1.html +517 -0
- data/website/static/1.13/tutorial/step_2.html +481 -0
- data/website/static/1.13/tutorial/step_3.html +323 -0
- data/website/static/1.13/tutorial/step_4.html +318 -0
- data/website/static/1.13/tutorial/step_5.html +265 -0
- data/website/static/1.13/tutorial/step_6.html +276 -0
- data/website/static/1.13/tutorial/step_7.html +390 -0
- data/website/static/1.13/tutorial/step_8.html +316 -0
- data/website/static/1.13/tutorial/step_9.html +365 -0
- data/website/static/assets/img/error_payload.png +0 -0
- data/website/static/assets/img/legacy/legacy-0378a3bb39.png +0 -0
- data/website/static/assets/img/legacy/legacy-05bbd3e5fd.png +0 -0
- data/website/static/assets/img/legacy/legacy-07aa104495.png +0 -0
- data/website/static/assets/img/legacy/legacy-0c75a16b3a.gif +0 -0
- data/website/static/assets/img/legacy/legacy-3076df6209.png +0 -0
- data/website/static/assets/img/legacy/legacy-7f6889bc89.png +0 -0
- data/website/static/assets/img/legacy/legacy-a2cc4363c3.png +0 -0
- data/website/static/assets/img/legacy/legacy-f67cfa89ab.png +0 -0
- data/website/static/assets/img/meta_total_count.png +0 -0
- data/website/static/img/docusaurus-social-card.jpg +0 -0
- data/website/static/img/docusaurus.png +0 -0
- data/website/static/img/favicon.ico +0 -0
- data/website/static/img/logo.png +0 -0
- data/website/static/img/logo.svg +1 -0
- data/website/static/img/undraw_docusaurus_mountain.svg +171 -0
- data/website/static/img/undraw_docusaurus_react.svg +170 -0
- data/website/static/img/undraw_docusaurus_tree.svg +40 -0
- metadata +245 -46
- data/gemfiles/rails_6.gemfile +0 -18
- data/gemfiles/rails_6_graphiti_rails.gemfile +0 -19
- data/gemfiles/rails_7.gemfile +0 -18
- data/gemfiles/rails_7_1_graphiti_rails.gemfile +0 -19
- data/gemfiles/rails_7_graphiti_rails.gemfile +0 -19
- data/lib/graphiti/railtie.rb +0 -121
- /data/{deprecated_generators → lib/generators}/graphiti/resource_test_generator.rb +0 -0
- /data/{deprecated_generators → lib/generators}/graphiti/templates/application_resource.rb.erb +0 -0
- /data/{deprecated_generators → lib/generators}/graphiti/templates/controller.rb.erb +0 -0
- /data/{deprecated_generators → lib/generators}/graphiti/templates/create_request_spec.rb.erb +0 -0
- /data/{deprecated_generators → lib/generators}/graphiti/templates/destroy_request_spec.rb.erb +0 -0
- /data/{deprecated_generators → lib/generators}/graphiti/templates/resource.rb.erb +0 -0
- /data/{deprecated_generators → lib/generators}/graphiti/templates/resource_writes_spec.rb.erb +0 -0
- /data/{deprecated_generators → lib/generators}/graphiti/templates/update_request_spec.rb.erb +0 -0
data/docs/js/reads.md
ADDED
|
@@ -0,0 +1,494 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: 'Reads'
|
|
3
|
+
sidebar_position: 4
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
The interface for read operations is a simpler version of the [ActiveRecord Query Interface](http://guides.rubyonrails.org/active_record_querying.html). Instead of generating SQL, we'll be generating JSONAPI requests.
|
|
7
|
+
|
|
8
|
+
## Basic Finders
|
|
9
|
+
|
|
10
|
+
Execute queries with `.all()`, `find()`, or `.first()`:
|
|
11
|
+
|
|
12
|
+
```typescript
|
|
13
|
+
let response = await Post.all()
|
|
14
|
+
response.data // array of Post instances
|
|
15
|
+
```
|
|
16
|
+
```javascript
|
|
17
|
+
Post.all().then(function(response) {
|
|
18
|
+
response.data // array of Post instances
|
|
19
|
+
});
|
|
20
|
+
```
|
|
21
|
+
<blockquote class="url">
|
|
22
|
+
<p>GET /posts</p>
|
|
23
|
+
</blockquote>
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
let response = await Post.find(123)
|
|
27
|
+
response.data // Post instance
|
|
28
|
+
```
|
|
29
|
+
```javascript
|
|
30
|
+
Post.find(123).then(function(response) {
|
|
31
|
+
response.data // Post instance
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
<blockquote class="url">
|
|
35
|
+
<p>GET /posts/123</p>
|
|
36
|
+
</blockquote>
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
let response = await Post.first()
|
|
40
|
+
response.data // Post instance
|
|
41
|
+
```
|
|
42
|
+
```javascript
|
|
43
|
+
Post.first().then(function(response) {
|
|
44
|
+
response.data // Post instance
|
|
45
|
+
});
|
|
46
|
+
```
|
|
47
|
+
<blockquote class="url">
|
|
48
|
+
<p>GET /posts?page[size]=1</p>
|
|
49
|
+
</blockquote>
|
|
50
|
+
|
|
51
|
+
## Composable Queries with Scopes
|
|
52
|
+
|
|
53
|
+
The beauty of ORMs is their ability to compose queries. We'll be doing this by chaining together `Scope`s (query fragments). All of the methods you see on this page can be chained together - the request will not fire until the chain ends with `all()`, `first()`, or `find`. Example:
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
let scope = Post.order({ name: "desc" })
|
|
57
|
+
|
|
58
|
+
if (someCheckboxIsChecked) {
|
|
59
|
+
scope = scope.where({ important: true })
|
|
60
|
+
} else {
|
|
61
|
+
scope = scope.where({ important: false })
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
scope.all() // request fires
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
```javascript
|
|
68
|
+
var scope = Post.order({ name: "desc" });
|
|
69
|
+
|
|
70
|
+
if (someCheckboxIsChecked) {
|
|
71
|
+
scope = scope.where({ important: true });
|
|
72
|
+
} else {
|
|
73
|
+
scope = scope.where({ important: false });
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
scope.all() // request fires
|
|
77
|
+
```
|
|
78
|
+
<blockquote class="url">
|
|
79
|
+
<p>/posts?sort=-name&filter[important]=true</p>
|
|
80
|
+
<p>/posts?sort=-name&filter[important]=false</p>
|
|
81
|
+
</blockquote>
|
|
82
|
+
|
|
83
|
+
In practice, you'll probably have some scopes you want to re-use across different contexts. A best practice is to store these scopes as class methods (static methods) in the model:
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
class Post extends ApplicationRecord {
|
|
87
|
+
// ... code ...
|
|
88
|
+
static superImportant() {
|
|
89
|
+
return this
|
|
90
|
+
.where({ ranking_gt: 8 })
|
|
91
|
+
.order({ ranking: 'desc' })
|
|
92
|
+
.stats({ total 'count' })
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// get 10 super important posts
|
|
97
|
+
let scope = Post.superImportant().per(10)
|
|
98
|
+
scope.all() // fire query
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
```javascript
|
|
102
|
+
const Post = ApplicationRecord.extend({
|
|
103
|
+
// ... code ...
|
|
104
|
+
static: {
|
|
105
|
+
superImportant() {
|
|
106
|
+
return this
|
|
107
|
+
.where({ ranking_gt: 8 })
|
|
108
|
+
.order({ ranking: 'desc' })
|
|
109
|
+
.stats({ total 'count' })
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
// get 10 super important posts
|
|
115
|
+
var scope = Post.superImportant().per(10);
|
|
116
|
+
scope.all() // fire query
|
|
117
|
+
```
|
|
118
|
+
<blockquote class="url">
|
|
119
|
+
<p>/posts?sort=-ranking&stats[total]=count&page[size]=10&filter[ranking_gt]=8</p>
|
|
120
|
+
</blockquote>
|
|
121
|
+
|
|
122
|
+
## Metadata
|
|
123
|
+
|
|
124
|
+
The [meta information](http://jsonapi.org/format/#document-meta) of the JSONAPI response is available as a POJO on the response:
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
let response = await Post.all()
|
|
128
|
+
response.meta // { stats: { total: { count: 100 } } }
|
|
129
|
+
```
|
|
130
|
+
```javascript
|
|
131
|
+
await Post.all().then(function(response) {
|
|
132
|
+
response.meta // { stats: { total: { count: 100 } } }
|
|
133
|
+
})
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## Promises and Async/Await
|
|
137
|
+
|
|
138
|
+
The result of `all()`, `first()` or `find` is a [Promise](https://developers.google.com/web/fundamentals/primers/promises). The promise will resolve to a `Response` object.
|
|
139
|
+
|
|
140
|
+
A `Response` object has three keys - `data`, `meta`, and `raw`. `data` - the one you'll be using the most - will be a `Model` instance (or array of `Model`) instances. `meta` will be the [Meta Information](http://jsonapi.org/format/#document-meta) returned by the API (mostly used for statistics in our case). `raw` is only used to introspect the raw response document.
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
Post.all().then((response) => {
|
|
144
|
+
response.data // array of Post instances
|
|
145
|
+
response.meta // js object from the server
|
|
146
|
+
response.raw // js response document
|
|
147
|
+
})
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
```javascript
|
|
151
|
+
Post.all().then(function(response) {
|
|
152
|
+
response.data // array of Post instances
|
|
153
|
+
response.meta // js object from the server
|
|
154
|
+
response.raw // js response document
|
|
155
|
+
});
|
|
156
|
+
```
|
|
157
|
+
<blockquote class="url">
|
|
158
|
+
<p>/posts</p>
|
|
159
|
+
</blockquote>
|
|
160
|
+
|
|
161
|
+
Hopefully you're running in an environment that supports ES7's [Async/Await](https://hackernoon.com/6-reasons-why-javascripts-async-await-blows-promises-away-tutorial-c7ec10518dd9). This makes things even easier:
|
|
162
|
+
|
|
163
|
+
```typescript
|
|
164
|
+
let { data } = await Post.all()
|
|
165
|
+
data // array of Post instances
|
|
166
|
+
|
|
167
|
+
// alternatively
|
|
168
|
+
|
|
169
|
+
let posts = (await Post.all()).data
|
|
170
|
+
posts // array of Post instances
|
|
171
|
+
```
|
|
172
|
+
<blockquote class="url">
|
|
173
|
+
<p>/posts</p>
|
|
174
|
+
</blockquote>
|
|
175
|
+
|
|
176
|
+
## Filtering
|
|
177
|
+
|
|
178
|
+
Use `#where()` to apply filters:
|
|
179
|
+
|
|
180
|
+
```typescript
|
|
181
|
+
Post.where({ important: true }).all()
|
|
182
|
+
```
|
|
183
|
+
<blockquote class="url">
|
|
184
|
+
<p>/posts?filter[important]=true</p>
|
|
185
|
+
</blockquote>
|
|
186
|
+
|
|
187
|
+
`#where()` clauses can be chained together. If the same key is seen twice, it will be overridden:
|
|
188
|
+
|
|
189
|
+
```typescript
|
|
190
|
+
Post
|
|
191
|
+
.where({ important: true })
|
|
192
|
+
.where({ ranking: 10 })
|
|
193
|
+
.where({ important: false })
|
|
194
|
+
.all()
|
|
195
|
+
```
|
|
196
|
+
<blockquote class="url">
|
|
197
|
+
<p>/posts?filter[important]=false&filter[ranking]=10</p>
|
|
198
|
+
</blockquote>
|
|
199
|
+
|
|
200
|
+
`#where()` clauses are based on **server implementation**. The key should be exactly as the server understands it. Here are some common conventions we promote:
|
|
201
|
+
|
|
202
|
+
```typescript
|
|
203
|
+
// id greater than 5
|
|
204
|
+
Post.where({ id_gt: 5 }).all()
|
|
205
|
+
|
|
206
|
+
// id greater than or equal to 5
|
|
207
|
+
Post.where({ id_gte: 5 }).all()
|
|
208
|
+
|
|
209
|
+
// id less than 5
|
|
210
|
+
Post.where({ id_lt: 5 }).all()
|
|
211
|
+
|
|
212
|
+
// id less or equal to 5
|
|
213
|
+
Post.where({ id_lte: 5 }).all()
|
|
214
|
+
|
|
215
|
+
// title starts with "foo"
|
|
216
|
+
Post.where({ title: { prefix: "foo" } }).all()
|
|
217
|
+
|
|
218
|
+
// OR these two values
|
|
219
|
+
Post.where({ status_or: ['draft', 'review'] })
|
|
220
|
+
|
|
221
|
+
// AND these two values (default)
|
|
222
|
+
Post.where({ status: ['draft', 'review'] })
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
### Escaping Values
|
|
226
|
+
|
|
227
|
+
[Graphiti treats a comma as a delimiter of multiple values](/concepts/resources#escaping-values). To escape the comma and tell Graphiti this is a single value, wrap it in `{{curlies}}`:
|
|
228
|
+
|
|
229
|
+
```typescript
|
|
230
|
+
Post.where({ title: "{{Hello World, here I am}}" })
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
## Sorting
|
|
234
|
+
|
|
235
|
+
Use `#order()` to sort.
|
|
236
|
+
|
|
237
|
+
If passed a string, it will default to **ascending**:
|
|
238
|
+
|
|
239
|
+
```typescript
|
|
240
|
+
Post.order("title").all()
|
|
241
|
+
```
|
|
242
|
+
<blockquote class="url">
|
|
243
|
+
<p>/posts?sort=title</p>
|
|
244
|
+
</blockquote>
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
Otherwise, pass an object:
|
|
248
|
+
|
|
249
|
+
```typescript
|
|
250
|
+
Post.order({ title: "desc" }).all()
|
|
251
|
+
```
|
|
252
|
+
<blockquote class="url">
|
|
253
|
+
<p>/posts?sort=-title</p>
|
|
254
|
+
</blockquote>
|
|
255
|
+
|
|
256
|
+
For multisort, chain multiple `#order()` clauses:
|
|
257
|
+
|
|
258
|
+
```typescript
|
|
259
|
+
Post
|
|
260
|
+
.order({ title: "desc" })
|
|
261
|
+
.order("ranking")
|
|
262
|
+
.all()
|
|
263
|
+
```
|
|
264
|
+
<blockquote class="url">
|
|
265
|
+
<p>/posts?sort=-title,ranking</p>
|
|
266
|
+
</blockquote>
|
|
267
|
+
|
|
268
|
+
## Pagination
|
|
269
|
+
|
|
270
|
+
Use `#per()` to set the limit per page:
|
|
271
|
+
|
|
272
|
+
```typescript
|
|
273
|
+
Post.per(10).all()
|
|
274
|
+
```
|
|
275
|
+
<blockquote class="url">
|
|
276
|
+
<p>/posts?page[size]=10</p>
|
|
277
|
+
</blockquote>
|
|
278
|
+
|
|
279
|
+
Use `#page()` to set the current page:
|
|
280
|
+
|
|
281
|
+
```typescript
|
|
282
|
+
Post.page(5).all()
|
|
283
|
+
```
|
|
284
|
+
<blockquote class="url">
|
|
285
|
+
<p>/posts?page[number]=5</p>
|
|
286
|
+
</blockquote>
|
|
287
|
+
|
|
288
|
+
When chained together (10 per page, the 5th page):
|
|
289
|
+
|
|
290
|
+
```typescript
|
|
291
|
+
Post.page(5).per(10).all()
|
|
292
|
+
```
|
|
293
|
+
<blockquote class="url">
|
|
294
|
+
<p>/posts?page[size]=10&page[number]=5</p>
|
|
295
|
+
</blockquote>
|
|
296
|
+
|
|
297
|
+
## Fieldsets
|
|
298
|
+
|
|
299
|
+
### Sparse Fieldsets
|
|
300
|
+
|
|
301
|
+
Use `#select()` to limit the fields returned by the server:
|
|
302
|
+
|
|
303
|
+
```typescript
|
|
304
|
+
Post.select(['title', 'status']).all()
|
|
305
|
+
```
|
|
306
|
+
<blockquote class="url">
|
|
307
|
+
<p>/posts?fields[posts]=title,status</p>
|
|
308
|
+
</blockquote>
|
|
309
|
+
|
|
310
|
+
When dealing with relationships, it may be easier to pass an object, where the key is the corresponding JSONAPI type. This will be exactly what's sent to the server in `?fields`:
|
|
311
|
+
|
|
312
|
+
```typescript
|
|
313
|
+
Post.select({
|
|
314
|
+
posts: ['title', 'status'],
|
|
315
|
+
comments: ['created_at']
|
|
316
|
+
}).all()
|
|
317
|
+
```
|
|
318
|
+
<blockquote class="url">
|
|
319
|
+
<p>/posts?fields[posts]=title,status&fields[comments]=created_at</p>
|
|
320
|
+
</blockquote>
|
|
321
|
+
|
|
322
|
+
### Extra Fieldsets
|
|
323
|
+
|
|
324
|
+
Use `#selectExtra()` to explicitly request a field that doesn't usually come back (often computationally expensive):
|
|
325
|
+
|
|
326
|
+
```typescript
|
|
327
|
+
Post.selectExtra(['highlights', 'cumulative_ranking']).all()
|
|
328
|
+
```
|
|
329
|
+
<blockquote class="url">
|
|
330
|
+
<p>/posts?extra_fields[posts]=highlights,cumulative_ranking</p>
|
|
331
|
+
</blockquote>
|
|
332
|
+
|
|
333
|
+
Just like the `select` example above, feel free to pass an object specifying the fields for each relationship.
|
|
334
|
+
|
|
335
|
+
## Includes
|
|
336
|
+
|
|
337
|
+
Use `#includes()` to ["sideload"](http://jsonapi.org/format/#fetching-includes) associations:
|
|
338
|
+
|
|
339
|
+
```typescript
|
|
340
|
+
Post.includes("comments").all()
|
|
341
|
+
```
|
|
342
|
+
<blockquote class="url">
|
|
343
|
+
<p>/posts?include=comments</p>
|
|
344
|
+
</blockquote>
|
|
345
|
+
|
|
346
|
+
You can also pass an array of associations:
|
|
347
|
+
|
|
348
|
+
```typescript
|
|
349
|
+
Post.includes(["blog", "comments"]).all()
|
|
350
|
+
```
|
|
351
|
+
<blockquote class="url">
|
|
352
|
+
<p>/posts?include=blog,comments</p>
|
|
353
|
+
</blockquote>
|
|
354
|
+
|
|
355
|
+
Or an object for nested associations:
|
|
356
|
+
|
|
357
|
+
```typescript
|
|
358
|
+
Post.includes(["blog", { comments: "author" }]).all()
|
|
359
|
+
```
|
|
360
|
+
<blockquote class="url">
|
|
361
|
+
<p>/posts?include=blog,comments.author</p>
|
|
362
|
+
</blockquote>
|
|
363
|
+
|
|
364
|
+
## Nested Queries
|
|
365
|
+
|
|
366
|
+
We can nest all read operations at any level of the graph. Let's say we wanted to fetch all `Post`s and their `Comment`s...but only return comments that are `active`, sorted by `created_at` descending. We can create a `Comment` scope as normal, then `#merge()` it into our `Post` scope:
|
|
367
|
+
|
|
368
|
+
```typescript
|
|
369
|
+
let commentScope = Comment
|
|
370
|
+
.where({ active: true })
|
|
371
|
+
.order({ created_at: "desc" })
|
|
372
|
+
Post
|
|
373
|
+
.includes("comments")
|
|
374
|
+
.merge({ comments: commentScope })
|
|
375
|
+
.all()
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
```javascript
|
|
379
|
+
var commentScope = Comment
|
|
380
|
+
.where({ active: true })
|
|
381
|
+
.order({ created_at: "desc" })
|
|
382
|
+
Post
|
|
383
|
+
.includes("comments")
|
|
384
|
+
.merge({ comments: commentScope })
|
|
385
|
+
.all()
|
|
386
|
+
```
|
|
387
|
+
<blockquote class="url">
|
|
388
|
+
<p>/posts?include=comments&filter[comments][active]=true&sort=-comments.active</p>
|
|
389
|
+
</blockquote>
|
|
390
|
+
|
|
391
|
+
Because this can get verbose, it's often desirable to store it on the class:
|
|
392
|
+
|
|
393
|
+
```typescript
|
|
394
|
+
class Comment extends ApplicationRecord {
|
|
395
|
+
// ... code ...
|
|
396
|
+
static recent() {
|
|
397
|
+
return this
|
|
398
|
+
.where({ active: true })
|
|
399
|
+
.order({ created_at: "desc" })
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
Post.merge({ comments: Comment.recent() }).all()
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
```javascript
|
|
407
|
+
const Comment = ApplicationRecord.extend({
|
|
408
|
+
// ... code ...
|
|
409
|
+
static: {
|
|
410
|
+
recent: function() {
|
|
411
|
+
return this
|
|
412
|
+
.where({ active: true })
|
|
413
|
+
.order({ created_at: "desc" })
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
})
|
|
417
|
+
|
|
418
|
+
Post
|
|
419
|
+
.includes("comments")
|
|
420
|
+
.merge({ comments: Comment.recent() })
|
|
421
|
+
.all()
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
Any number of scopes can be merged in. Just remember to `#include()` and `#merge()` relationship names **as the server understands them**:
|
|
425
|
+
|
|
426
|
+
```typescript
|
|
427
|
+
class Dog extends ApplicationRecord {
|
|
428
|
+
@BelongsTo() person: Person
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
// We've modeled this as Dog > person in javascript
|
|
432
|
+
// And Person is jsonapiType "people"
|
|
433
|
+
// But the server defined the relationship as "owner"
|
|
434
|
+
Dog.includes("owner").merge({ owner: Person.limitedFields() })
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
```javascript
|
|
438
|
+
const Dog = ApplicationRecord.extend({
|
|
439
|
+
// ... code ...
|
|
440
|
+
methods: {
|
|
441
|
+
person: belongsTo()
|
|
442
|
+
}
|
|
443
|
+
})
|
|
444
|
+
|
|
445
|
+
// We've modeled this as Dog > person in javascript
|
|
446
|
+
// And Person is jsonapiType "people"
|
|
447
|
+
// But the server defined the relationship as "owner"
|
|
448
|
+
Dog.includes("owner").merge({ owner: Person.limitedFields() })
|
|
449
|
+
```
|
|
450
|
+
|
|
451
|
+
## Statistics
|
|
452
|
+
|
|
453
|
+
Use `#stats()` to request statistics. Access stats within `meta`:
|
|
454
|
+
|
|
455
|
+
```typescript
|
|
456
|
+
let { data } = await Post.stats({ total: "count" }).all()
|
|
457
|
+
data.meta.stats.total.count // the total count
|
|
458
|
+
```
|
|
459
|
+
|
|
460
|
+
```javascript
|
|
461
|
+
Post.stats({ total: "count" }).all().then(function(response) {
|
|
462
|
+
response.meta.stats.total.count // the total count
|
|
463
|
+
})
|
|
464
|
+
```
|
|
465
|
+
<blockquote class="url">
|
|
466
|
+
<p>/posts?stats[total]=count</p>
|
|
467
|
+
</blockquote>
|
|
468
|
+
|
|
469
|
+
Stats are always independent of pagination. If you request the total count, you'll get the total count even if you're limiting to 10 per page. This means to get **only** statistics - avoid returning `Post` instances altogether - request `0` results per page:
|
|
470
|
+
|
|
471
|
+
```typescript
|
|
472
|
+
let { data } = await Post.per(0)stats({ total: "count" }).all()
|
|
473
|
+
data.meta.stats.total.count // the total count
|
|
474
|
+
```
|
|
475
|
+
|
|
476
|
+
```javascript
|
|
477
|
+
Post
|
|
478
|
+
.per(0)
|
|
479
|
+
.stats({ total: "count" })
|
|
480
|
+
.all().then(function(response) {
|
|
481
|
+
response.meta.stats.total.count // the total count
|
|
482
|
+
})
|
|
483
|
+
```
|
|
484
|
+
<blockquote class="url">
|
|
485
|
+
<p>/posts?stats[total]=count&page[size]=0</p>
|
|
486
|
+
</blockquote>
|
|
487
|
+
|
|
488
|
+
<h2 id="next">
|
|
489
|
+
<a href="/js/writes">
|
|
490
|
+
NEXT:
|
|
491
|
+
<small>Writes</small>
|
|
492
|
+
»
|
|
493
|
+
</a>
|
|
494
|
+
</h2>
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: 'State Syncing'
|
|
3
|
+
sidebar_position: 8
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
### State Syncing
|
|
7
|
+
|
|
8
|
+
You may have encountered state management libraries like
|
|
9
|
+
[Redux](https://redux.js.org) or [Vuex](https://vuex.vuejs.org/en/intro.html). These are fantastic libraries, but their usefulness is lessened with Spraypaint. As a full-fledged model layer, Spraypaint manages state for you, automatically.
|
|
10
|
+
|
|
11
|
+
If you opt-in to this feature:
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
ApplicationRecord.sync = true
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Instances will sync up whenever the server tells us about updated state.
|
|
18
|
+
Consider the scenario where an instance is initially loaded, then separately polled in the background:
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
let person = (await Person.find(1)).data
|
|
22
|
+
|
|
23
|
+
let poll = () => {
|
|
24
|
+
await Person.find(1)
|
|
25
|
+
setTimeout(poll, 1000)
|
|
26
|
+
}
|
|
27
|
+
poll()
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
```javascript
|
|
31
|
+
Person.find(1).then(function(response) {
|
|
32
|
+
var person = response.data;
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
var poll = function() {
|
|
36
|
+
Person.find(1);
|
|
37
|
+
setTimeout(poll, 1000);
|
|
38
|
+
}
|
|
39
|
+
poll()
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Our `poll()` function **never assigns or updates `person`**. But if the server returns an updated `name` attribute, **`person.name` will be automatically updated**. This is true even if `person.name`
|
|
43
|
+
is bound in 17 different nested components.
|
|
44
|
+
|
|
45
|
+
Instances can still update their attributes independently - we only sync
|
|
46
|
+
when the server returns updated data:
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
let instanceA = (await Person.find(1)).data
|
|
50
|
+
let instanceB = (await Person.find(1)).data
|
|
51
|
+
|
|
52
|
+
instanceA.name // "Jane"
|
|
53
|
+
instanceB.name // "Jane"
|
|
54
|
+
|
|
55
|
+
instanceB.name = "Silvia"
|
|
56
|
+
instanceA.name // "Jane"
|
|
57
|
+
instanceB.name // "Silvia"
|
|
58
|
+
|
|
59
|
+
await instanceB.save()
|
|
60
|
+
instanceA.name // "Silvia"
|
|
61
|
+
instanceB.name // "Silvia"
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
```javascript
|
|
65
|
+
var instanceA, instanceB;
|
|
66
|
+
Person.find(1).then(function(response) {
|
|
67
|
+
instanceA = response.data;
|
|
68
|
+
});
|
|
69
|
+
Person.find(1).then(function(response) {
|
|
70
|
+
instanceB = response.data;
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
instanceA.name // "Jane"
|
|
74
|
+
instanceB.name // "Jane"
|
|
75
|
+
|
|
76
|
+
instanceB.name = "Silvia"
|
|
77
|
+
instanceA.name // "Jane"
|
|
78
|
+
instanceB.name // "Silvia"
|
|
79
|
+
|
|
80
|
+
instanceB.save().then(function() {
|
|
81
|
+
instanceA.name // "Silvia"
|
|
82
|
+
instanceB.name // "Silvia"
|
|
83
|
+
});
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
#### Gotchas
|
|
87
|
+
|
|
88
|
+
Under the hood, instances are listening for updates from a central data
|
|
89
|
+
store. This means that you'll want to remove listeners whenever you no
|
|
90
|
+
longer need the instance - otherwise it will never be garbage collected
|
|
91
|
+
properly. To remove a listener:
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
instance.unlisten()
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
In practice, when developing in a SPA, you'll want to `#unlisten()`
|
|
98
|
+
whenever a view is destroyed and model instances no longer need to be referenced. If
|
|
99
|
+
you are using VueJS, this is done automatically by adding [spraypaint-vue](https://github.com/graphiti-api/spraypaint-vue)
|
|
100
|
+
to your application.
|