cacheql 0.1.0 → 0.1.1
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/README.md +126 -3
- data/cacheql.gemspec +1 -1
- data/lib/cacheql/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4b63b557dc02328918c18828fc532653ff77096d
|
4
|
+
data.tar.gz: 9309e2b0986da06f49a2ce6ad90fb88928d2a57e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cfd0715295b686bebf91fddd605e1c1193a7ca2fc0ec9bbb63559231c9f1a274bf169f18571caa481e417c9b909ae79a41033cc1b13e1314cafeac00f9dcaf22
|
7
|
+
data.tar.gz: 0cf778191eb26d2bb6af7a1ec8978fbebc608101fd40dc15d50d92cf5ccce20c4ecc6c5d029c7e1588880cfcb4ddcf8f54e82077844e27dcdba0fea5bd32bca2
|
data/README.md
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
# CacheQL
|
2
2
|
|
3
|
-
|
3
|
+
Need to cache and instrument your GraphQL code in Ruby? Look no further!
|
4
4
|
|
5
|
-
|
5
|
+
This is a collection of utilities for [graphql-ruby](http://graphql-ruby.org)
|
6
|
+
that were collected from various places on GitHub + docs.
|
7
|
+
|
8
|
+
This code was extracted from [Chatterbug](https://chatterbug.com). Check out the talk from [Railsconf 2018](https://speakerdeck.com/qrush/the-graphql-way-a-new-path-for-json-apis) about this gem!
|
6
9
|
|
7
10
|
## Installation
|
8
11
|
|
@@ -22,7 +25,127 @@ Or install it yourself as:
|
|
22
25
|
|
23
26
|
## Usage
|
24
27
|
|
25
|
-
|
28
|
+
There's four major parts to this gem:
|
29
|
+
|
30
|
+
### Cache helpers
|
31
|
+
|
32
|
+
Need to cache a single resolve function? Wrap it in `CacheQL`:
|
33
|
+
|
34
|
+
``` ruby
|
35
|
+
resolve CacheQL -> (obj, args, ctx) {
|
36
|
+
# run expensive operation
|
37
|
+
# this resolve function's result will be cached on obj.cache_key
|
38
|
+
}
|
39
|
+
```
|
40
|
+
|
41
|
+
Want to cache the entire response after GraphQL has generated it? Try this in
|
42
|
+
your controller:
|
43
|
+
|
44
|
+
``` ruby
|
45
|
+
def execute
|
46
|
+
# other graphql stuff...
|
47
|
+
|
48
|
+
FIELDS = %w(users curriculum)
|
49
|
+
render json: CacheQL.fetch(FIELDS, query, variables) { |document|
|
50
|
+
YourSchema.execute(
|
51
|
+
document: document,
|
52
|
+
variables: variables,
|
53
|
+
context: { },
|
54
|
+
operation_name: params[:operationName]).to_h
|
55
|
+
}
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
This will cache the entire response when the query includes `FIELDS`.
|
60
|
+
|
61
|
+
### Loaders
|
62
|
+
|
63
|
+
These are all based off of community-written examples using [graphql-batch](https://github.com/Shopify/graphql-batch).
|
64
|
+
These will reduce N+1 queries in your GraphQL code.
|
65
|
+
|
66
|
+
Before using loaders, you'll have to [use `GraphQL::Batch`](https://github.com/Shopify/graphql-batch#basic-usage) as a plugin in your schema:
|
67
|
+
|
68
|
+
``` ruby
|
69
|
+
YourSchema = GraphQL::Schema.define do
|
70
|
+
query YourQueryType
|
71
|
+
|
72
|
+
use GraphQL::Batch
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
76
|
+
Batch up `belongs_to` calls:
|
77
|
+
|
78
|
+
``` ruby
|
79
|
+
# when obj has a belongs_to :language
|
80
|
+
|
81
|
+
resolve -> (obj, args, ctx) {
|
82
|
+
CacheQL::RecordLoader.for(Language).load(obj.language_id)
|
83
|
+
}
|
84
|
+
```
|
85
|
+
|
86
|
+
Batch up `belongs_to polymorphic: true` calls:
|
87
|
+
|
88
|
+
``` ruby
|
89
|
+
# when obj has a belongs_to :respondable, polymorphic: true
|
90
|
+
|
91
|
+
resolve -> (obj, args, ctx) {
|
92
|
+
CacheQL::PolymorphicKeyLoader.for(Response, :respondable).load(obj.respondable)
|
93
|
+
}
|
94
|
+
```
|
95
|
+
|
96
|
+
Batch up entire associations:
|
97
|
+
|
98
|
+
``` ruby
|
99
|
+
# when obj has_many :clozes
|
100
|
+
|
101
|
+
resolve -> (obj, args, ctx) {
|
102
|
+
CacheQL::AssociationLoader.for(obj.class, :clozes).load(obj)
|
103
|
+
}
|
104
|
+
```
|
105
|
+
|
106
|
+
### Logging
|
107
|
+
|
108
|
+
Want to get your GraphQL fields logging locally? In your controller, add:
|
109
|
+
|
110
|
+
|
111
|
+
``` ruby
|
112
|
+
around_action :log_field_instrumentation
|
113
|
+
|
114
|
+
private
|
115
|
+
|
116
|
+
def log_field_instrumentation(&block)
|
117
|
+
CacheQL::FieldInstrumentation.log(&block)
|
118
|
+
end
|
119
|
+
```
|
120
|
+
|
121
|
+
This will then spit out timing logs for each field run during each request.
|
122
|
+
For example:
|
123
|
+
|
124
|
+
```
|
125
|
+
[CacheQL::Tracing] User.displayLanguage took 7.591ms
|
126
|
+
[CacheQL::Tracing] User.createdAt took 0.117ms
|
127
|
+
[CacheQL::Tracing] User.intercomHash took 0.095ms
|
128
|
+
[CacheQL::Tracing] User.id took 0.09ms
|
129
|
+
[CacheQL::Tracing] User.friendlyTimezone took 0.087ms
|
130
|
+
[CacheQL::Tracing] User.utmContent took 0.075ms
|
131
|
+
[GraphQL::Tracing] User.timezone took 0.048ms
|
132
|
+
[CacheQL::Tracing] User.email took 0.046ms
|
133
|
+
[CacheQL::Tracing] User.name took 0.042ms
|
134
|
+
[CacheQL::Tracing] Query.currentUser took 0.041ms
|
135
|
+
```
|
136
|
+
|
137
|
+
### Instrumentation
|
138
|
+
|
139
|
+
This gem includes an instrumenter for [Scout](https://scoutapp.com) that will
|
140
|
+
show the timing breakdown of each field in your metrics. Assuming you have Scout
|
141
|
+
setup, add to your schema:
|
142
|
+
|
143
|
+
|
144
|
+
``` ruby
|
145
|
+
YourSchema = GraphQL::Schema.define do
|
146
|
+
instrument :field, CacheQL::FieldInstrumentation.new(ScoutApm::Tracer)
|
147
|
+
end
|
148
|
+
```
|
26
149
|
|
27
150
|
## Development
|
28
151
|
|
data/cacheql.gemspec
CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
|
|
11
11
|
|
12
12
|
spec.summary = %q{Various GraphQL caching / instrumentation tools}
|
13
13
|
spec.description = spec.summary
|
14
|
-
spec.homepage = "https://github.com/
|
14
|
+
spec.homepage = "https://github.com/chatterbugapp/cacheql"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
17
17
|
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
data/lib/cacheql/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cacheql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Quaranto
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-05-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: graphql-batch
|
@@ -133,7 +133,7 @@ files:
|
|
133
133
|
- lib/cacheql/record_loader.rb
|
134
134
|
- lib/cacheql/resolve_wrapper.rb
|
135
135
|
- lib/cacheql/version.rb
|
136
|
-
homepage: https://github.com/
|
136
|
+
homepage: https://github.com/chatterbugapp/cacheql
|
137
137
|
licenses:
|
138
138
|
- MIT
|
139
139
|
metadata: {}
|