@takeshape/schema 8.161.0 → 8.166.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.
@@ -716,9 +716,8 @@ function getShapeDependencies(projectSchema, shape, predicate) {
716
716
  const shapeNames = new Set();
717
717
 
718
718
  const findDependencies = schema => {
719
- schema = schema.items ?? schema;
720
-
721
719
  if (!predicate || predicate(schema)) {
720
+ schema = schema.items ?? schema;
722
721
  const refItem = (0, _refs.getRef)(projectSchema, schema);
723
722
 
724
723
  if (refItem) {
@@ -1 +1 @@
1
- {"version":3,"file":"templates.d.ts","sourceRoot":"","sources":["../../../src/template-shapes/templates.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,YAAY,EAAE,cAAc,EAAE,KAAK,EAAc,MAAM,mBAAmB,CAAC;AACxF,OAAO,KAAK,EAAC,gBAAgB,EAAE,eAAe,EAAC,MAAM,SAAS,CAAC;AAC/D,OAAO,EAAC,aAAa,EAAC,MAAM,SAAS,CAAC;AAWtC,eAAO,MAAM,SAAS,YAoJH,OAAO,SAAS,KAAK,KAAG,gBAAgB,AApJP,CAAC;AACrD,eAAO,MAAM,kBAAkB,YAmJZ,OAAO,SAAS,KAAK,KAAG,gBAAgB,AAnJW,CAAC;AACvE,eAAO,MAAM,UAAU,YAoWJ,eAAe,SAAS,KAAK,KAAG,gBAAgB,AApWA,CAAC;AACpE,eAAO,MAAM,YAAY,YAmWN,eAAe,SAAS,KAAK,KAAG,gBAAgB,AAnWK,CAAC;AACzE,eAAO,MAAM,UAAU,sBAgNI,eAAe,SAAS,KAAK,KAAG,gBAAgB,AAhNE,CAAC;AAC9E,eAAO,MAAM,UAAU,sBA+MI,eAAe,SAAS,KAAK,KAAG,gBAAgB,AA/ME,CAAC;AAC9E,eAAO,MAAM,aAAa,sBA8MC,eAAe,SAAS,KAAK,KAAG,gBAAgB,AA9MW,CAAC;AACvF,eAAO,MAAM,UAAU,sBA6MI,eAAe,SAAS,KAAK,K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gB,CA4E1E;AAED,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,aAAa,aACtC,OAAO,SAAS,KAAK,KAAG,gBAAgB,CAiB1D;AAED,eAAO,MAAM,gBAAgB,EAAE,YAwB9B,CAAC;AAuBF,wBAAgB,qBAAqB,CAAC,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,OAAO,aAC5D,eAAe,SAAS,KAAK,KAAG,gBAAgB,CA0BlE"}
@@ -255,24 +255,21 @@ function getOutputShapeName(name) {
255
255
  return name.replace(/(?:Input|PartialInput)$/, '');
256
256
  }
257
257
 
258
- function rewriteRefs(shapeSchema, rewriteShapeName) {
258
+ function rewriteRefs(context, shapeSchema, rewriteShapeName) {
259
259
  const newSchema = (0, _util.deepClone)(shapeSchema);
260
260
  (0, _util.visit)(newSchema, ['$ref', '@ref'], (_, path) => {
261
261
  const parentPath = path.slice(0, -1);
262
262
  const value = (0, _get.default)(newSchema, parentPath);
263
263
 
264
264
  if ((value['@ref'] || value.$ref) && !parentPath.includes('@input') && !parentPath.includes('@output')) {
265
- if (value['@ref']) {
266
- const ref = value['@ref'];
267
- const parts = ref.split(':');
268
- value['@ref'] = `local:${rewriteShapeName(parts[1])}`;
269
- }
265
+ const refItem = (0, _refs.getRef)(context, value);
266
+
267
+ if (refItem) {
268
+ value['@ref'] = refItem.isForeign ? (0, _refs.refItemToAtRef)(refItem) : `local:${rewriteShapeName((0, _refs.refItemToShapeName)(refItem))}`;
270
269
 
271
- if (value.$ref) {
272
- const ref = value.$ref;
273
- const parts = ref.split('/');
274
- parts[2] = rewriteShapeName(parts[2]);
275
- value.$ref = parts.join('/');
270
+ if (value.$ref) {
271
+ delete value.$ref;
272
+ }
276
273
  }
277
274
  }
278
275
  });
@@ -301,7 +298,7 @@ function getMutationArgs(templateName, verb) {
301
298
  rewrittenSchema = (0, _createInputSchema.createInputSchema)(shape.schema, {
302
299
  isUpdate
303
300
  });
304
- inputShapeSchema = rewriteRefs(rewrittenSchema, getInputShapeName);
301
+ inputShapeSchema = rewriteRefs(projectSchema, rewrittenSchema, getInputShapeName);
305
302
  }
306
303
 
307
304
  const inputShape = (0, _schemaUtil.createShape)(inputName, (0, _util2.mergeObjectSchemas)(shouldRequireId ? idSchema : undefined, inputShapeSchema), {
@@ -341,7 +338,7 @@ function getMutationArgs(templateName, verb) {
341
338
  }).map(shapeName => {
342
339
  const shape = projectSchema.shapes[shapeName]; // We only allow partial input at the top-level
343
340
 
344
- return (0, _schemaUtil.createShape)(getInputShapeName(shapeName), rewriteRefs((0, _createInputSchema.createInputSchema)(shape.schema), getInputShapeName));
341
+ return (0, _schemaUtil.createShape)(getInputShapeName(shapeName), rewriteRefs(projectSchema, (0, _createInputSchema.createInputSchema)(shape.schema), getInputShapeName));
345
342
  }), 'name')),
346
343
  ...(addStructure ? {
347
344
  ContentStructureInput
@@ -0,0 +1,12 @@
1
+ import { ProjectSchema, PropertySchema, Shape } from '../project-schema';
2
+ /**
3
+ *
4
+ * @param projectSchema
5
+ * @param schema
6
+ * @param queryPath
7
+ *
8
+ * @example
9
+ * findShapeAtPath(projectSchema, projectSchema.shapes.Shopify_ProductConnection, ['edges', 'node']) // finds Shopify_Product
10
+ */
11
+ export declare function findShapeAtPath(projectSchema: ProjectSchema, schema: PropertySchema, queryPath: string[]): Shape | undefined;
12
+ //# sourceMappingURL=find-shape-at-path.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"find-shape-at-path.d.ts","sourceRoot":"","sources":["../../../src/util/find-shape-at-path.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAE,cAAc,EAAE,KAAK,EAAC,MAAM,mBAAmB,CAAC;AAIvE;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAC7B,aAAa,EAAE,aAAa,EAC5B,MAAM,EAAE,cAAc,EACtB,SAAS,EAAE,MAAM,EAAE,GAClB,KAAK,GAAG,SAAS,CAuBnB"}
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.findShapeAtPath = findShapeAtPath;
7
+
8
+ var _types = require("../types");
9
+
10
+ var _refs = require("../refs");
11
+
12
+ /**
13
+ *
14
+ * @param projectSchema
15
+ * @param schema
16
+ * @param queryPath
17
+ *
18
+ * @example
19
+ * findShapeAtPath(projectSchema, projectSchema.shapes.Shopify_ProductConnection, ['edges', 'node']) // finds Shopify_Product
20
+ */
21
+ function findShapeAtPath(projectSchema, schema, queryPath) {
22
+ const [prop, ...rest] = queryPath;
23
+
24
+ if ((0, _types.isRefSchema)(schema)) {
25
+ if (queryPath.length === 0) {
26
+ const refItem = (0, _refs.getRef)(projectSchema, schema);
27
+ return refItem && (0, _refs.refItemToShape)(projectSchema, refItem);
28
+ }
29
+
30
+ return findShapeAtPath(projectSchema, (0, _refs.followRef)(projectSchema, schema), queryPath);
31
+ }
32
+
33
+ if ((0, _types.isArraySchema)(schema)) {
34
+ return findShapeAtPath(projectSchema, schema.items, queryPath);
35
+ }
36
+
37
+ if ((0, _types.isObjectSchema)(schema)) {
38
+ const propSchema = schema.properties[prop];
39
+
40
+ if (propSchema) {
41
+ return findShapeAtPath(projectSchema, propSchema, rest);
42
+ }
43
+ }
44
+
45
+ return undefined;
46
+ }
@@ -0,0 +1,7 @@
1
+ import { Shape } from '../project-schema';
2
+ import { ServicesShapesContext } from '../refs';
3
+ /**
4
+ * Given a list of shapes find the conflicting props
5
+ */
6
+ export declare function getConflictingProperties(context: ServicesShapesContext, shapes: Shape[]): Set<string>;
7
+ //# sourceMappingURL=get-conflicting-properties.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-conflicting-properties.d.ts","sourceRoot":"","sources":["../../../src/util/get-conflicting-properties.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,KAAK,EAAC,MAAM,mBAAmB,CAAC;AACxD,OAAO,EAA6D,qBAAqB,EAAC,MAAM,SAAS,CAAC;AAsB1G;;GAEG;AACH,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,qBAAqB,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,GAAG,CAAC,MAAM,CAAC,CAoCrG"}
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getConflictingProperties = getConflictingProperties;
7
+
8
+ var _refs = require("../refs");
9
+
10
+ var _util = require("@takeshape/util");
11
+
12
+ var _types = require("../types");
13
+
14
+ /**
15
+ * Normalize ref, oneOf and array schemas and get a list of @ref expressions
16
+ */
17
+ function getRefStrings(context, propertySchema) {
18
+ const schema = propertySchema.items ?? propertySchema;
19
+ const schemas = schema.oneOf ? schema.oneOf : [schema];
20
+ return schemas.map(propertySchema => {
21
+ const ref = (0, _refs.getRef)(context, propertySchema);
22
+ return ref ? (0, _refs.refItemToAtRef)(ref) : undefined;
23
+ }).filter(_util.isDefined);
24
+ }
25
+
26
+ function isRefEqual(context, a, b) {
27
+ return (0, _util.setIsEqual)(getRefStrings(context, a), getRefStrings(context, b));
28
+ }
29
+ /**
30
+ * Given a list of shapes find the conflicting props
31
+ */
32
+
33
+
34
+ function getConflictingProperties(context, shapes) {
35
+ const conflicts = new Set();
36
+ const allProps = {};
37
+
38
+ for (const shape of Object.values(shapes)) {
39
+ const schema = (0, _refs.dereferenceSchema)(context, shape.schema, ['shapes', shape.name]);
40
+
41
+ if (!(0, _types.isObjectSchema)(schema)) {
42
+ continue;
43
+ }
44
+
45
+ const required = new Set(schema.required ?? []);
46
+
47
+ for (const [name, prop] of Object.entries(schema.properties)) {
48
+ // skip props that we know already have conflict
49
+ if (conflicts.has(name)) {
50
+ continue;
51
+ }
52
+
53
+ const isRequired = required.has(name);
54
+
55
+ if (allProps[name]) {
56
+ if ( // same type
57
+ allProps[name][0].type !== prop.type || // same refs
58
+ !isRefEqual(context, allProps[name][0], prop) || // same nullability
59
+ isRequired !== allProps[name][1]) {
60
+ conflicts.add(name);
61
+ }
62
+ } else {
63
+ allProps[name] = [prop, isRequired];
64
+ }
65
+ }
66
+ }
67
+
68
+ return conflicts;
69
+ }
@@ -0,0 +1,3 @@
1
+ import { ProjectSchema, PropertySchema, Query, Shape } from '../project-schema';
2
+ export declare function getReturnShape(projectSchema: ProjectSchema, propOrQuery: PropertySchema | Query): Shape;
3
+ //# sourceMappingURL=get-return-shape.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-return-shape.d.ts","sourceRoot":"","sources":["../../../src/util/get-return-shape.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAE,cAAc,EAAE,KAAK,EAAE,KAAK,EAAC,MAAM,mBAAmB,CAAC;AA2B9E,wBAAgB,cAAc,CAAC,aAAa,EAAE,aAAa,EAAE,WAAW,EAAE,cAAc,GAAG,KAAK,GAAG,KAAK,CAQvG"}
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getReturnShape = getReturnShape;
7
+
8
+ var _refs = require("../refs");
9
+
10
+ function isQuery(propOrQuery) {
11
+ return 'shape' in propOrQuery && 'resolver' in propOrQuery;
12
+ }
13
+
14
+ function getReturnShapeHelper(projectSchema, propOrQuery) {
15
+ let shape;
16
+
17
+ if (isQuery(propOrQuery)) {
18
+ const {
19
+ shapeName
20
+ } = (0, _refs.parseReturnShape)(projectSchema, propOrQuery.shape);
21
+ shape = projectSchema.shapes[shapeName];
22
+ } else {
23
+ const ref = (0, _refs.getRef)(projectSchema, propOrQuery);
24
+
25
+ if (ref) {
26
+ shape = (0, _refs.refItemToShape)(projectSchema, ref);
27
+ }
28
+ }
29
+
30
+ if (!shape) {
31
+ throw new Error(`Unable to find return shape for ${JSON.stringify(propOrQuery)}`);
32
+ }
33
+
34
+ return shape;
35
+ }
36
+
37
+ const cache = new WeakMap();
38
+
39
+ function getReturnShape(projectSchema, propOrQuery) {
40
+ let result = cache.get(propOrQuery);
41
+
42
+ if (!result) {
43
+ result = getReturnShapeHelper(projectSchema, propOrQuery);
44
+ cache.set(propOrQuery, result);
45
+ }
46
+
47
+ return result;
48
+ }
@@ -0,0 +1,3 @@
1
+ import { ProjectSchema, PropertySchema, Query } from '../project-schema';
2
+ export declare function hasArg(projectSchema: ProjectSchema, prop: PropertySchema | Query, argName: string): boolean;
3
+ //# sourceMappingURL=has-arg.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"has-arg.d.ts","sourceRoot":"","sources":["../../../src/util/has-arg.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAE,cAAc,EAAE,KAAK,EAAC,MAAM,mBAAmB,CAAC;AAGvE,wBAAgB,MAAM,CAAC,aAAa,EAAE,aAAa,EAAE,IAAI,EAAE,cAAc,GAAG,KAAK,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAU3G"}
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.hasArg = hasArg;
7
+
8
+ var _schemaUtil = require("../schema-util");
9
+
10
+ function hasArg(projectSchema, prop, argName) {
11
+ var _projectSchema$shapes;
12
+
13
+ const args = 'args' in prop ? prop.args : prop['@args'];
14
+
15
+ if (!args) {
16
+ return false;
17
+ }
18
+
19
+ const schema = typeof args === 'string' ? (_projectSchema$shapes = projectSchema.shapes[args]) === null || _projectSchema$shapes === void 0 ? void 0 : _projectSchema$shapes.schema : args;
20
+ const schemaPropertyAccessor = (0, _schemaUtil.createSchemaPropertyAccessor)(projectSchema, schema);
21
+ return Boolean(schemaPropertyAccessor.getValue(argName));
22
+ }
@@ -1,5 +1,9 @@
1
1
  export * from './api-indexing';
2
2
  export * from './detect-cycles';
3
+ export * from './find-shape-at-path';
4
+ export * from './get-conflicting-properties';
5
+ export * from './get-return-shape';
6
+ export * from './has-arg';
3
7
  export * from './merge';
4
8
  export * from './form-config';
5
9
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/util/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC;AAChC,cAAc,SAAS,CAAC;AACxB,cAAc,eAAe,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/util/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC;AAChC,cAAc,sBAAsB,CAAC;AACrC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,oBAAoB,CAAC;AACnC,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC;AACxB,cAAc,eAAe,CAAC"}
@@ -30,6 +30,58 @@ Object.keys(_detectCycles).forEach(function (key) {
30
30
  });
31
31
  });
32
32
 
33
+ var _findShapeAtPath = require("./find-shape-at-path");
34
+
35
+ Object.keys(_findShapeAtPath).forEach(function (key) {
36
+ if (key === "default" || key === "__esModule") return;
37
+ if (key in exports && exports[key] === _findShapeAtPath[key]) return;
38
+ Object.defineProperty(exports, key, {
39
+ enumerable: true,
40
+ get: function () {
41
+ return _findShapeAtPath[key];
42
+ }
43
+ });
44
+ });
45
+
46
+ var _getConflictingProperties = require("./get-conflicting-properties");
47
+
48
+ Object.keys(_getConflictingProperties).forEach(function (key) {
49
+ if (key === "default" || key === "__esModule") return;
50
+ if (key in exports && exports[key] === _getConflictingProperties[key]) return;
51
+ Object.defineProperty(exports, key, {
52
+ enumerable: true,
53
+ get: function () {
54
+ return _getConflictingProperties[key];
55
+ }
56
+ });
57
+ });
58
+
59
+ var _getReturnShape = require("./get-return-shape");
60
+
61
+ Object.keys(_getReturnShape).forEach(function (key) {
62
+ if (key === "default" || key === "__esModule") return;
63
+ if (key in exports && exports[key] === _getReturnShape[key]) return;
64
+ Object.defineProperty(exports, key, {
65
+ enumerable: true,
66
+ get: function () {
67
+ return _getReturnShape[key];
68
+ }
69
+ });
70
+ });
71
+
72
+ var _hasArg = require("./has-arg");
73
+
74
+ Object.keys(_hasArg).forEach(function (key) {
75
+ if (key === "default" || key === "__esModule") return;
76
+ if (key in exports && exports[key] === _hasArg[key]) return;
77
+ Object.defineProperty(exports, key, {
78
+ enumerable: true,
79
+ get: function () {
80
+ return _hasArg[key];
81
+ }
82
+ });
83
+ });
84
+
33
85
  var _merge = require("./merge");
34
86
 
35
87
  Object.keys(_merge).forEach(function (key) {
package/es/schema-util.js CHANGED
@@ -590,9 +590,8 @@ export function getShapeDependencies(projectSchema, shape, predicate) {
590
590
  const shapeNames = new Set();
591
591
 
592
592
  const findDependencies = schema => {
593
- schema = schema.items ?? schema;
594
-
595
593
  if (!predicate || predicate(schema)) {
594
+ schema = schema.items ?? schema;
596
595
  const refItem = getRef(projectSchema, schema);
597
596
 
598
597
  if (refItem) {
@@ -3,7 +3,7 @@ import { listTypePrefix } from '../migration';
3
3
  import { deepClone, pascalCase, visit } from '@takeshape/util';
4
4
  import keyBy from 'lodash/keyBy';
5
5
  import { createShape, getShapeDependencies, isIndexedRemoteShape } from '../schema-util';
6
- import { getFlattenedTemplateShapeName, getRefShapeName } from '../refs';
6
+ import { getFlattenedTemplateShapeName, getRef, getRefShapeName, refItemToAtRef, refItemToShapeName } from '../refs';
7
7
  import { getWhereSearchArg } from './where';
8
8
  import { mergeObjectSchemas, mergeSchemaProperties } from '../util';
9
9
  import { createInputSchema } from '../create-input-schema';
@@ -200,24 +200,21 @@ export function getOutputShapeName(name) {
200
200
  return name.replace(/(?:Input|PartialInput)$/, '');
201
201
  }
202
202
 
203
- function rewriteRefs(shapeSchema, rewriteShapeName) {
203
+ function rewriteRefs(context, shapeSchema, rewriteShapeName) {
204
204
  const newSchema = deepClone(shapeSchema);
205
205
  visit(newSchema, ['$ref', '@ref'], (_, path) => {
206
206
  const parentPath = path.slice(0, -1);
207
207
  const value = get(newSchema, parentPath);
208
208
 
209
209
  if ((value['@ref'] || value.$ref) && !parentPath.includes('@input') && !parentPath.includes('@output')) {
210
- if (value['@ref']) {
211
- const ref = value['@ref'];
212
- const parts = ref.split(':');
213
- value['@ref'] = `local:${rewriteShapeName(parts[1])}`;
214
- }
210
+ const refItem = getRef(context, value);
211
+
212
+ if (refItem) {
213
+ value['@ref'] = refItem.isForeign ? refItemToAtRef(refItem) : `local:${rewriteShapeName(refItemToShapeName(refItem))}`;
215
214
 
216
- if (value.$ref) {
217
- const ref = value.$ref;
218
- const parts = ref.split('/');
219
- parts[2] = rewriteShapeName(parts[2]);
220
- value.$ref = parts.join('/');
215
+ if (value.$ref) {
216
+ delete value.$ref;
217
+ }
221
218
  }
222
219
  }
223
220
  });
@@ -246,7 +243,7 @@ export function getMutationArgs(templateName, verb) {
246
243
  rewrittenSchema = createInputSchema(shape.schema, {
247
244
  isUpdate
248
245
  });
249
- inputShapeSchema = rewriteRefs(rewrittenSchema, getInputShapeName);
246
+ inputShapeSchema = rewriteRefs(projectSchema, rewrittenSchema, getInputShapeName);
250
247
  }
251
248
 
252
249
  const inputShape = createShape(inputName, mergeObjectSchemas(shouldRequireId ? idSchema : undefined, inputShapeSchema), {
@@ -286,7 +283,7 @@ export function getMutationArgs(templateName, verb) {
286
283
  }).map(shapeName => {
287
284
  const shape = projectSchema.shapes[shapeName]; // We only allow partial input at the top-level
288
285
 
289
- return createShape(getInputShapeName(shapeName), rewriteRefs(createInputSchema(shape.schema), getInputShapeName));
286
+ return createShape(getInputShapeName(shapeName), rewriteRefs(projectSchema, createInputSchema(shape.schema), getInputShapeName));
290
287
  }), 'name')),
291
288
  ...(addStructure ? {
292
289
  ContentStructureInput
@@ -0,0 +1,38 @@
1
+ import { isArraySchema, isObjectSchema, isRefSchema } from '../types';
2
+ import { followRef, getRef, refItemToShape } from '../refs';
3
+ /**
4
+ *
5
+ * @param projectSchema
6
+ * @param schema
7
+ * @param queryPath
8
+ *
9
+ * @example
10
+ * findShapeAtPath(projectSchema, projectSchema.shapes.Shopify_ProductConnection, ['edges', 'node']) // finds Shopify_Product
11
+ */
12
+
13
+ export function findShapeAtPath(projectSchema, schema, queryPath) {
14
+ const [prop, ...rest] = queryPath;
15
+
16
+ if (isRefSchema(schema)) {
17
+ if (queryPath.length === 0) {
18
+ const refItem = getRef(projectSchema, schema);
19
+ return refItem && refItemToShape(projectSchema, refItem);
20
+ }
21
+
22
+ return findShapeAtPath(projectSchema, followRef(projectSchema, schema), queryPath);
23
+ }
24
+
25
+ if (isArraySchema(schema)) {
26
+ return findShapeAtPath(projectSchema, schema.items, queryPath);
27
+ }
28
+
29
+ if (isObjectSchema(schema)) {
30
+ const propSchema = schema.properties[prop];
31
+
32
+ if (propSchema) {
33
+ return findShapeAtPath(projectSchema, propSchema, rest);
34
+ }
35
+ }
36
+
37
+ return undefined;
38
+ }
@@ -0,0 +1,60 @@
1
+ import { dereferenceSchema, getRef, refItemToAtRef } from '../refs';
2
+ import { isDefined, setIsEqual } from '@takeshape/util';
3
+ import { isObjectSchema } from '../types';
4
+ /**
5
+ * Normalize ref, oneOf and array schemas and get a list of @ref expressions
6
+ */
7
+
8
+ function getRefStrings(context, propertySchema) {
9
+ const schema = propertySchema.items ?? propertySchema;
10
+ const schemas = schema.oneOf ? schema.oneOf : [schema];
11
+ return schemas.map(propertySchema => {
12
+ const ref = getRef(context, propertySchema);
13
+ return ref ? refItemToAtRef(ref) : undefined;
14
+ }).filter(isDefined);
15
+ }
16
+
17
+ function isRefEqual(context, a, b) {
18
+ return setIsEqual(getRefStrings(context, a), getRefStrings(context, b));
19
+ }
20
+ /**
21
+ * Given a list of shapes find the conflicting props
22
+ */
23
+
24
+
25
+ export function getConflictingProperties(context, shapes) {
26
+ const conflicts = new Set();
27
+ const allProps = {};
28
+
29
+ for (const shape of Object.values(shapes)) {
30
+ const schema = dereferenceSchema(context, shape.schema, ['shapes', shape.name]);
31
+
32
+ if (!isObjectSchema(schema)) {
33
+ continue;
34
+ }
35
+
36
+ const required = new Set(schema.required ?? []);
37
+
38
+ for (const [name, prop] of Object.entries(schema.properties)) {
39
+ // skip props that we know already have conflict
40
+ if (conflicts.has(name)) {
41
+ continue;
42
+ }
43
+
44
+ const isRequired = required.has(name);
45
+
46
+ if (allProps[name]) {
47
+ if ( // same type
48
+ allProps[name][0].type !== prop.type || // same refs
49
+ !isRefEqual(context, allProps[name][0], prop) || // same nullability
50
+ isRequired !== allProps[name][1]) {
51
+ conflicts.add(name);
52
+ }
53
+ } else {
54
+ allProps[name] = [prop, isRequired];
55
+ }
56
+ }
57
+ }
58
+
59
+ return conflicts;
60
+ }
@@ -0,0 +1,40 @@
1
+ import { getRef, parseReturnShape, refItemToShape } from '../refs';
2
+
3
+ function isQuery(propOrQuery) {
4
+ return 'shape' in propOrQuery && 'resolver' in propOrQuery;
5
+ }
6
+
7
+ function getReturnShapeHelper(projectSchema, propOrQuery) {
8
+ let shape;
9
+
10
+ if (isQuery(propOrQuery)) {
11
+ const {
12
+ shapeName
13
+ } = parseReturnShape(projectSchema, propOrQuery.shape);
14
+ shape = projectSchema.shapes[shapeName];
15
+ } else {
16
+ const ref = getRef(projectSchema, propOrQuery);
17
+
18
+ if (ref) {
19
+ shape = refItemToShape(projectSchema, ref);
20
+ }
21
+ }
22
+
23
+ if (!shape) {
24
+ throw new Error(`Unable to find return shape for ${JSON.stringify(propOrQuery)}`);
25
+ }
26
+
27
+ return shape;
28
+ }
29
+
30
+ const cache = new WeakMap();
31
+ export function getReturnShape(projectSchema, propOrQuery) {
32
+ let result = cache.get(propOrQuery);
33
+
34
+ if (!result) {
35
+ result = getReturnShapeHelper(projectSchema, propOrQuery);
36
+ cache.set(propOrQuery, result);
37
+ }
38
+
39
+ return result;
40
+ }
@@ -0,0 +1,14 @@
1
+ import { createSchemaPropertyAccessor } from '../schema-util';
2
+ export function hasArg(projectSchema, prop, argName) {
3
+ var _projectSchema$shapes;
4
+
5
+ const args = 'args' in prop ? prop.args : prop['@args'];
6
+
7
+ if (!args) {
8
+ return false;
9
+ }
10
+
11
+ const schema = typeof args === 'string' ? (_projectSchema$shapes = projectSchema.shapes[args]) === null || _projectSchema$shapes === void 0 ? void 0 : _projectSchema$shapes.schema : args;
12
+ const schemaPropertyAccessor = createSchemaPropertyAccessor(projectSchema, schema);
13
+ return Boolean(schemaPropertyAccessor.getValue(argName));
14
+ }
package/es/util/index.js CHANGED
@@ -1,4 +1,8 @@
1
1
  export * from './api-indexing';
2
2
  export * from './detect-cycles';
3
+ export * from './find-shape-at-path';
4
+ export * from './get-conflicting-properties';
5
+ export * from './get-return-shape';
6
+ export * from './has-arg';
3
7
  export * from './merge';
4
8
  export * from './form-config';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@takeshape/schema",
3
- "version": "8.161.0",
3
+ "version": "8.166.0",
4
4
  "description": "TakeShape Schema",
5
5
  "homepage": "https://www.takeshape.io",
6
6
  "repository": {
@@ -21,9 +21,9 @@
21
21
  "examples"
22
22
  ],
23
23
  "dependencies": {
24
- "@takeshape/errors": "8.161.0",
25
- "@takeshape/json-schema": "8.161.0",
26
- "@takeshape/util": "8.161.0",
24
+ "@takeshape/errors": "8.166.0",
25
+ "@takeshape/json-schema": "8.166.0",
26
+ "@takeshape/util": "8.166.0",
27
27
  "ajv": "^8.10.0",
28
28
  "ajv-formats": "^2.1.1",
29
29
  "blueimp-md5": "^2.10.0",