@@ -42,7 +42,7 @@ let future2 = try userLoader.load(key: 2, on: eventLoopGroup)
42
42
let future3 = try userLoader.load (key : 1 , on : eventLoopGroup)
43
43
```
44
44
45
- The example above will only fetch two users, because the user with key ` 1 ` is present twice in the list.
45
+ The example above will only fetch two users, because the user with key ` 1 ` is present twice in the list.
46
46
47
47
### Load multiple keys
48
48
There is also a method to load multiple keys at once
@@ -52,14 +52,14 @@ try userLoader.loadMany(keys: [1, 2, 3], on: eventLoopGroup)
52
52
53
53
### Execution
54
54
By default, a DataLoader will wait for a short time from the moment ` load ` is called to collect keys prior
55
- to running the ` batchLoadFunction ` and completing the ` load ` futures. This is to let keys accumulate and
56
- batch into a smaller number of total requests. This amount of time is configurable using the ` executionPeriod `
55
+ to running the ` batchLoadFunction ` and completing the ` load ` futures. This is to let keys accumulate and
56
+ batch into a smaller number of total requests. This amount of time is configurable using the ` executionPeriod `
57
57
option:
58
58
59
59
``` swift
60
60
let myLoader = DataLoader< String , String > (
61
61
options : DataLoaderOptions (executionPeriod : .milliseconds (50 )),
62
- batchLoadFunction : { keys in
62
+ batchLoadFunction : { keys in
63
63
self .someBatchLoader (keys : keys).map { DataLoaderFutureValue.success ($0 ) }
64
64
}
65
65
)
@@ -68,10 +68,10 @@ let myLoader = DataLoader<String, String>(
68
68
Longer execution periods reduce the number of total data requests, but also reduce the responsiveness of the
69
69
` load ` futures.
70
70
71
- If desired, you can manually execute the ` batchLoadFunction ` and complete the futures at any time, using the
71
+ If desired, you can manually execute the ` batchLoadFunction ` and complete the futures at any time, using the
72
72
` .execute() ` method.
73
73
74
- Scheduled execution can be disabled by setting ` executionPeriod ` to ` nil ` , but be careful - you * must* call ` .execute() `
74
+ Scheduled execution can be disabled by setting ` executionPeriod ` to ` nil ` , but be careful - you * must* call ` .execute() `
75
75
manually in this case. Otherwise, the futures will never complete!
76
76
77
77
### Disable batching
@@ -80,10 +80,10 @@ In this case, the `batchLoadFunction` will be invoked immediately when a key is
80
80
81
81
82
82
## Caching 💰
83
- DataLoader provides a memoization cache. After ` .load() ` is called with a key, the resulting value is cached
83
+ DataLoader provides a memoization cache. After ` .load() ` is called with a key, the resulting value is cached
84
84
for the lifetime of the DataLoader object. This eliminates redundant loads.
85
85
86
- In addition to relieving pressure on your data storage, caching results also creates fewer objects which may
86
+ In addition to relieving pressure on your data storage, caching results also creates fewer objects which may
87
87
relieve memory pressure on your application:
88
88
89
89
``` swift
@@ -134,15 +134,15 @@ userLoader.load(key: 4, on: eventLoopGroup)
134
134
135
135
### Caching Errors
136
136
137
- If a batch load fails (that is, a batch function throws or returns a DataLoaderFutureValue.failure(Error)),
137
+ If a batch load fails (that is, a batch function throws or returns a DataLoaderFutureValue.failure(Error)),
138
138
then the requested values will not be cached. However if a batch
139
139
function returns an ` Error ` instance for an individual value, that ` Error ` will
140
140
be cached to avoid frequently loading the same ` Error ` .
141
141
142
142
In some circumstances you may wish to clear the cache for these individual Errors:
143
143
144
144
``` swift
145
- userLoader.load (key : 1 , on : eventLoopGroup).whenFailure { error in
145
+ userLoader.load (key : 1 , on : eventLoopGroup).whenFailure { error in
146
146
if (/* determine if should clear error */ ) {
147
147
userLoader.clear (key : 1 );
148
148
}
@@ -167,7 +167,7 @@ For example:
167
167
``` swift
168
168
let myLoader = DataLoader< String , String > (
169
169
options : DataLoaderOptions (cachingEnabled : false ),
170
- batchLoadFunction : { keys in
170
+ batchLoadFunction : { keys in
171
171
self .someBatchLoader (keys : keys).map { DataLoaderFutureValue.success ($0 ) }
172
172
}
173
173
)
@@ -195,7 +195,7 @@ let myLoader = DataLoader<String, String>(batchLoadFunction: { keys in
195
195
## Using with GraphQL 🎀
196
196
197
197
DataLoader pairs nicely well with [ GraphQL] ( https://github.com/GraphQLSwift/GraphQL ) and
198
- [ Graphiti] ( https://github.com/GraphQLSwift/Graphiti ) . GraphQL fields are designed to be
198
+ [ Graphiti] ( https://github.com/GraphQLSwift/Graphiti ) . GraphQL fields are designed to be
199
199
stand-alone functions. Without a caching or batching mechanism,
200
200
it's easy for a naive GraphQL server to issue new database requests each time a
201
201
field is resolved.
@@ -222,7 +222,7 @@ Consider the following GraphQL request:
222
222
Naively, if ` me ` , ` bestFriend ` and ` friends ` each need to request the backend,
223
223
there could be at most 12 database requests!
224
224
225
- By using DataLoader, we could batch our requests to a ` User ` type, and
225
+ By using DataLoader, we could batch our requests to a ` User ` type, and
226
226
only require at most 4 database requests, and possibly fewer if there are cache hits.
227
227
Here's a full example using Graphiti:
228
228
@@ -232,11 +232,11 @@ struct User : Codable {
232
232
let name: String
233
233
let bestFriendID: Int
234
234
let friendIDs: [Int ]
235
-
235
+
236
236
func getBestFriend (context : UserContext, arguments : NoArguments, group : EventLoopGroup) throws -> EventLoopFuture<User> {
237
237
return try context.userLoader .load (key : user.bestFriendID , on : group)
238
238
}
239
-
239
+
240
240
struct FriendArguments {
241
241
first: Int
242
242
}
@@ -273,7 +273,7 @@ struct UserAPI : API {
273
273
Argument (" first" , at : .\first)
274
274
}
275
275
}
276
-
276
+
277
277
Query {
278
278
Field (" me" , at : UserResolver.hero , as : User.self )
279
279
}
@@ -301,6 +301,6 @@ swiftformat .
301
301
302
302
## Acknowledgements 👏
303
303
304
- This library is entirely a Swift version of Facebooks [ DataLoader] ( https://github.com/facebook/dataloader ) .
305
- Developed by [ Lee Byron] ( https://github.com/leebyron ) and [ Nicholas Schrock] ( https://github.com/schrockn )
304
+ This library is entirely a Swift version of Facebooks [ DataLoader] ( https://github.com/facebook/dataloader ) .
305
+ Developed by [ Lee Byron] ( https://github.com/leebyron ) and [ Nicholas Schrock] ( https://github.com/schrockn )
306
306
from [ Facebook] ( https://www.facebook.com/ ) .
0 commit comments