graphql_migrate_execution 0.0.2 → 1.0.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/bin/graphql_migrate_execution +5 -6
- data/lib/graphql_migrate_execution/action.rb +33 -24
- data/lib/graphql_migrate_execution/dataloader_all.rb +39 -14
- data/lib/graphql_migrate_execution/dataloader_batch.rb +66 -4
- data/lib/graphql_migrate_execution/dataloader_manual.rb +2 -5
- data/lib/graphql_migrate_execution/dataloader_shorthand.rb +20 -7
- data/lib/graphql_migrate_execution/do_nothing.rb +1 -1
- data/lib/graphql_migrate_execution/fallback_value.rb +39 -0
- data/lib/graphql_migrate_execution/field_definition.rb +32 -4
- data/lib/graphql_migrate_execution/hash_key.rb +12 -0
- data/lib/graphql_migrate_execution/implicit.rb +22 -6
- data/lib/graphql_migrate_execution/migration.rb +45 -0
- data/lib/graphql_migrate_execution/not_implemented.rb +1 -1
- data/lib/graphql_migrate_execution/resolve_batch.rb +16 -0
- data/lib/graphql_migrate_execution/resolve_each.rb +7 -7
- data/lib/graphql_migrate_execution/resolve_static.rb +8 -7
- data/lib/graphql_migrate_execution/resolver_method.rb +80 -5
- data/lib/graphql_migrate_execution/strategy.rb +111 -25
- data/lib/graphql_migrate_execution/type_definition.rb +14 -1
- data/lib/graphql_migrate_execution/unsupported_current_path.rb +8 -0
- data/lib/graphql_migrate_execution/unsupported_extra.rb +8 -0
- data/lib/graphql_migrate_execution/version.rb +1 -1
- data/lib/graphql_migrate_execution/visitor.rb +54 -18
- data/lib/graphql_migrate_execution.rb +7 -30
- data/readme.md +158 -4
- metadata +8 -5
- data/lib/graphql_migrate_execution/add_future.rb +0 -9
- data/lib/graphql_migrate_execution/analyze.rb +0 -30
- data/lib/graphql_migrate_execution/remove_legacy.rb +0 -9
data/readme.md
CHANGED
|
@@ -25,15 +25,169 @@ Inspect the files matched by `glob` and ...
|
|
|
25
25
|
|
|
26
26
|
Options:
|
|
27
27
|
|
|
28
|
-
--migrate Update the files with future-
|
|
28
|
+
--migrate Update the files with future-compatible configuration
|
|
29
29
|
--cleanup Remove resolver instance methods for GraphQL-Ruby's old runtime
|
|
30
|
-
--
|
|
31
|
-
--implicit MODE
|
|
32
|
-
--only PATTERN Only analyze or update fields whose path (`Type.field`) matches /PATTERN/
|
|
30
|
+
--dry-run Don't actually modify files
|
|
31
|
+
--implicit [MODE] Handle implicit field resolution this way (ignore / hash_key / hash_key_string)
|
|
33
32
|
```
|
|
34
33
|
|
|
34
|
+
## Supported Field Resolution Patterns
|
|
35
|
+
|
|
36
|
+
Check out the docs for refactors implemented by this tool:
|
|
37
|
+
|
|
38
|
+
- Dataloader-based fields:
|
|
39
|
+
- [`DataloaderShorthand`](https://rmosolgo.github.io/graphql_migrate_execution/GraphqlMigrateExecution/DataloaderShorthand.html): use the new `dataload: ...` field configuration shorthand
|
|
40
|
+
- [`DataloaderAll`](https://rmosolgo.github.io/graphql_migrate_execution/GraphqlMigrateExecution/DataloaderAll.html): use a `dataload_all(...)` call to fetch data for a batch of objects
|
|
41
|
+
- [`DataloaderBatch`](https://rmosolgo.github.io/graphql_migrate_execution/GraphqlMigrateExecution/DataloaderBatch.html): Fetch a list of results _for each object_ (2-layer list)
|
|
42
|
+
- [`DataloaderManual`](https://rmosolgo.github.io/graphql_migrate_execution/GraphqlMigrateExecution/DataloaderManual.html): 💔 Identifies dataloader usage which can't be migrated
|
|
43
|
+
- Migrate method:
|
|
44
|
+
- These identify Ruby code in the method which only uses `context` and `object` and migrates it to a suitable class method. Then, it updates the instance method to call the new class method and adds the suitable future-compatible config.
|
|
45
|
+
- [`ResolveBatch`](https://rmosolgo.github.io/graphql_migrate_execution/GraphqlMigrateExecution/ResolveBatch.html)
|
|
46
|
+
- [`ResolveEach`](https://rmosolgo.github.io/graphql_migrate_execution/GraphqlMigrateExecution/ResolveEach.html)
|
|
47
|
+
- [`ResolveStatic`](https://rmosolgo.github.io/graphql_migrate_execution/GraphqlMigrateExecution/ResolveStatic.html)
|
|
48
|
+
- 💔 Not migratable:
|
|
49
|
+
- [`NotImplemented`](https://rmosolgo.github.io/graphql_migrate_execution/GraphqlMigrateExecution/NotImplemented.html): This field couldn't be matched to a refactor
|
|
50
|
+
- [`UnsupportedCurrentPath`](https://rmosolgo.github.io/graphql_migrate_execution/GraphqlMigrateExecution/UnsupportedCurrentPath.html): uses `context[:current_path]` which isn't supported anymore - [`UnsupportedExtra`](https://rmosolgo.github.io/graphql_migrate_execution/GraphqlMigrateExecution/UnsupportedExtra.html): as at least one `extras: ...` configuration which isn't supported anymore
|
|
51
|
+
- Configuration:
|
|
52
|
+
- [`DoNothing`](https://rmosolgo.github.io/graphql_migrate_execution/GraphqlMigrateExecution/DoNothing.html): Already includes future-compatible configuration
|
|
53
|
+
- [`HashKey`](https://rmosolgo.github.io/graphql_migrate_execution/GraphqlMigrateExecution/HashKey.html): Can be migrated using `hash_key:` (especially useful for Resolvers and Mutations)
|
|
54
|
+
- [`Implicit`](https://rmosolgo.github.io/graphql_migrate_execution/GraphqlMigrateExecution/Implicit.html): ⚠️ GraphQL-Ruby's default field resolution is changing, see the doc
|
|
55
|
+
|
|
56
|
+
## Unsupported Field Resolution Patterns
|
|
57
|
+
|
|
58
|
+
Here are a few fields in my app that this tool didn't handle automatically, along with my manual migrations:
|
|
59
|
+
|
|
60
|
+
- __Working with a dataloaded value__:
|
|
61
|
+
|
|
62
|
+
This resolver called arbitrary code _after_ using Dataloader:
|
|
63
|
+
|
|
64
|
+
```ruby
|
|
65
|
+
field :is_locked_to_viewer, Boolean, null: false
|
|
66
|
+
|
|
67
|
+
def is_locked_to_viewer
|
|
68
|
+
status = dataload(Sources::GrowthTaskStatusForUserSource, context[:current_user], object)
|
|
69
|
+
status == :LOCKED
|
|
70
|
+
end
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
I _could_ have handled this by refactoring the dataload call to return `true|false`. Then it could have been auto-migrated. Instead, I migrated it like this:
|
|
74
|
+
|
|
75
|
+
```ruby
|
|
76
|
+
field :is_locked_to_viewer, Boolean, null: false, resolve_batch: true
|
|
77
|
+
|
|
78
|
+
def self.is_locked_to_viewer(objects, context)
|
|
79
|
+
statuses = context.dataload_all(Sources::GrowthTaskStatusForUserSource, context[:current_user], objects)
|
|
80
|
+
statuses.map { |s| s == :LOCKED }
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def is_locked_to_viewer
|
|
84
|
+
self.class.is_locked_to_viewer([ object ], context).first
|
|
85
|
+
end
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
- __Conditional dataloader call__:
|
|
89
|
+
|
|
90
|
+
This field only called dataloader in some cases:
|
|
91
|
+
|
|
92
|
+
```ruby
|
|
93
|
+
field :viewer_growth_task_submission, GrowthTaskSubmissionType
|
|
94
|
+
|
|
95
|
+
def viewer_growth_task_submission
|
|
96
|
+
if object.frequency.present?
|
|
97
|
+
# TODO should not include a recurring submission whose duration has passed
|
|
98
|
+
nil
|
|
99
|
+
else
|
|
100
|
+
context.dataloader.with(Sources::GrowthTaskForViewerSource, context[:current_user]).request(object.id)
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
It _could_ have been auto-migrated if I made two refactors:
|
|
106
|
+
|
|
107
|
+
- Update the Source to receive `object` instead of `object.id`
|
|
108
|
+
- Update the Source's `#fetch` to return `nil` based on `object.frequency.present?`
|
|
109
|
+
|
|
110
|
+
But I didn't do that. Instead, I migrated it manually:
|
|
111
|
+
|
|
112
|
+
```ruby
|
|
113
|
+
field :viewer_growth_task_submission, GrowthTaskSubmissionType, resolve_batch: true
|
|
114
|
+
|
|
115
|
+
def self.viewer_growth_task_submission(objects, context)
|
|
116
|
+
requests = objects.map do |object|
|
|
117
|
+
if object.frequency.present?
|
|
118
|
+
# TODO should not include a recurring submission whose duration has passed
|
|
119
|
+
nil
|
|
120
|
+
else
|
|
121
|
+
context.dataloader.with(Sources::GrowthTaskForViewerSource, context[:current_user]).request(object.id)
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
requests.map { |l| l&.load }
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def viewer_growth_task_submission
|
|
128
|
+
self.class.viewer_growth_task_submission([ object ], context).first
|
|
129
|
+
end
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
- __Resolver that calls another resolver:__
|
|
133
|
+
|
|
134
|
+
The tool just gives up when it sees calls on `self`. It didn't handle this:
|
|
135
|
+
|
|
136
|
+
```ruby
|
|
137
|
+
field :current_user, Types::UserType
|
|
138
|
+
|
|
139
|
+
def current_user
|
|
140
|
+
context[:current_user]
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
field :unread_notification_count, Integer, null: false
|
|
144
|
+
|
|
145
|
+
def unread_notification_count
|
|
146
|
+
# vvvvvvvvv Calls the resolver method above
|
|
147
|
+
current_user ? current_user.notification_events.unread.count : 0
|
|
148
|
+
end
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
I migrated it manually:
|
|
152
|
+
|
|
153
|
+
```ruby
|
|
154
|
+
field :unread_notification_count, Integer, null: false, resolve_static: true
|
|
155
|
+
|
|
156
|
+
def self.unread_notification_count(context)
|
|
157
|
+
if (cu = current_user(context))
|
|
158
|
+
cu.notification_events.unread.count
|
|
159
|
+
else
|
|
160
|
+
0
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def unread_notification_count
|
|
165
|
+
self.class.unread_notification_count(context)
|
|
166
|
+
end
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
- __Single-line method definition__:
|
|
170
|
+
|
|
171
|
+
The tool's heavy-handed Ruby source generation botched this:
|
|
172
|
+
|
|
173
|
+
```ruby
|
|
174
|
+
field :growth_levels, Types::GrowthLevelType.connection_type, null: false, resolve_each: true
|
|
175
|
+
def growth_levels; object.growth_levels.by_sequence; end;
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
This tool could be improved to properly handle single-line methods -- open an issue if you need this.
|
|
179
|
+
|
|
35
180
|
## Develop
|
|
36
181
|
|
|
37
182
|
```
|
|
38
183
|
bundle exec rake test # TEST=test/...
|
|
39
184
|
```
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
## TODO
|
|
188
|
+
|
|
189
|
+
- [ ] Interfaces: use a `resolver_methods` block with instance methods
|
|
190
|
+
- [ ] `@object` is not migrated, only `object` is
|
|
191
|
+
- [ ] `**kwargs` is not correctly migrated into `resolve_static` methods
|
|
192
|
+
- [ ] Does `--cleanup` work on my app? I haven't run it yet.
|
|
193
|
+
- [ ] Doesn't support `def ... =`-style single-line methods
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: graphql_migrate_execution
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 1.0.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Robert Mosolgo
|
|
8
8
|
bindir: bin
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date: 2026-
|
|
10
|
+
date: 2026-05-25 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
12
|
- !ruby/object:Gem::Dependency
|
|
13
13
|
name: irb
|
|
@@ -91,22 +91,25 @@ files:
|
|
|
91
91
|
- bin/graphql_migrate_execution
|
|
92
92
|
- lib/graphql_migrate_execution.rb
|
|
93
93
|
- lib/graphql_migrate_execution/action.rb
|
|
94
|
-
- lib/graphql_migrate_execution/add_future.rb
|
|
95
|
-
- lib/graphql_migrate_execution/analyze.rb
|
|
96
94
|
- lib/graphql_migrate_execution/dataloader_all.rb
|
|
97
95
|
- lib/graphql_migrate_execution/dataloader_batch.rb
|
|
98
96
|
- lib/graphql_migrate_execution/dataloader_manual.rb
|
|
99
97
|
- lib/graphql_migrate_execution/dataloader_shorthand.rb
|
|
100
98
|
- lib/graphql_migrate_execution/do_nothing.rb
|
|
99
|
+
- lib/graphql_migrate_execution/fallback_value.rb
|
|
101
100
|
- lib/graphql_migrate_execution/field_definition.rb
|
|
101
|
+
- lib/graphql_migrate_execution/hash_key.rb
|
|
102
102
|
- lib/graphql_migrate_execution/implicit.rb
|
|
103
|
+
- lib/graphql_migrate_execution/migration.rb
|
|
103
104
|
- lib/graphql_migrate_execution/not_implemented.rb
|
|
104
|
-
- lib/graphql_migrate_execution/
|
|
105
|
+
- lib/graphql_migrate_execution/resolve_batch.rb
|
|
105
106
|
- lib/graphql_migrate_execution/resolve_each.rb
|
|
106
107
|
- lib/graphql_migrate_execution/resolve_static.rb
|
|
107
108
|
- lib/graphql_migrate_execution/resolver_method.rb
|
|
108
109
|
- lib/graphql_migrate_execution/strategy.rb
|
|
109
110
|
- lib/graphql_migrate_execution/type_definition.rb
|
|
111
|
+
- lib/graphql_migrate_execution/unsupported_current_path.rb
|
|
112
|
+
- lib/graphql_migrate_execution/unsupported_extra.rb
|
|
110
113
|
- lib/graphql_migrate_execution/version.rb
|
|
111
114
|
- lib/graphql_migrate_execution/visitor.rb
|
|
112
115
|
- readme.md
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
require "irb"
|
|
3
|
-
module GraphqlMigrateExecution
|
|
4
|
-
class Analyze < Action
|
|
5
|
-
def run
|
|
6
|
-
super
|
|
7
|
-
message = "Found #{@total_field_definitions} field definitions:".dup
|
|
8
|
-
|
|
9
|
-
@field_definitions_by_strategy.each do |strategy_class, definitions|
|
|
10
|
-
message << "\n\n#{color("#{color(strategy_class.name.split("::").last, strategy_class.color)} (#{definitions.size})", :BOLD)}:\n"
|
|
11
|
-
if !@migration.skip_description
|
|
12
|
-
message << "\n#{strategy_class::DESCRIPTION.split("\n").map { |l| l.length > 0 ? " #{l}" : l }.join("\n")}\n"
|
|
13
|
-
end
|
|
14
|
-
max_path = definitions.map { |f| f.path.size }.max + 2
|
|
15
|
-
definitions.each do |field_defn|
|
|
16
|
-
name = field_defn.path.ljust(max_path)
|
|
17
|
-
message << "\n - #{name} (#{field_defn.resolve_mode.inspect} -> #{field_defn.resolve_mode_key.inspect}) @ #{@path}:#{field_defn.source_line}"
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
message
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
private
|
|
25
|
-
|
|
26
|
-
def color(str, color_or_colors)
|
|
27
|
-
IRB::Color.colorize(str, Array(color_or_colors), colorable: @migration.colorable)
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
end
|