rescript-relay 0.0.0-cli-687699fd → 0.0.0-linux-musl-d5435e23

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.
package/src/utils.js CHANGED
@@ -24,6 +24,10 @@ function traverse(
24
24
  instructionPaths,
25
25
  addFragmentOnRoot
26
26
  ) {
27
+ // We lazily set up a new object for each "level", as we don't want to mutate
28
+ // what comes back from the Relay store, and nor do we want to create new
29
+ // objects unless we need to. And we only need to when we need to change
30
+ // something.
27
31
  var newObj;
28
32
 
29
33
  if (addFragmentOnRoot) {
@@ -39,193 +43,168 @@ function traverse(
39
43
  var thisPath = makeNewPath(currentPath, [key]);
40
44
  var path = getPathName(thisPath);
41
45
 
42
- var instructions = instructionMap[path];
46
+ var instructions = instructionMap[path] || {};
43
47
 
44
- var hasDeeperInstructions =
45
- instructionPaths.filter(function (p) {
46
- return p.indexOf(path) === 0 && p.length > path.length;
47
- }).length > 0;
48
+ if (currentObj[key] == null) {
49
+ newObj = getNewObj(newObj, currentObj);
50
+ newObj[key] = nullableValue;
51
+ continue;
52
+ }
48
53
 
49
- if (instructions) {
50
- if (currentObj[key] == null) {
51
- if (instructions["n"] === "") {
52
- newObj = getNewObj(newObj, currentObj);
53
- newObj[key] = nullableValue;
54
- }
55
- } else {
56
- var shouldConvertRootObj =
57
- typeof instructions["r"] === "string" &&
58
- fullInstructionMap[instructions["r"]];
54
+ var shouldConvertRootObj =
55
+ typeof instructions["r"] === "string" &&
56
+ fullInstructionMap[instructions["r"]];
59
57
 
60
- var shouldAddFragmentFn = instructions["f"] === "";
58
+ var shouldAddFragmentFn = instructions["f"] === "";
61
59
 
62
- var shouldConvertEnum =
63
- typeof instructions["e"] === "string" &&
64
- !!converters[instructions["e"]];
60
+ var shouldConvertEnum =
61
+ typeof instructions["e"] === "string" && !!converters[instructions["e"]];
65
62
 
66
- var shouldConvertCustomField =
67
- typeof instructions["c"] === "string" &&
68
- !!converters[instructions["c"]];
63
+ var shouldConvertCustomField =
64
+ typeof instructions["c"] === "string" && !!converters[instructions["c"]];
69
65
 
70
- var shouldConvertUnion =
71
- typeof instructions["u"] === "string" &&
72
- !!converters[instructions["u"]];
66
+ var shouldConvertUnion =
67
+ typeof instructions["u"] === "string" && !!converters[instructions["u"]];
73
68
 
74
- var isTopLevelNodeField = typeof instructions["tnf"] === "string";
69
+ var isTopLevelNodeField = typeof instructions["tnf"] === "string";
75
70
 
76
- /**
77
- * Handle arrays
78
- */
79
- if (Array.isArray(currentObj[key])) {
80
- newObj = getNewObj(newObj, currentObj);
81
- newObj[key] = currentObj[key].map(function (v) {
82
- if (v == null) {
83
- return nullableValue;
84
- }
85
-
86
- if (shouldConvertRootObj) {
87
- return traverser(
88
- v,
89
- fullInstructionMap,
90
- converters,
91
- nullableValue,
92
- instructions["r"]
93
- );
94
- }
95
-
96
- if (shouldConvertEnum) {
97
- return converters[instructions["e"]](v);
98
- }
99
-
100
- if (shouldConvertCustomField) {
101
- return converters[instructions["c"]](v);
102
- }
103
-
104
- if (
105
- shouldConvertUnion &&
106
- typeof v === "object" &&
107
- typeof v.__typename === "string"
108
- ) {
109
- isUnion = true;
110
-
111
- var newPath = makeNewPath(currentPath, [
112
- key,
113
- v.__typename.toLowerCase(),
114
- ]);
115
-
116
- var unionRootHasFragment =
117
- (instructionMap[getPathName(newPath)] || {}).f === "";
118
-
119
- var traversedValue = traverse(
120
- fullInstructionMap,
121
- newPath,
122
- v,
123
- instructionMap,
124
- converters,
125
- nullableValue,
126
- instructionPaths,
127
- unionRootHasFragment
128
- );
129
-
130
- return converters[instructions["u"]](traversedValue);
131
- }
132
-
133
- if (shouldAddFragmentFn && typeof v === "object") {
134
- var objWithFragmentFn = Object.assign({}, v);
135
- objWithFragmentFn.fragmentRefs = Object.assign(
136
- {},
137
- objWithFragmentFn
138
- );
139
- return objWithFragmentFn;
140
- }
141
-
142
- return v;
143
- });
144
- } else {
145
- /**
146
- * Handle normal values.
147
- */
148
- var v = currentObj[key];
149
-
150
- if (shouldConvertRootObj) {
151
- newObj = getNewObj(newObj, currentObj);
152
- newObj[key] = traverser(
153
- v,
154
- fullInstructionMap,
155
- converters,
156
- nullableValue,
157
- instructions["r"]
158
- );
159
- }
71
+ /**
72
+ * Handle arrays
73
+ */
74
+ if (Array.isArray(currentObj[key])) {
75
+ newObj = getNewObj(newObj, currentObj);
76
+ newObj[key] = currentObj[key].map(function (v) {
77
+ if (v == null) {
78
+ return nullableValue;
79
+ }
160
80
 
161
- if (isTopLevelNodeField) {
162
- if (
163
- !v ||
164
- !v.hasOwnProperty("__typename") ||
165
- v.__typename !== instructions["tnf"]
166
- ) {
167
- newObj = getNewObj(newObj, currentObj);
168
- newObj[key] = nullableValue;
169
- }
170
- }
81
+ if (shouldConvertRootObj) {
82
+ return traverser(
83
+ v,
84
+ fullInstructionMap,
85
+ converters,
86
+ nullableValue,
87
+ instructions["r"]
88
+ );
89
+ }
171
90
 
172
- if (shouldConvertEnum) {
173
- newObj = getNewObj(newObj, currentObj);
174
- newObj[key] = converters[instructions["e"]](v);
175
- }
91
+ if (shouldConvertEnum) {
92
+ return converters[instructions["e"]](v);
93
+ }
176
94
 
177
- if (shouldConvertCustomField) {
178
- newObj = getNewObj(newObj, currentObj);
179
- newObj[key] = converters[instructions["c"]](v);
180
- }
95
+ if (shouldConvertCustomField) {
96
+ return converters[instructions["c"]](v);
97
+ }
181
98
 
182
- if (
183
- shouldConvertUnion &&
184
- typeof v === "object" &&
185
- typeof v.__typename === "string"
186
- ) {
187
- isUnion = true;
99
+ if (
100
+ shouldConvertUnion &&
101
+ typeof v === "object" &&
102
+ typeof v.__typename === "string"
103
+ ) {
104
+ isUnion = true;
105
+
106
+ var newPath = makeNewPath(currentPath, [key, v.__typename]);
107
+
108
+ var unionRootHasFragment =
109
+ (instructionMap[getPathName(newPath)] || {}).f === "";
110
+
111
+ var traversedValue = traverse(
112
+ fullInstructionMap,
113
+ newPath,
114
+ v,
115
+ instructionMap,
116
+ converters,
117
+ nullableValue,
118
+ instructionPaths,
119
+ unionRootHasFragment
120
+ );
121
+
122
+ return converters[instructions["u"]](traversedValue);
123
+ }
188
124
 
189
- var newPath = makeNewPath(currentPath, [
190
- key,
191
- v.__typename.toLowerCase(),
192
- ]);
125
+ if (shouldAddFragmentFn && typeof v === "object") {
126
+ var objWithFragmentFn = Object.assign({}, v);
127
+ objWithFragmentFn.fragmentRefs = Object.assign({}, objWithFragmentFn);
128
+ return objWithFragmentFn;
129
+ }
193
130
 
194
- var unionRootHasFragment =
195
- (instructionMap[getPathName(newPath)] || {}).f === "";
131
+ return v;
132
+ });
133
+ } else {
134
+ /**
135
+ * Handle normal values.
136
+ */
137
+ var v = currentObj[key];
196
138
 
197
- var traversedValue = traverse(
198
- fullInstructionMap,
199
- newPath,
200
- v,
201
- instructionMap,
202
- converters,
203
- nullableValue,
204
- instructionPaths,
205
- unionRootHasFragment
206
- );
139
+ if (shouldConvertRootObj) {
140
+ newObj = getNewObj(newObj, currentObj);
141
+ newObj[key] = traverser(
142
+ v,
143
+ fullInstructionMap,
144
+ converters,
145
+ nullableValue,
146
+ instructions["r"]
147
+ );
148
+ }
207
149
 
208
- newObj = getNewObj(newObj, currentObj);
209
- newObj[key] = converters[instructions["u"]](traversedValue);
210
- }
150
+ if (isTopLevelNodeField) {
151
+ if (
152
+ v == null ||
153
+ !v.hasOwnProperty("__typename") ||
154
+ v.__typename !== instructions["tnf"]
155
+ ) {
156
+ newObj = getNewObj(newObj, currentObj);
157
+ newObj[key] = nullableValue;
158
+ }
159
+ }
211
160
 
212
- if (shouldAddFragmentFn && typeof v === "object") {
213
- newObj = getNewObj(newObj, currentObj);
161
+ if (shouldConvertEnum) {
162
+ newObj = getNewObj(newObj, currentObj);
163
+ newObj[key] = converters[instructions["e"]](v);
164
+ }
214
165
 
215
- var objWithFragmentFn = Object.assign({}, v);
166
+ if (shouldConvertCustomField) {
167
+ newObj = getNewObj(newObj, currentObj);
168
+ newObj[key] = converters[instructions["c"]](v);
169
+ }
216
170
 
217
- objWithFragmentFn.fragmentRefs = Object.assign(
218
- {},
219
- objWithFragmentFn
220
- );
171
+ if (
172
+ shouldConvertUnion &&
173
+ v != null &&
174
+ typeof v === "object" &&
175
+ typeof v.__typename === "string"
176
+ ) {
177
+ isUnion = true;
221
178
 
222
- newObj[key] = objWithFragmentFn;
223
- }
224
- }
179
+ var newPath = makeNewPath(currentPath, [key, v.__typename]);
180
+
181
+ var unionRootHasFragment =
182
+ (instructionMap[getPathName(newPath)] || {}).f === "";
183
+
184
+ var traversedValue = traverse(
185
+ fullInstructionMap,
186
+ newPath,
187
+ v,
188
+ instructionMap,
189
+ converters,
190
+ nullableValue,
191
+ instructionPaths,
192
+ unionRootHasFragment
193
+ );
194
+
195
+ newObj = getNewObj(newObj, currentObj);
196
+ newObj[key] = converters[instructions["u"]](traversedValue);
197
+ }
198
+
199
+ if (shouldAddFragmentFn && typeof v === "object") {
200
+ newObj = getNewObj(newObj, currentObj);
201
+ var objWithFragmentFn = Object.assign({}, v);
202
+ objWithFragmentFn.fragmentRefs = Object.assign({}, objWithFragmentFn);
203
+ newObj[key] = objWithFragmentFn;
225
204
  }
226
205
  }
227
206
 
228
- if (hasDeeperInstructions && originalValue != null && !isUnion) {
207
+ if (originalValue != null && !isUnion) {
229
208
  var nextObj = (newObj && newObj[key]) || currentObj[key];
230
209
 
231
210
  if (typeof nextObj === "object" && !Array.isArray(originalValue)) {
@@ -246,17 +225,21 @@ function traverse(
246
225
  } else if (Array.isArray(originalValue)) {
247
226
  newObj = getNewObj(newObj, currentObj);
248
227
  newObj[key] = nextObj.map(function (o) {
249
- return typeof o === "object" && o != null
250
- ? traverse(
251
- fullInstructionMap,
252
- thisPath,
253
- o,
254
- instructionMap,
255
- converters,
256
- nullableValue,
257
- instructionPaths
258
- )
259
- : o;
228
+ if (typeof o === "object" && o != null) {
229
+ return traverse(
230
+ fullInstructionMap,
231
+ thisPath,
232
+ o,
233
+ instructionMap,
234
+ converters,
235
+ nullableValue,
236
+ instructionPaths
237
+ );
238
+ } else if (o == null) {
239
+ return nullableValue;
240
+ } else {
241
+ return o;
242
+ }
260
243
  });
261
244
  }
262
245
  }
@@ -276,7 +259,7 @@ function traverse(
276
259
  */
277
260
  function traverser(
278
261
  root,
279
- _instructionMaps,
262
+ instructionMaps_,
280
263
  theConverters,
281
264
  nullableValue,
282
265
  rootObjectKey
@@ -285,22 +268,12 @@ function traverser(
285
268
  return nullableValue;
286
269
  }
287
270
 
288
- var instructionMaps = _instructionMaps || {};
289
- var instructionMap = instructionMaps[rootObjectKey || "__root"];
290
-
291
- // No instructions
292
- if (!instructionMap) {
293
- return root;
294
- }
271
+ var instructionMaps = instructionMaps_ || {};
272
+ var instructionMap = instructionMaps[rootObjectKey || "__root"] || {};
295
273
 
296
274
  var converters = theConverters == null ? {} : theConverters;
297
275
  var instructionPaths = Object.keys(instructionMap);
298
276
 
299
- // Nothing to convert, bail early
300
- if (instructionPaths.length === 0) {
301
- return root;
302
- }
303
-
304
277
  // We'll add the fragmentRefs reference to the root if needed here.
305
278
  var fragmentsOnRoot = (instructionMap[""] || {}).f === "";
306
279
  var unionRootConverter = converters[(instructionMap[""] || {}).u];
@@ -311,9 +284,19 @@ function traverser(
311
284
  return nullableValue;
312
285
  }
313
286
 
287
+ var n = [];
288
+
289
+ // Since a root level union is treated as a "new root level", we'll need
290
+ // to do a separate check here of whether there's a fragment on the root
291
+ // we need to account for, or not.
292
+ if (unionRootConverter != null) {
293
+ n = [v.__typename];
294
+ fragmentsOnRoot = (instructionMap[v.__typename] || {}).f === "";
295
+ }
296
+
314
297
  var traversedObj = traverse(
315
298
  instructionMaps,
316
- [],
299
+ n,
317
300
  v,
318
301
  instructionMap,
319
302
  converters,
@@ -330,9 +313,18 @@ function traverser(
330
313
 
331
314
  var newObj = Object.assign({}, root);
332
315
 
316
+ var n = [];
317
+
318
+ // Same as in the union array check above - if there's a fragment in the new
319
+ // root created by the union, we need to account for that separately here.
320
+ if (unionRootConverter != null) {
321
+ n = [newObj.__typename];
322
+ fragmentsOnRoot = (instructionMap[newObj.__typename] || {}).f === "";
323
+ }
324
+
333
325
  var v = traverse(
334
326
  instructionMaps,
335
- [],
327
+ n,
336
328
  newObj,
337
329
  instructionMap,
338
330
  converters,