@@ -117,4 +117,82 @@ type Query {
117
117
}
118
118
"""
119
119
)
120
+
121
+ """
122
+ `customersOffset` pages through twenty three generated `Customer` objects.
123
+ The data is supplied by a simulated REST API using offset pagination
124
+ but is exposed using standard GraphQL pagination.
125
+
126
+ Typically the `endpoint` argument of `@rest` would have query
127
+ parameters that set the pagination arguments expected by the
128
+ REST API from the field arguments `first` and `after` (offset defaults to 0 if empty),
129
+ for example: `?limt=$first&offset=$after`.
130
+ """
131
+ customersOffset (first : Int ! , after : String = " " ): CustomerConnection
132
+ @rest (
133
+ # pagination sets the type of pagination the REST API uses
134
+ # and for OFFSET requires a setter that declares
135
+ # where the total number of records are in the JSON response.
136
+ pagination : {
137
+ type : OFFSET
138
+ setters : [{ field : " total" , path : " meta.records" }]
139
+ }
140
+
141
+ # resultroot indicates where root in the JSON response
142
+ # for the values that will populate the nodes.
143
+ # Note this does not affect pagination setters, they
144
+ # are always relative to the root of the response.
145
+ resultroot : " values[]"
146
+
147
+ # Ecmascript (with empty endpoint) is used to mimic the response from a REST api.
148
+ # Note ECMAScript is only used to generate a mock response with customer objects and page number metadata,
149
+ # using @rest against a real endpoint would not typically require any ECMAScript.
150
+ endpoint : " stepzen:empty"
151
+ ecmascript : " " "
152
+ function transformREST() {
153
+ // A total of 23 items will be returned
154
+ const totalItems = 23;
155
+
156
+ // Pagination field arguments
157
+ // Since this is OFFSET pagination
158
+ // " after " is decoded by StepZen from the opaque string value
159
+ // and passed into @rest as a integer offset value,
160
+ // with the offset of the first record being zero.
161
+ const limit = get('first');
162
+ const offset = get('after');
163
+
164
+ // metadata - total number of records
165
+ const records = Math.ceil(totalItems / limit)
166
+
167
+ // generate customers for nodes based on the limit and offset values
168
+ const startIndex = offset+1 || 1;
169
+ const endIndex = Math.min(startIndex + limit, totalItems+1);
170
+ var customers = []
171
+ for (let i = startIndex; i < endIndex; i++) {
172
+ customers.push({
173
+ id: i,
174
+ name: 'name-' + i,
175
+ email: 'user-' + i + '@example.com'
176
+ });
177
+ }
178
+
179
+ // This returns a typical layout of a REST response
180
+ // when pagination is through an offset.
181
+ // @rest must be configured to match the REST response layout.
182
+ //
183
+ // pagination setters defines that the page count
184
+ // is taken from meta.records
185
+ //
186
+ // resultroot corresponds to the location that contains the
187
+ // data values. Note the REST API returns the customer objects,
188
+ // StepZen automatically creates the connection/edges structure
189
+ // for the values.
190
+ return ({
191
+ meta: { 'records': records },
192
+ values: customers
193
+ }
194
+ );
195
+ }
196
+ """
197
+ )
120
198
}
0 commit comments