Skip to content

Commit 3f15aab

Browse files
author
Joel Warrington
authored
Update pro encoding documentation to use class based definition
1 parent 63e699d commit 3f15aab

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

guides/pro/encoders.md

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pro: true
2525
Encoders can be created with `Encoder.define { ... }`:
2626

2727
```ruby
28-
MyEncoder = GraphQL::Pro::Encoder.define do
28+
class MyEncoder < GraphQL::Pro::Encoder
2929
key("f411f30...")
3030
# optional:
3131
tag("81ce51c307")
@@ -40,7 +40,7 @@ end
4040
Encrypt cursors by attaching an encrypted encoder to `Schema#cursor_encoder`:
4141

4242
```ruby
43-
MySchema = GraphQL::Schema.define do
43+
class MySchema GraphQL::Schema
4444
cursor_encoder(MyCursorEncoder)
4545
end
4646
```
@@ -55,17 +55,17 @@ If you implement your own connections, you can access the encoder's encryption m
5555
Encrypt IDs by using encoders in `Schema#id_from_object` and `Schema#object_from_id`:
5656

5757
```ruby
58-
MySchema = GraphQL::Schema.define do
59-
id_from_object ->(object, type, ctx) {
58+
class MySchema < GraphQL::Schema
59+
def id_from_object(object, type, ctx)
6060
id_data = "#{object.class.name}/#{object.id}"
6161
MyIDEncoder.encode(id_data)
62-
}
63-
64-
object_from_id ->(id, ctx) {
62+
end
63+
64+
def object_from_id(id, ctx)
6565
id_data = MyIDEncoder.decode(id)
6666
class_name, id = id_data.split("/")
67-
class_name.constantize.find(id)
68-
}
67+
class_name.constantize.find(id)
68+
end
6969
end
7070
```
7171

@@ -77,9 +77,17 @@ You can combine several encoders into a single chain of versioned encoders. Pass
7777

7878
```ruby
7979
# Define some encoders ...
80-
NewSecureEncoder = GraphQL::Pro::Encoder.define { ... }
81-
OldSecureEncoder = GraphQL::Pro::Encoder.define { ... }
82-
LegacyInsecureEncoder = GraphQL::Pro::Encoder.define { ... }
80+
class NewSecureEncoder < GraphQL::Pro::Encoder
81+
# ...
82+
end
83+
84+
class OldSecureEncoder < GraphQL::Pro::Encoder
85+
# ...
86+
end
87+
88+
class LegacyInsecureEncoder < GraphQL::Pro::Encoder
89+
# ...
90+
end
8391

8492
# Then order them by priority:
8593
VersionedEncoder = GraphQL::Pro::Encoder.versioned(
@@ -127,7 +135,7 @@ end
127135
Then attach it to your encoder:
128136

129137
```ruby
130-
MyURLSafeEncoder = GraphQL::Pro::Encoder.define do
138+
class MyURLSafeEncoder < GraphQL::Pro::Encoder
131139
encoder URLSafeEncoder
132140
end
133141
```

0 commit comments

Comments
 (0)