@takeshape/schema 8.46.0 → 8.49.0

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.
@@ -17,7 +17,7 @@ export function flattenTemplates(projectSchema) {
17
17
  const {
18
18
  shapeName,
19
19
  dependencies
20
- } = resolveTemplate(projectSchema, ref.template, refItemToShapeName(ref));
20
+ } = resolveTemplate(newSchema, ref.template, refItemToShapeName(ref));
21
21
  set(newSchema, ref.path, ref.path[ref.path.length - 1] === '@ref' ? `local:${shapeName}` : shapeName);
22
22
  Object.assign(newSchema.shapes, dependencies);
23
23
  }
@@ -249,7 +249,7 @@ export function getMutationArgs(templateName, verb) {
249
249
  const shapeName = getRefShapeName(projectSchema, propSchema);
250
250
 
251
251
  if (shapeName) {
252
- return !projectSchema.shapes[getInputShapeName(shapeName)];
252
+ return shapeName !== 'TSRelationship' && !projectSchema.shapes[getInputShapeName(shapeName)];
253
253
  }
254
254
 
255
255
  return true;
@@ -1,8 +1,6 @@
1
1
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
2
2
 
3
3
  import { pascalCase, isDefined } from '@takeshape/util';
4
- import isEqual from 'lodash/isEqual';
5
- import get from 'lodash/get';
6
4
  import unset from 'lodash/unset';
7
5
  import isFunction from 'lodash/isFunction';
8
6
  import { createSchemaPropertyList, createShape, getAllRefsInShapes, getShapeById } from '../schema-util';
@@ -110,14 +108,40 @@ export const boolOpDescriptionMap = {
110
108
  OR: 'OR takes an array of conditions that should appear in the matching results. Nested boolean operators can be used to create complex filters.',
111
109
  NOT: 'NOT takes a single condition that must not appear in the matching results.'
112
110
  };
111
+ export function isEqualRelationship(a, b) {
112
+ var _a$Relationship, _b$Relationship;
113
113
 
114
- const getConflictingProperties = (projectSchema, shapes) => {
115
- let conflicts = [];
114
+ // non-relationships are considered equal in that they are both equal that regard
115
+ if (!a['@relationship'] && !b['@relationship']) {
116
+ return true;
117
+ }
118
+
119
+ const idsA = (_a$Relationship = a['@relationship']) === null || _a$Relationship === void 0 ? void 0 : _a$Relationship.shapeIds;
120
+
121
+ if (!idsA) {
122
+ return false;
123
+ }
124
+
125
+ const idsB = (_b$Relationship = b['@relationship']) === null || _b$Relationship === void 0 ? void 0 : _b$Relationship.shapeIds;
126
+
127
+ if (!idsB || idsA.length !== idsB.length) {
128
+ return false;
129
+ }
130
+
131
+ const idSet = new Set(idsA);
132
+ return idsB.every(id => idSet.has(id));
133
+ }
134
+
135
+ function getConflictingProperties(projectSchema, shapes) {
136
+ const conflicts = new Set();
116
137
  const allProps = {};
117
138
 
118
139
  for (const shape of Object.values(shapes)) {
119
- const shapeConflicts = createSchemaPropertyList(projectSchema, shape.schema).filterBy(([name, prop]) => name in allProps && (allProps[name].type !== prop.type || !isEqual(get(allProps, [name, '@relationship', 'shapeIds']), get(prop, ['@relationship', 'shapeIds'])))).getNames();
120
- conflicts = conflicts.concat(shapeConflicts);
140
+ for (const [name, prop] of createSchemaPropertyList(projectSchema, shape.schema).getNodes()) {
141
+ if (name in allProps && (allProps[name].type !== prop.type || !isEqualRelationship(allProps[name], prop))) {
142
+ conflicts.add(name);
143
+ }
144
+ }
121
145
 
122
146
  if (isObjectSchema(shape.schema)) {
123
147
  Object.assign(allProps, shape.schema.properties);
@@ -125,7 +149,7 @@ const getConflictingProperties = (projectSchema, shapes) => {
125
149
  }
126
150
 
127
151
  return conflicts;
128
- };
152
+ }
129
153
 
130
154
  export function getFieldTypeComparison(fieldType) {
131
155
  const comparison = fieldTypeComparison[fieldType];
@@ -252,7 +276,7 @@ function getTypeName(name, prop, shapeName, context) {
252
276
 
253
277
  if (type === 'object' || type === 'array') {
254
278
  const prefix = `TS${relationshipDepth >= MAX_RELATIONSHIP_DEPTH ? 'Shallow' : ''}Where`;
255
- const fieldName = `${conflictingProperties.includes(name) ? '_' : ''}${pascalCase(name)}`;
279
+ const fieldName = `${conflictingProperties !== null && conflictingProperties !== void 0 && conflictingProperties.has(name) ? '_' : ''}${pascalCase(name)}`;
256
280
  const refItem = getRefOrItemsRef(projectSchema, prop);
257
281
  const suffix = refItem ? refItemToShapeName(refItem) : `${shapeName}${fieldName}`;
258
282
  return `${prefix}${suffix}`;
@@ -273,12 +297,16 @@ export function getFields(typeName, shapes, context) {
273
297
  } = context;
274
298
  const result = {};
275
299
  const tooDeep = exceededRelationshipDepth(relationshipDepth);
300
+ const conflictingProperties = shapes.length ? getConflictingProperties(projectSchema, shapes) : undefined;
301
+ const fieldContext = { ...context,
302
+ conflictingProperties
303
+ };
276
304
 
277
305
  for (const shape of shapes) {
278
306
  const nodes = createSchemaPropertyList(projectSchema, shape.schema).filterBy(([name, property]) => !skipField(name, property, projectSchema, tooDeep)).getNodes();
279
307
 
280
308
  for (const [name, property] of nodes) {
281
- Object.assign(result, getPropertyComparisonType(name, property, shape.name, context));
309
+ Object.assign(result, getPropertyComparisonType(name, property, shape.name, fieldContext));
282
310
  }
283
311
  }
284
312
 
@@ -330,7 +358,7 @@ export function getPropertyComparisonType(name, prop, shapeName, context) {
330
358
  }
331
359
  }
332
360
 
333
- if (!shapeCache.has(typeName)) {
361
+ if (!shapeCache.has(typeName) && !projectSchema.shapes[typeName]) {
334
362
  shapeCache.set(typeName, () => {
335
363
  var _prop$items;
336
364
 
@@ -342,13 +370,11 @@ export function getPropertyComparisonType(name, prop, shapeName, context) {
342
370
  } else if (prop['@relationship']) {
343
371
  const shapes = prop['@relationship'].shapeIds.map(id => getShapeById(projectSchema, id)).filter(isDefined);
344
372
  props = getFields(typeName, shapes, { ...context,
345
- conflictingProperties: getConflictingProperties(context.projectSchema, shapes),
346
373
  relationshipDepth: relationshipDepth + 1
347
374
  });
348
375
  } else if (isUnionSchema(propOrItems)) {
349
376
  const shapes = enumerateOneOfKeys(projectSchema, propOrItems.oneOf).map(option => projectSchema.shapes[option.shapeName]);
350
377
  props = getFields(typeName, shapes, { ...context,
351
- conflictingProperties: getConflictingProperties(context.projectSchema, shapes),
352
378
  relationshipDepth,
353
379
  booleanOperators: false
354
380
  });
@@ -358,10 +384,14 @@ export function getPropertyComparisonType(name, prop, shapeName, context) {
358
384
  const properties = ((_prop$items2 = prop.items) === null || _prop$items2 === void 0 ? void 0 : _prop$items2.properties) ?? prop.properties;
359
385
 
360
386
  if (properties) {
387
+ props = props ?? {};
388
+ const fieldContext = { ...context,
389
+ conflictingProperties: undefined // non-union objects have no conflicts
390
+
391
+ };
392
+
361
393
  for (const propName of Object.keys(properties)) {
362
- props = { ...props,
363
- ...getPropertyComparisonType(propName, properties[propName], shapeName, context)
364
- };
394
+ Object.assign(props, getPropertyComparisonType(propName, properties[propName], shapeName, fieldContext));
365
395
  }
366
396
  }
367
397
  } else {
@@ -386,8 +416,8 @@ export function getPropertyComparisonType(name, prop, shapeName, context) {
386
416
  });
387
417
  }
388
418
 
389
- if (shapeCache.has(typeName)) {
390
- const fieldName = `${conflictingProperties.includes(name) ? `${shapeName}_` : ''}${name}`;
419
+ if (shapeCache.has(typeName) || projectSchema.shapes[typeName]) {
420
+ const fieldName = `${conflictingProperties !== null && conflictingProperties !== void 0 && conflictingProperties.has(name) ? `${shapeName}_` : ''}${name}`;
391
421
  return {
392
422
  [fieldName]: {
393
423
  '@ref': `local:${typeName}`
@@ -466,14 +496,20 @@ function expandUnionShapes(projectSchema, shapes) {
466
496
 
467
497
  export function getWhereShape(projectSchema, selectedShapes) {
468
498
  const shapeName = selectedShapes.length === 1 ? `TSWhere${pascalCase(selectedShapes[0].name)}Input` : 'TSWhereInput';
499
+
500
+ if (projectSchema.shapes[shapeName]) {
501
+ return {
502
+ shapeName,
503
+ dependencies: {}
504
+ };
505
+ }
506
+
469
507
  const expandedShapes = expandUnionShapes(projectSchema, selectedShapes);
470
508
  const isManyContentTypes = expandedShapes.length > 1;
471
509
  const shapeCache = new ShapeCache();
472
- const conflictingProperties = getConflictingProperties(projectSchema, expandedShapes);
473
510
  const fields = getFields(shapeName, expandedShapes, {
474
511
  projectSchema,
475
512
  shapeCache,
476
- conflictingProperties,
477
513
  relationshipDepth: 0
478
514
  });
479
515
  return {