@takeshape/schema 7.194.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.
- package/README.md +5 -0
- package/es/api-version.js +21 -0
- package/es/auth-schemas.js +0 -0
- package/es/builtin-schema.js +590 -0
- package/es/content-schema-transform.js +192 -0
- package/es/entitlements.js +0 -0
- package/es/enum.js +11 -0
- package/es/get-is-leaf.js +58 -0
- package/es/index.js +25 -0
- package/es/migration.js +413 -0
- package/es/mocks.js +47 -0
- package/es/patterns.js +0 -0
- package/es/project-schema/index.js +10 -0
- package/es/project-schema/v1.0.0.js +0 -0
- package/es/project-schema/v3.0.0.js +0 -0
- package/es/project-schema/v3.1.0.js +0 -0
- package/es/project-schema/v3.2.0.js +0 -0
- package/es/project-schema/v3.3.0.js +0 -0
- package/es/project-schema/v3.4.0.js +0 -0
- package/es/project-schema/v3.5.0.js +0 -0
- package/es/project-schema/v3.5.1.js +0 -0
- package/es/project-schema/v3.6.0.js +0 -0
- package/es/refs.js +301 -0
- package/es/resolvers.js +2 -0
- package/es/scalars.js +4 -0
- package/es/schema-util.js +1197 -0
- package/es/schemas/auth-schemas.json +313 -0
- package/es/schemas/project-schema/meta-schema-v1.0.0.json +209 -0
- package/es/schemas/project-schema/meta-schema-v3.0.0.json +609 -0
- package/es/schemas/project-schema/meta-schema-v3.1.0.json +609 -0
- package/es/schemas/project-schema/meta-schema-v3.2.0.json +613 -0
- package/es/schemas/project-schema/meta-schema-v3.3.0.json +613 -0
- package/es/schemas/project-schema/meta-schema-v3.4.0.json +613 -0
- package/es/schemas/project-schema/meta-schema-v3.5.0.json +627 -0
- package/es/schemas/project-schema/meta-schema-v3.5.1.json +630 -0
- package/es/schemas/project-schema/meta-schema-v3.6.0.json +633 -0
- package/es/schemas/project-schema/v1.0.0.json +321 -0
- package/es/schemas/project-schema/v3.0.0.json +411 -0
- package/es/schemas/project-schema/v3.1.0.json +569 -0
- package/es/schemas/project-schema/v3.2.0.json +577 -0
- package/es/schemas/project-schema/v3.3.0.json +578 -0
- package/es/schemas/project-schema/v3.4.0.json +568 -0
- package/es/schemas/project-schema/v3.5.0.json +568 -0
- package/es/schemas/project-schema/v3.5.1.json +568 -0
- package/es/schemas/project-schema/v3.6.0.json +584 -0
- package/es/schemas/project-schema.json +16 -0
- package/es/schemas/user-schema.json +58 -0
- package/es/services.js +379 -0
- package/es/taxonomies.js +55 -0
- package/es/template-shapes.js +79 -0
- package/es/types.js +202 -0
- package/es/unions.js +137 -0
- package/es/user-schema.js +0 -0
- package/es/util/detect-cycles.js +54 -0
- package/es/util/index.js +1 -0
- package/es/validate.js +764 -0
- package/es/versions.js +4 -0
- package/es/workflows.js +69 -0
- package/examples/.pnpm-debug.log +20 -0
- package/examples/v1_0_0/beer-schema.json +125 -0
- package/examples/v1_0_0/blog-schema.json +192 -0
- package/examples/v1_0_0/brewery-schema.json +232 -0
- package/examples/v1_0_0/complex-project-schema.json +3197 -0
- package/examples/v1_0_0/error-schema.json +275 -0
- package/examples/v1_0_0/frank-and-fred-schema.json +21387 -0
- package/examples/v1_0_0/massive-schema.json +6930 -0
- package/examples/v1_0_0/post-schema.json +83 -0
- package/examples/v1_0_0/real-world-schema.json +797 -0
- package/examples/v1_0_0/recursive-repeater-schema.json +69 -0
- package/examples/v1_0_0/recursive-schema.json +79 -0
- package/examples/v1_0_0/schema-with-repeater-draftjs.json +292 -0
- package/examples/v1_0_0/shape-books.json +797 -0
- package/examples/v1_0_0/user-schema-no-required.json +84 -0
- package/examples/v1_0_0/user-schema-with-defaults.json +105 -0
- package/examples/v1_0_0/user-schema-with-obj-prop.json +92 -0
- package/examples/v1_0_0/user-schema.json +87 -0
- package/examples/v3_0_0/rick-and-morty-rest.json +62 -0
- package/examples/v3_0_0/shopify-lookbook.json +1172 -0
- package/examples/v3_0_0/shopify-store-with-widget.json +11323 -0
- package/examples/v3_2_0/blog-schema.json +378 -0
- package/examples/v3_2_0/brewery-schema.json +385 -0
- package/examples/v3_2_0/complex-project-schema.json +3748 -0
- package/examples/v3_2_0/frank-and-fred-schema.json +19217 -0
- package/examples/v3_2_0/massive-schema.json +6676 -0
- package/examples/v3_2_0/pet-oneof-array.json +323 -0
- package/examples/v3_2_0/post-schema.json +265 -0
- package/examples/v3_2_0/real-world-schema.json +970 -0
- package/examples/v3_2_0/recursive-repeater-schema.json +144 -0
- package/examples/v3_2_0/recursive-schema.json +151 -0
- package/examples/v3_2_0/rick-and-morty-rest.json +65 -0
- package/examples/v3_2_0/schema-with-repeater-draftjs.json +381 -0
- package/examples/v3_2_0/shape-books.json +1044 -0
- package/examples/v3_2_0/shopify-store-with-widget.json +11016 -0
- package/examples/v3_2_0/user-schema-no-required.json +174 -0
- package/examples/v3_2_0/user-schema-with-defaults.json +187 -0
- package/examples/v3_3_0/blog-schema.json +405 -0
- package/examples/v3_3_0/brewery-schema.json +406 -0
- package/examples/v3_3_0/complex-project-schema.json +4076 -0
- package/examples/v3_3_0/frank-and-fred-schema.json +20913 -0
- package/examples/v3_3_0/massive-schema.json +7309 -0
- package/examples/v3_3_0/pet-oneof-array.json +380 -0
- package/examples/v3_3_0/post-schema.json +248 -0
- package/examples/v3_3_0/real-world-schema.json +1046 -0
- package/examples/v3_3_0/recursive-repeater-schema.json +154 -0
- package/examples/v3_3_0/recursive-schema.json +161 -0
- package/examples/v3_3_0/rick-and-morty-rest.json +101 -0
- package/examples/v3_3_0/schema-with-repeater-draftjs.json +393 -0
- package/examples/v3_3_0/shape-books.json +1121 -0
- package/examples/v3_3_0/shopify-lookbook.json +1196 -0
- package/examples/v3_3_0/shopify-store-with-widget.json +11320 -0
- package/examples/v3_3_0/user-schema-no-required.json +185 -0
- package/examples/v3_3_0/user-schema-with-defaults.json +210 -0
- package/examples/v3_4_0/blog-schema.json +405 -0
- package/examples/v3_4_0/brewery-schema.json +406 -0
- package/examples/v3_4_0/complex-project-schema.json +4076 -0
- package/examples/v3_4_0/frank-and-fred-schema.json +20913 -0
- package/examples/v3_4_0/massive-schema.json +7309 -0
- package/examples/v3_4_0/pet-oneof-array.json +420 -0
- package/examples/v3_4_0/post-schema.json +248 -0
- package/examples/v3_4_0/real-world-schema.json +1046 -0
- package/examples/v3_4_0/recursive-repeater-schema.json +154 -0
- package/examples/v3_4_0/recursive-schema.json +161 -0
- package/examples/v3_4_0/rick-and-morty-rest.json +101 -0
- package/examples/v3_4_0/schema-with-repeater-draftjs.json +393 -0
- package/examples/v3_4_0/shape-books.json +1121 -0
- package/examples/v3_4_0/shopify-lookbook.json +1196 -0
- package/examples/v3_4_0/shopify-store-with-widget.json +11323 -0
- package/examples/v3_4_0/user-schema-no-required.json +185 -0
- package/examples/v3_4_0/user-schema-with-defaults.json +210 -0
- package/examples/v3_5_0/blog-schema.json +405 -0
- package/examples/v3_5_0/brewery-schema.json +406 -0
- package/examples/v3_5_0/complex-project-schema.json +4076 -0
- package/examples/v3_5_0/frank-and-fred-schema.json +20913 -0
- package/examples/v3_5_0/massive-schema.json +7309 -0
- package/examples/v3_5_0/pet-oneof-array.json +420 -0
- package/examples/v3_5_0/post-schema.json +248 -0
- package/examples/v3_5_0/real-world-schema.json +1046 -0
- package/examples/v3_5_0/recursive-repeater-schema.json +154 -0
- package/examples/v3_5_0/recursive-schema.json +161 -0
- package/examples/v3_5_0/rick-and-morty-rest.json +101 -0
- package/examples/v3_5_0/schema-with-repeater-draftjs.json +393 -0
- package/examples/v3_5_0/shape-books.json +1121 -0
- package/examples/v3_5_0/shopify-lookbook.json +1196 -0
- package/examples/v3_5_0/shopify-store-with-widget.json +12271 -0
- package/examples/v3_5_0/user-schema-no-required.json +185 -0
- package/examples/v3_5_0/user-schema-with-defaults.json +210 -0
- package/examples/v3_5_1/blog-schema.json +405 -0
- package/examples/v3_5_1/brewery-schema.json +406 -0
- package/examples/v3_5_1/complex-project-schema.json +4076 -0
- package/examples/v3_5_1/frank-and-fred-schema.json +20913 -0
- package/examples/v3_5_1/massive-schema.json +7309 -0
- package/examples/v3_5_1/pet-oneof-array.json +420 -0
- package/examples/v3_5_1/post-schema.json +248 -0
- package/examples/v3_5_1/real-world-schema.json +1046 -0
- package/examples/v3_5_1/recursive-repeater-schema.json +154 -0
- package/examples/v3_5_1/recursive-schema.json +161 -0
- package/examples/v3_5_1/rick-and-morty-rest.json +101 -0
- package/examples/v3_5_1/schema-with-repeater-draftjs.json +393 -0
- package/examples/v3_5_1/shape-books.json +1121 -0
- package/examples/v3_5_1/shopify-lookbook.json +1196 -0
- package/examples/v3_5_1/shopify-store-with-widget.json +12271 -0
- package/examples/v3_5_1/stripe-starter-resolved.json +21419 -0
- package/examples/v3_5_1/user-schema-no-required.json +185 -0
- package/examples/v3_5_1/user-schema-with-defaults.json +210 -0
- package/examples/v3_6_0/blog-schema.json +405 -0
- package/examples/v3_6_0/brewery-schema.json +406 -0
- package/examples/v3_6_0/complex-project-schema.json +4076 -0
- package/examples/v3_6_0/frank-and-fred-schema.json +20913 -0
- package/examples/v3_6_0/massive-schema.json +7309 -0
- package/examples/v3_6_0/pet-oneof-array.json +420 -0
- package/examples/v3_6_0/post-schema.json +248 -0
- package/examples/v3_6_0/real-world-schema.json +1046 -0
- package/examples/v3_6_0/recursive-repeater-schema.json +154 -0
- package/examples/v3_6_0/recursive-schema.json +161 -0
- package/examples/v3_6_0/rick-and-morty-rest.json +101 -0
- package/examples/v3_6_0/schema-with-repeater-draftjs.json +393 -0
- package/examples/v3_6_0/shape-books.json +1121 -0
- package/examples/v3_6_0/shopify-lookbook.json +1196 -0
- package/examples/v3_6_0/shopify-store-with-widget.json +12271 -0
- package/examples/v3_6_0/stripe-starter-resolved.json +21419 -0
- package/examples/v3_6_0/user-schema-no-required.json +185 -0
- package/examples/v3_6_0/user-schema-with-defaults.json +210 -0
- package/lib/api-version.d.ts +6 -0
- package/lib/api-version.d.ts.map +1 -0
- package/lib/api-version.js +36 -0
- package/lib/auth-schemas.d.ts +105 -0
- package/lib/auth-schemas.d.ts.map +1 -0
- package/lib/auth-schemas.js +1 -0
- package/lib/builtin-schema.d.ts +8 -0
- package/lib/builtin-schema.d.ts.map +1 -0
- package/lib/builtin-schema.js +606 -0
- package/lib/content-schema-transform.d.ts +41 -0
- package/lib/content-schema-transform.d.ts.map +1 -0
- package/lib/content-schema-transform.js +217 -0
- package/lib/entitlements.d.ts +1 -0
- package/lib/entitlements.d.ts.map +1 -0
- package/lib/entitlements.js +1 -0
- package/lib/enum.d.ts +3 -0
- package/lib/enum.d.ts.map +1 -0
- package/lib/enum.js +18 -0
- package/lib/get-is-leaf.d.ts +4 -0
- package/lib/get-is-leaf.d.ts.map +1 -0
- package/lib/get-is-leaf.js +68 -0
- package/lib/index.d.ts +27 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +359 -0
- package/lib/migration.d.ts +55 -0
- package/lib/migration.d.ts.map +1 -0
- package/lib/migration.js +463 -0
- package/lib/mocks.d.ts +13 -0
- package/lib/mocks.d.ts.map +1 -0
- package/lib/mocks.js +61 -0
- package/lib/patterns.d.ts +13 -0
- package/lib/patterns.d.ts.map +1 -0
- package/lib/patterns.js +1 -0
- package/lib/project-schema/index.d.ts +21 -0
- package/lib/project-schema/index.d.ts.map +1 -0
- package/lib/project-schema/index.js +122 -0
- package/lib/project-schema/v1.0.0.d.ts +293 -0
- package/lib/project-schema/v1.0.0.d.ts.map +1 -0
- package/lib/project-schema/v1.0.0.js +1 -0
- package/lib/project-schema/v3.0.0.d.ts +480 -0
- package/lib/project-schema/v3.0.0.d.ts.map +1 -0
- package/lib/project-schema/v3.0.0.js +1 -0
- package/lib/project-schema/v3.1.0.d.ts +623 -0
- package/lib/project-schema/v3.1.0.d.ts.map +1 -0
- package/lib/project-schema/v3.1.0.js +1 -0
- package/lib/project-schema/v3.2.0.d.ts +634 -0
- package/lib/project-schema/v3.2.0.d.ts.map +1 -0
- package/lib/project-schema/v3.2.0.js +1 -0
- package/lib/project-schema/v3.3.0.d.ts +634 -0
- package/lib/project-schema/v3.3.0.d.ts.map +1 -0
- package/lib/project-schema/v3.3.0.js +1 -0
- package/lib/project-schema/v3.4.0.d.ts +626 -0
- package/lib/project-schema/v3.4.0.d.ts.map +1 -0
- package/lib/project-schema/v3.4.0.js +1 -0
- package/lib/project-schema/v3.5.0.d.ts +651 -0
- package/lib/project-schema/v3.5.0.d.ts.map +1 -0
- package/lib/project-schema/v3.5.0.js +1 -0
- package/lib/project-schema/v3.5.1.d.ts +651 -0
- package/lib/project-schema/v3.5.1.d.ts.map +1 -0
- package/lib/project-schema/v3.5.1.js +1 -0
- package/lib/project-schema/v3.6.0.d.ts +660 -0
- package/lib/project-schema/v3.6.0.d.ts.map +1 -0
- package/lib/project-schema/v3.6.0.js +1 -0
- package/lib/refs.d.ts +122 -0
- package/lib/refs.d.ts.map +1 -0
- package/lib/refs.js +359 -0
- package/lib/resolvers.d.ts +4 -0
- package/lib/resolvers.d.ts.map +1 -0
- package/lib/resolvers.js +10 -0
- package/lib/scalars.d.ts +3 -0
- package/lib/scalars.d.ts.map +1 -0
- package/lib/scalars.js +13 -0
- package/lib/schema-util.d.ts +201 -0
- package/lib/schema-util.d.ts.map +1 -0
- package/lib/schema-util.js +1353 -0
- package/lib/schemas/auth-schemas.json +313 -0
- package/lib/schemas/project-schema/meta-schema-v1.0.0.json +209 -0
- package/lib/schemas/project-schema/meta-schema-v3.0.0.json +609 -0
- package/lib/schemas/project-schema/meta-schema-v3.1.0.json +609 -0
- package/lib/schemas/project-schema/meta-schema-v3.2.0.json +613 -0
- package/lib/schemas/project-schema/meta-schema-v3.3.0.json +613 -0
- package/lib/schemas/project-schema/meta-schema-v3.4.0.json +613 -0
- package/lib/schemas/project-schema/meta-schema-v3.5.0.json +627 -0
- package/lib/schemas/project-schema/meta-schema-v3.5.1.json +630 -0
- package/lib/schemas/project-schema/meta-schema-v3.6.0.json +633 -0
- package/lib/schemas/project-schema/v1.0.0.json +321 -0
- package/lib/schemas/project-schema/v3.0.0.json +411 -0
- package/lib/schemas/project-schema/v3.1.0.json +569 -0
- package/lib/schemas/project-schema/v3.2.0.json +577 -0
- package/lib/schemas/project-schema/v3.3.0.json +578 -0
- package/lib/schemas/project-schema/v3.4.0.json +568 -0
- package/lib/schemas/project-schema/v3.5.0.json +568 -0
- package/lib/schemas/project-schema/v3.5.1.json +568 -0
- package/lib/schemas/project-schema/v3.6.0.json +584 -0
- package/lib/schemas/project-schema.json +16 -0
- package/lib/schemas/user-schema.json +58 -0
- package/lib/services.d.ts +71 -0
- package/lib/services.d.ts.map +1 -0
- package/lib/services.js +413 -0
- package/lib/taxonomies.d.ts +16 -0
- package/lib/taxonomies.d.ts.map +1 -0
- package/lib/taxonomies.js +69 -0
- package/lib/template-shapes.d.ts +32 -0
- package/lib/template-shapes.d.ts.map +1 -0
- package/lib/template-shapes.js +100 -0
- package/lib/types.d.ts +187 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +289 -0
- package/lib/unions.d.ts +29 -0
- package/lib/unions.d.ts.map +1 -0
- package/lib/unions.js +167 -0
- package/lib/user-schema.d.ts +39 -0
- package/lib/user-schema.d.ts.map +1 -0
- package/lib/user-schema.js +1 -0
- package/lib/util/detect-cycles.d.ts +6 -0
- package/lib/util/detect-cycles.d.ts.map +1 -0
- package/lib/util/detect-cycles.js +61 -0
- package/lib/util/index.d.ts +2 -0
- package/lib/util/index.d.ts.map +1 -0
- package/lib/util/index.js +18 -0
- package/lib/validate.d.ts +49 -0
- package/lib/validate.d.ts.map +1 -0
- package/lib/validate.js +823 -0
- package/lib/versions.d.ts +5 -0
- package/lib/versions.d.ts.map +1 -0
- package/lib/versions.js +14 -0
- package/lib/workflows.d.ts +15 -0
- package/lib/workflows.d.ts.map +1 -0
- package/lib/workflows.js +92 -0
- package/package.json +65 -0
|
@@ -0,0 +1,1197 @@
|
|
|
1
|
+
import find from 'lodash/find';
|
|
2
|
+
import mapValues from 'lodash/mapValues';
|
|
3
|
+
import omitBy from 'lodash/omitBy';
|
|
4
|
+
import omit from 'lodash/fp/omit';
|
|
5
|
+
import assign from 'lodash/fp/assign';
|
|
6
|
+
import pickBy from 'lodash/pickBy';
|
|
7
|
+
import get from 'lodash/get';
|
|
8
|
+
import union from 'lodash/union';
|
|
9
|
+
import keyBy from 'lodash/keyBy';
|
|
10
|
+
import isEmpty from 'lodash/isEmpty';
|
|
11
|
+
import isObject from 'lodash/isObject';
|
|
12
|
+
import curry from 'lodash/fp/curry';
|
|
13
|
+
import orderBy from 'lodash/fp/orderBy';
|
|
14
|
+
import uniq from 'lodash/uniq';
|
|
15
|
+
import { deepClone, isDefined } from '@takeshape/util';
|
|
16
|
+
import { mergeWithArrayConcat } from '@takeshape/util/lib/merge';
|
|
17
|
+
import { defaultWorkflow, getStatusField } from './workflows';
|
|
18
|
+
import { CURRENT_API_VERSION, CURRENT_SCHEMA_VERSION } from './versions';
|
|
19
|
+
import { workflowsEnabled } from './api-version';
|
|
20
|
+
import { builtInShapes, builtInForms } from './builtin-schema';
|
|
21
|
+
import { createTemplateShapeName, parseReturnShape } from './template-shapes';
|
|
22
|
+
import { getArgsType, isObjectSchema, isAllOfSchema, isRefSchema, isOneOfSchema, isContentObjectSchema, isBasicResolver } from './types';
|
|
23
|
+
import { getStoredServiceConfig } from './services';
|
|
24
|
+
import { followRef, getRef, getRefOrItemsRef, getRefShapeName, getRefWithPath, omitRefAndExtend, refExpressionToRefItem, refItemToShape, refItemToShapeName, refItemToShapeSchema } from './refs';
|
|
25
|
+
import isEqual from 'lodash/isEqual';
|
|
26
|
+
export const SERVICE_OBJECT_PATTERN_NAME = 'pattern:service-object';
|
|
27
|
+
export function isBuiltinShape(shapeName) {
|
|
28
|
+
return Boolean(builtInShapes[shapeName]);
|
|
29
|
+
}
|
|
30
|
+
const builtInQueriesAndMutations = getShapeQueriesAndMutations(Object.values(builtInShapes));
|
|
31
|
+
export function isBuiltinQuery(name) {
|
|
32
|
+
return builtInQueriesAndMutations.queries[name] !== undefined;
|
|
33
|
+
}
|
|
34
|
+
export function isBuiltinMutation(name) {
|
|
35
|
+
return builtInQueriesAndMutations.mutations[name] !== undefined;
|
|
36
|
+
}
|
|
37
|
+
export function isBuiltinType(projectSchema, refSchema) {
|
|
38
|
+
const shapeName = getRefShapeName(projectSchema, refSchema);
|
|
39
|
+
return shapeName !== undefined && isBuiltinShape(shapeName);
|
|
40
|
+
}
|
|
41
|
+
export const schemaMetadataKeys = ['$comment', 'title', 'description', '@l10n', '@deprecationReason', '@tag', '@tags', '@syncLocaleStructure', '@sensitive'];
|
|
42
|
+
export const commonSchemaKeys = ['type', 'default', 'readOnly', ...schemaMetadataKeys];
|
|
43
|
+
export const scalarSchemaKeys = [...commonSchemaKeys, 'format', 'oneOf', 'multipleOf', 'maximum', 'exclusiveMaximum', 'minimum', 'exclusiveMinimum', 'maxLength', 'minLength', 'pattern'];
|
|
44
|
+
export const objectSchemaKeys = [...commonSchemaKeys, 'properties', 'required'];
|
|
45
|
+
export const arraySchemaKeys = [...commonSchemaKeys, 'items', 'additionalItems', 'maxItems', 'minItems', 'uniqueItems'];
|
|
46
|
+
export const multipleRelationshipSchemaKeys = [...schemaMetadataKeys, 'maxItems', 'minItems']; // all properties except properties, items, additionalItems
|
|
47
|
+
|
|
48
|
+
export const nonStructuralSchemaKeys = [...scalarSchemaKeys, '@relationship', 'required', 'maxItems', 'minItems', 'uniqueItems'];
|
|
49
|
+
const commonProperties = {
|
|
50
|
+
_id: {
|
|
51
|
+
title: 'Id',
|
|
52
|
+
type: 'string'
|
|
53
|
+
},
|
|
54
|
+
_version: {
|
|
55
|
+
title: 'Version',
|
|
56
|
+
type: 'integer'
|
|
57
|
+
},
|
|
58
|
+
_shapeId: {
|
|
59
|
+
title: 'Shape Id',
|
|
60
|
+
type: 'string'
|
|
61
|
+
},
|
|
62
|
+
_shapeName: {
|
|
63
|
+
title: 'Shape Name',
|
|
64
|
+
type: 'string'
|
|
65
|
+
},
|
|
66
|
+
_createdAt: {
|
|
67
|
+
title: 'Created',
|
|
68
|
+
type: 'string',
|
|
69
|
+
format: 'date-time'
|
|
70
|
+
},
|
|
71
|
+
_createdBy: {
|
|
72
|
+
title: 'Created By',
|
|
73
|
+
type: 'string',
|
|
74
|
+
'@user': true
|
|
75
|
+
},
|
|
76
|
+
_updatedAt: {
|
|
77
|
+
title: 'Updated',
|
|
78
|
+
type: 'string',
|
|
79
|
+
format: 'date-time'
|
|
80
|
+
},
|
|
81
|
+
_updatedBy: {
|
|
82
|
+
title: 'Updated By',
|
|
83
|
+
type: 'string',
|
|
84
|
+
'@user': true
|
|
85
|
+
},
|
|
86
|
+
_schemaVersion: {
|
|
87
|
+
title: 'Schema Version',
|
|
88
|
+
type: 'number'
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
export function getServiceIdFieldName(serviceFieldName) {
|
|
92
|
+
return `${serviceFieldName}Id`;
|
|
93
|
+
}
|
|
94
|
+
const generatorVersions = {
|
|
95
|
+
[SERVICE_OBJECT_PATTERN_NAME]: '1'
|
|
96
|
+
};
|
|
97
|
+
export function getGeneratorVersion(generator) {
|
|
98
|
+
return generatorVersions[generator];
|
|
99
|
+
} // Returns info for all services found in shape, keyed by provider
|
|
100
|
+
|
|
101
|
+
export function getServiceInfo(projectSchema, shape) {
|
|
102
|
+
const result = {};
|
|
103
|
+
const depShapes = getShapeDependencies(projectSchema, shape).map(name => projectSchema.shapes[name]);
|
|
104
|
+
const shapesToCheck = [shape, ...depShapes];
|
|
105
|
+
shapesToCheck.forEach(thisShape => {
|
|
106
|
+
createSchemaPropertyList(projectSchema, thisShape.schema).forEach(([key, value]) => {
|
|
107
|
+
const resolver = value['@resolver'];
|
|
108
|
+
|
|
109
|
+
if (!isBasicResolver(resolver)) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const serviceConfig = getStoredServiceConfig(projectSchema, resolver.service);
|
|
114
|
+
|
|
115
|
+
if (!serviceConfig) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const provider = serviceConfig.provider;
|
|
120
|
+
const generators = {};
|
|
121
|
+
const tag = value['@tag'];
|
|
122
|
+
|
|
123
|
+
if (tag !== null && tag !== void 0 && tag.startsWith(SERVICE_OBJECT_PATTERN_NAME)) {
|
|
124
|
+
var _resolver$options;
|
|
125
|
+
|
|
126
|
+
const idFieldName = getServiceIdFieldName(key);
|
|
127
|
+
const version = tag.substr(tag.lastIndexOf(':') + 1);
|
|
128
|
+
generators[SERVICE_OBJECT_PATTERN_NAME] = {
|
|
129
|
+
version,
|
|
130
|
+
currentVersion: getGeneratorVersion(SERVICE_OBJECT_PATTERN_NAME),
|
|
131
|
+
idFieldName,
|
|
132
|
+
serviceObjectType: (_resolver$options = resolver.options) === null || _resolver$options === void 0 ? void 0 : _resolver$options.fieldName
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const serviceInfo = {
|
|
137
|
+
service: resolver.service,
|
|
138
|
+
fieldName: key,
|
|
139
|
+
path: pathBetween(shape, thisShape, projectSchema),
|
|
140
|
+
generators
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
if (Array.isArray(result[provider])) {
|
|
144
|
+
// dedupe service info
|
|
145
|
+
if (!result[provider].find(existingServiceInfo => isEqual(existingServiceInfo, serviceInfo))) {
|
|
146
|
+
result[provider].push(serviceInfo);
|
|
147
|
+
}
|
|
148
|
+
} else {
|
|
149
|
+
result[provider] = [serviceInfo];
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
return result;
|
|
154
|
+
} // Follow refs to find the path between two shapes in a schema
|
|
155
|
+
|
|
156
|
+
export function pathBetween(fromShape, toShape, projectSchema) {
|
|
157
|
+
if (fromShape.name === toShape.name) {
|
|
158
|
+
return [];
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const toShapeName = toShape.name;
|
|
162
|
+
let shortestPath;
|
|
163
|
+
let shortestPathLength = Number.MAX_SAFE_INTEGER;
|
|
164
|
+
|
|
165
|
+
const pathBetweenHelper = (schema, pathSoFar, seen) => {
|
|
166
|
+
const refItem = getRefOrItemsRef(projectSchema, schema);
|
|
167
|
+
|
|
168
|
+
if (refItem) {
|
|
169
|
+
const referenced = refItemToShape(projectSchema, refItem);
|
|
170
|
+
|
|
171
|
+
if (referenced) {
|
|
172
|
+
if (referenced.name === toShapeName && pathSoFar.length < shortestPathLength) {
|
|
173
|
+
shortestPath = pathSoFar;
|
|
174
|
+
shortestPathLength = pathSoFar.length;
|
|
175
|
+
} else if (!seen.has(referenced.name)) {
|
|
176
|
+
pathBetweenHelper(referenced.schema, pathSoFar, new Set([...seen, referenced.name]));
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
} else if (pathSoFar.length + 1 < shortestPathLength && (schema.oneOf || schema.allOf || schema.properties)) {
|
|
180
|
+
if (schema.oneOf) {
|
|
181
|
+
for (const itemSchema of schema.oneOf) {
|
|
182
|
+
pathBetweenHelper(itemSchema, pathSoFar, seen);
|
|
183
|
+
}
|
|
184
|
+
} else {
|
|
185
|
+
for (const [propName, propSchema] of createSchemaPropertyList(projectSchema, schema).getNodes()) {
|
|
186
|
+
pathBetweenHelper(propSchema, pathSoFar.concat(String(propName)), seen);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
pathBetweenHelper(fromShape.schema, [], new Set([fromShape.name]));
|
|
193
|
+
|
|
194
|
+
if (!shortestPath) {
|
|
195
|
+
throw new Error(`No path between ${fromShape.name} and ${toShape.name}`);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return shortestPath;
|
|
199
|
+
}
|
|
200
|
+
export function getShapeQueriesAndMutations(shapeList) {
|
|
201
|
+
const queries = {};
|
|
202
|
+
const mutations = {};
|
|
203
|
+
|
|
204
|
+
for (const shape of shapeList) {
|
|
205
|
+
const {
|
|
206
|
+
name,
|
|
207
|
+
model
|
|
208
|
+
} = shape;
|
|
209
|
+
const service = stringifyQuerySource('takeshape', 'local');
|
|
210
|
+
|
|
211
|
+
if (model) {
|
|
212
|
+
const isSingleton = model.type === 'single';
|
|
213
|
+
queries[`get${name}`] = {
|
|
214
|
+
shape: name,
|
|
215
|
+
resolver: {
|
|
216
|
+
name: 'takeshape:get',
|
|
217
|
+
service,
|
|
218
|
+
options: {
|
|
219
|
+
model: name
|
|
220
|
+
}
|
|
221
|
+
},
|
|
222
|
+
description: `Get a ${name} by ID`,
|
|
223
|
+
args: isSingleton ? createTemplateShapeName('TSGetSingletonArgs', name) : createTemplateShapeName('TSGetArgs', name)
|
|
224
|
+
};
|
|
225
|
+
mutations[`update${name}`] = {
|
|
226
|
+
shape: createTemplateShapeName('UpdateResult', name),
|
|
227
|
+
resolver: {
|
|
228
|
+
name: 'takeshape:update',
|
|
229
|
+
service,
|
|
230
|
+
options: {
|
|
231
|
+
model: name
|
|
232
|
+
}
|
|
233
|
+
},
|
|
234
|
+
description: `Update ${name}`,
|
|
235
|
+
args: createTemplateShapeName('UpdateArgs', name)
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
if (!isSingleton) {
|
|
239
|
+
queries[`get${name}List`] = {
|
|
240
|
+
shape: createTemplateShapeName('PaginatedList', name),
|
|
241
|
+
resolver: {
|
|
242
|
+
name: 'takeshape:list',
|
|
243
|
+
service,
|
|
244
|
+
options: {
|
|
245
|
+
model: name
|
|
246
|
+
}
|
|
247
|
+
},
|
|
248
|
+
description: `Returns a list ${name} in natural order.`,
|
|
249
|
+
args: createTemplateShapeName('TSListArgs', name)
|
|
250
|
+
};
|
|
251
|
+
mutations[`create${name}`] = {
|
|
252
|
+
shape: createTemplateShapeName('CreateResult', name),
|
|
253
|
+
resolver: {
|
|
254
|
+
name: 'takeshape:create',
|
|
255
|
+
service,
|
|
256
|
+
options: {
|
|
257
|
+
model: name
|
|
258
|
+
}
|
|
259
|
+
},
|
|
260
|
+
description: `Create ${name}`,
|
|
261
|
+
args: createTemplateShapeName('CreateArgs', name)
|
|
262
|
+
};
|
|
263
|
+
mutations[`duplicate${name}`] = {
|
|
264
|
+
shape: createTemplateShapeName('DuplicateResult', name),
|
|
265
|
+
resolver: {
|
|
266
|
+
name: 'takeshape:duplicate',
|
|
267
|
+
service,
|
|
268
|
+
options: {
|
|
269
|
+
model: name
|
|
270
|
+
}
|
|
271
|
+
},
|
|
272
|
+
description: `Duplicate ${name}`,
|
|
273
|
+
args: createTemplateShapeName('DuplicateArgs', name)
|
|
274
|
+
};
|
|
275
|
+
mutations[`delete${name}`] = {
|
|
276
|
+
shape: createTemplateShapeName('DeleteResult', name),
|
|
277
|
+
resolver: {
|
|
278
|
+
name: 'takeshape:delete',
|
|
279
|
+
service,
|
|
280
|
+
options: {
|
|
281
|
+
model: name
|
|
282
|
+
}
|
|
283
|
+
},
|
|
284
|
+
description: `Delete ${name}`,
|
|
285
|
+
args: createTemplateShapeName('DeleteArgs', name)
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
return {
|
|
292
|
+
queries,
|
|
293
|
+
mutations
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
export function getBuiltInProperties(apiVersion = CURRENT_API_VERSION) {
|
|
297
|
+
const showV1Deprecation = apiVersion !== '1';
|
|
298
|
+
return { ...commonProperties,
|
|
299
|
+
_enabled: {
|
|
300
|
+
title: 'Enabled',
|
|
301
|
+
type: 'boolean',
|
|
302
|
+
'@deprecationReason': showV1Deprecation ? 'Use _status instead' : undefined
|
|
303
|
+
},
|
|
304
|
+
_enabledAt: {
|
|
305
|
+
title: 'Enabled Date',
|
|
306
|
+
type: 'string',
|
|
307
|
+
format: 'date-time',
|
|
308
|
+
'@deprecationReason': showV1Deprecation ? 'Use a custom date field instead' : undefined
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Return all the built in property names,
|
|
314
|
+
*/
|
|
315
|
+
|
|
316
|
+
export const getBuiltInPropertyNames = (apiVersion = CURRENT_API_VERSION) => new Set([...Object.keys(getBuiltInProperties()), workflowsEnabled(apiVersion) ? '_status' : undefined].filter(isDefined));
|
|
317
|
+
export function applyBuiltinPropertiesToShape(projectSchema, shape) {
|
|
318
|
+
return { ...shape,
|
|
319
|
+
schema: { ...shape.schema,
|
|
320
|
+
properties: { ...omitBy(dereferenceObjectSchema(projectSchema, shape.schema).properties, (_, name) => name.startsWith('_')),
|
|
321
|
+
...getBuiltInProperties(projectSchema.apiVersion),
|
|
322
|
+
...(workflowsEnabled(projectSchema.apiVersion) && {
|
|
323
|
+
_status: getStatusField(projectSchema.workflows, shape)
|
|
324
|
+
})
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
export function mergeSchemaProperties(base, overrides) {
|
|
330
|
+
const result = { ...base,
|
|
331
|
+
properties: { ...base.properties,
|
|
332
|
+
...overrides.properties
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
const required = union(base.required, overrides.required);
|
|
336
|
+
|
|
337
|
+
if (required.length) {
|
|
338
|
+
result.required = required;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
return result;
|
|
342
|
+
}
|
|
343
|
+
export function mergeFormProperties(base, overrides) {
|
|
344
|
+
const result = { ...base,
|
|
345
|
+
properties: { ...base.properties,
|
|
346
|
+
...overrides.properties
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
const order = union(base.order, overrides.order);
|
|
350
|
+
|
|
351
|
+
if (order.length) {
|
|
352
|
+
result.order = order;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
return result;
|
|
356
|
+
}
|
|
357
|
+
export function applyDefaultsToSchema(projectSchema) {
|
|
358
|
+
return { ...projectSchema,
|
|
359
|
+
workflows: { ...projectSchema.workflows,
|
|
360
|
+
...(workflowsEnabled(projectSchema.apiVersion) && {
|
|
361
|
+
default: defaultWorkflow
|
|
362
|
+
})
|
|
363
|
+
},
|
|
364
|
+
queries: { ...builtInQueriesAndMutations.queries,
|
|
365
|
+
...projectSchema.queries
|
|
366
|
+
},
|
|
367
|
+
mutations: { ...builtInQueriesAndMutations.mutations,
|
|
368
|
+
...projectSchema.mutations
|
|
369
|
+
},
|
|
370
|
+
shapes: mapValues({ ...builtInShapes,
|
|
371
|
+
...projectSchema.shapes
|
|
372
|
+
}, shape => {
|
|
373
|
+
const builtInShape = builtInShapes[shape.name];
|
|
374
|
+
|
|
375
|
+
if (builtInShape && builtInShape !== shape) {
|
|
376
|
+
shape = { ...shape,
|
|
377
|
+
schema: mergeSchemaProperties(dereferenceObjectSchema(projectSchema, shape.schema), dereferenceObjectSchema(projectSchema, builtInShape.schema))
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
return isModelShape(shape) ? applyBuiltinPropertiesToShape(projectSchema, shape) : shape;
|
|
382
|
+
}),
|
|
383
|
+
forms: mapValues({ ...builtInForms,
|
|
384
|
+
...projectSchema.forms
|
|
385
|
+
}, (formsConfig, shapeName) => {
|
|
386
|
+
const builtInFormsConfig = builtInForms[shapeName];
|
|
387
|
+
|
|
388
|
+
if (builtInFormsConfig && builtInFormsConfig !== formsConfig) {
|
|
389
|
+
formsConfig = { ...formsConfig,
|
|
390
|
+
default: mergeFormProperties(formsConfig.default, builtInFormsConfig.default)
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
return formsConfig;
|
|
395
|
+
})
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
function addRelationship(relationships, id, env) {
|
|
400
|
+
if (!relationships[id]) {
|
|
401
|
+
relationships[id] = [];
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
relationships[id].push(env);
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Finds all relationships in the schema and returns an object indexed by shape.id that lists all of the shapes
|
|
408
|
+
* properties that reference it
|
|
409
|
+
*/
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
export function findExistingRelationships(projectSchema, shapes) {
|
|
413
|
+
const relationships = {};
|
|
414
|
+
const shapeArray = Object.values(shapes);
|
|
415
|
+
const shapesSeen = new Set();
|
|
416
|
+
const shapeIds = new Set(shapeArray.map(shape => shape.id));
|
|
417
|
+
|
|
418
|
+
const findRelationships = (schema, path) => {
|
|
419
|
+
const shapeName = getRefShapeName(projectSchema, schema);
|
|
420
|
+
|
|
421
|
+
if (shapeName && !shapes[shapeName]) {
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
const prop = shapeName ? followRef(projectSchema, schema) : schema;
|
|
426
|
+
|
|
427
|
+
if (prop['@relationship']) {
|
|
428
|
+
const {
|
|
429
|
+
shapeIds: relatedShapeIds,
|
|
430
|
+
relatedName
|
|
431
|
+
} = prop['@relationship'];
|
|
432
|
+
|
|
433
|
+
for (const shapeId of relatedShapeIds) {
|
|
434
|
+
if (shapeIds.has(shapeId)) {
|
|
435
|
+
addRelationship(relationships, shapeId, {
|
|
436
|
+
relatedName,
|
|
437
|
+
path: path.slice(1),
|
|
438
|
+
shapeId: path[0]
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
if (shapeName) {
|
|
445
|
+
// skip shapes we have already seen to avoid cycles
|
|
446
|
+
if (shapesSeen.has(shapeName)) {
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
shapesSeen.add(shapeName);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
if (prop.properties) {
|
|
454
|
+
for (const name of Object.keys(prop.properties)) {
|
|
455
|
+
findRelationships(prop.properties[name], path.concat(name));
|
|
456
|
+
}
|
|
457
|
+
} else if (prop.items) {
|
|
458
|
+
findRelationships(prop.items, path);
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
|
|
462
|
+
for (const shape of shapeArray) {
|
|
463
|
+
if (isModelShape(shape)) {
|
|
464
|
+
const {
|
|
465
|
+
schema,
|
|
466
|
+
id
|
|
467
|
+
} = shape;
|
|
468
|
+
findRelationships(schema, [id]);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
return relationships;
|
|
473
|
+
}
|
|
474
|
+
export function stringifyQuerySource(providerId, serviceId) {
|
|
475
|
+
return `${providerId}:${serviceId}`;
|
|
476
|
+
}
|
|
477
|
+
export function parseQuerySource(source) {
|
|
478
|
+
const [providerId, serviceId] = source.split(':');
|
|
479
|
+
return {
|
|
480
|
+
providerId,
|
|
481
|
+
serviceId
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
export function parseQueryResolver(resolver) {
|
|
485
|
+
const [providerId, verb] = resolver.split(':');
|
|
486
|
+
return {
|
|
487
|
+
providerId,
|
|
488
|
+
verb
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
export function getShapeById(projectSchema, shapeId) {
|
|
492
|
+
return find(projectSchema.shapes, shape => shape.id === shapeId);
|
|
493
|
+
}
|
|
494
|
+
export function isModelShape(shape) {
|
|
495
|
+
return Boolean(shape.model);
|
|
496
|
+
}
|
|
497
|
+
export function getArgsReference(projectSchema, argsSchema) {
|
|
498
|
+
var _argsSchema$Args;
|
|
499
|
+
|
|
500
|
+
const args = (_argsSchema$Args = argsSchema['@args']) !== null && _argsSchema$Args !== void 0 ? _argsSchema$Args : argsSchema.args;
|
|
501
|
+
|
|
502
|
+
if (args && typeof args === 'string') {
|
|
503
|
+
// @args: ShapeName OR Namespace_TypeName
|
|
504
|
+
return refExpressionToRefItem(projectSchema, args);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
export function getArgsReferenceWithPath(projectSchema, argsSchema, schemaPath = []) {
|
|
508
|
+
const refItem = getArgsReference(projectSchema, argsSchema);
|
|
509
|
+
|
|
510
|
+
if (refItem) {
|
|
511
|
+
const argsType = getArgsType(argsSchema);
|
|
512
|
+
return { ...refItem,
|
|
513
|
+
path: schemaPath.concat([argsType])
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
export function getArgsShapeSchema(projectSchema, argsSchema) {
|
|
518
|
+
var _argsSchema$Args2;
|
|
519
|
+
|
|
520
|
+
const args = (_argsSchema$Args2 = argsSchema['@args']) !== null && _argsSchema$Args2 !== void 0 ? _argsSchema$Args2 : argsSchema.args;
|
|
521
|
+
|
|
522
|
+
if (args && typeof args === 'string') {
|
|
523
|
+
const refItem = getArgsReference(projectSchema, argsSchema);
|
|
524
|
+
|
|
525
|
+
if (refItem) {
|
|
526
|
+
return refItemToShapeSchema(projectSchema, refItem);
|
|
527
|
+
}
|
|
528
|
+
} else if (args) {
|
|
529
|
+
return args;
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
/**
|
|
533
|
+
* Helper fn to omit `allOf` props from the target schema, and then extend it with the source schema.
|
|
534
|
+
*/
|
|
535
|
+
|
|
536
|
+
const omitAllOfAndExtend = (targetSchema, sourceSchema) => assign(omit(['allOf'], targetSchema), sourceSchema);
|
|
537
|
+
|
|
538
|
+
const createShapeToContentSchema = (projectSchema, definitions, definitionNames, useShapeIdDiscriminator = true) => {
|
|
539
|
+
const rewriteRefs = (shape, addDiscriminator = false) => {
|
|
540
|
+
const shapeSchemaCopy = dereferenceObjectSchema(projectSchema, deepClone(shape.schema));
|
|
541
|
+
|
|
542
|
+
if (useShapeIdDiscriminator) {
|
|
543
|
+
if (addDiscriminator && shapeSchemaCopy.properties) {
|
|
544
|
+
shapeSchemaCopy.properties._shapeId = {
|
|
545
|
+
type: 'string',
|
|
546
|
+
enum: [shape.id]
|
|
547
|
+
};
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
if (addDiscriminator && shape.title) {
|
|
551
|
+
shapeSchemaCopy.title = shape.title;
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
const handleSchema = (propSchema, addDiscriminator = false) => {
|
|
556
|
+
var _propSchema$items;
|
|
557
|
+
|
|
558
|
+
// unwrap array schema
|
|
559
|
+
propSchema = (_propSchema$items = propSchema.items) !== null && _propSchema$items !== void 0 ? _propSchema$items : propSchema;
|
|
560
|
+
|
|
561
|
+
if (isOneOfSchema(propSchema)) {
|
|
562
|
+
propSchema.oneOf.forEach(schema => {
|
|
563
|
+
handleSchema(schema, true);
|
|
564
|
+
});
|
|
565
|
+
return;
|
|
566
|
+
} // Add workflow validation
|
|
567
|
+
|
|
568
|
+
|
|
569
|
+
if (propSchema['@workflow']) {
|
|
570
|
+
const workflowName = propSchema['@workflow'];
|
|
571
|
+
const workflow = projectSchema.workflows[workflowName] || defaultWorkflow;
|
|
572
|
+
propSchema.oneOf = workflow.steps.map(step => ({
|
|
573
|
+
enum: [step.key, step.name],
|
|
574
|
+
title: step.title
|
|
575
|
+
}));
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
const refItem = getRef(projectSchema, propSchema);
|
|
579
|
+
|
|
580
|
+
if (refItem) {
|
|
581
|
+
const shapeName = refItemToShapeName(refItem);
|
|
582
|
+
const shape = refItemToShape(projectSchema, refItem);
|
|
583
|
+
|
|
584
|
+
if (shape) {
|
|
585
|
+
delete propSchema['@ref'];
|
|
586
|
+
propSchema.$ref = `#/definitions/${shapeName}`;
|
|
587
|
+
|
|
588
|
+
if (!definitionNames.has(shapeName)) {
|
|
589
|
+
definitionNames.add(shapeName);
|
|
590
|
+
definitions[shapeName] = rewriteRefs(shape, addDiscriminator);
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
};
|
|
595
|
+
|
|
596
|
+
Object.values(shapeSchemaCopy.properties).forEach(schema => {
|
|
597
|
+
handleSchema(schema);
|
|
598
|
+
});
|
|
599
|
+
return shapeSchemaCopy;
|
|
600
|
+
};
|
|
601
|
+
|
|
602
|
+
return rewriteRefs;
|
|
603
|
+
};
|
|
604
|
+
|
|
605
|
+
export function shapeToContentSchema(projectSchema, shape) {
|
|
606
|
+
const definitions = {};
|
|
607
|
+
const definitionNames = new Set();
|
|
608
|
+
const convert = createShapeToContentSchema(projectSchema, definitions, definitionNames);
|
|
609
|
+
return { ...convert(shape),
|
|
610
|
+
definitions
|
|
611
|
+
};
|
|
612
|
+
}
|
|
613
|
+
export function projectSchemaToJSONSchema(projectSchema) {
|
|
614
|
+
const definitions = {};
|
|
615
|
+
const definitionNames = new Set();
|
|
616
|
+
const convert = createShapeToContentSchema(projectSchema, definitions, definitionNames, false);
|
|
617
|
+
|
|
618
|
+
for (const shape of Object.values(projectSchema.shapes)) {
|
|
619
|
+
definitions[shape.name] = convert(shape);
|
|
620
|
+
definitionNames.add(shape.name);
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
return {
|
|
624
|
+
definitions
|
|
625
|
+
};
|
|
626
|
+
}
|
|
627
|
+
export function getShapeDependencies(projectSchema, shape) {
|
|
628
|
+
const shapeNames = new Set();
|
|
629
|
+
|
|
630
|
+
const findDependencies = schema => {
|
|
631
|
+
var _schema$items;
|
|
632
|
+
|
|
633
|
+
schema = (_schema$items = schema.items) !== null && _schema$items !== void 0 ? _schema$items : schema;
|
|
634
|
+
const refItem = getRef(projectSchema, schema);
|
|
635
|
+
|
|
636
|
+
if (refItem) {
|
|
637
|
+
const refShape = refItemToShape(projectSchema, refItem);
|
|
638
|
+
|
|
639
|
+
if (refShape && !shapeNames.has(refShape.name)) {
|
|
640
|
+
shapeNames.add(refShape.name);
|
|
641
|
+
findDependencies(dereferenceSchema(projectSchema, refShape.schema));
|
|
642
|
+
}
|
|
643
|
+
} else if (isObjectSchema(schema) || isAllOfSchema(schema) || isOneOfSchema(schema)) {
|
|
644
|
+
visitSchemaProperties(schema, [], findDependencies);
|
|
645
|
+
}
|
|
646
|
+
};
|
|
647
|
+
|
|
648
|
+
findDependencies(shape.schema);
|
|
649
|
+
return [...shapeNames];
|
|
650
|
+
}
|
|
651
|
+
export function getRelevantShapes(projectSchema, params) {
|
|
652
|
+
const {
|
|
653
|
+
shapeIds,
|
|
654
|
+
shapeNames,
|
|
655
|
+
byId = false,
|
|
656
|
+
includeDependencies = true
|
|
657
|
+
} = params;
|
|
658
|
+
|
|
659
|
+
if (shapeNames !== null && shapeNames !== void 0 && shapeNames.length || shapeIds !== null && shapeIds !== void 0 && shapeIds.length) {
|
|
660
|
+
const ids = new Set(shapeIds);
|
|
661
|
+
const names = new Set(shapeNames);
|
|
662
|
+
const shapes = {};
|
|
663
|
+
|
|
664
|
+
for (const shape of Object.values(projectSchema.shapes)) {
|
|
665
|
+
if (ids.has(shape.id) || names.has(shape.name)) {
|
|
666
|
+
const shapeNames = includeDependencies ? [shape.name].concat(getShapeDependencies(projectSchema, shape)) : [shape.name];
|
|
667
|
+
|
|
668
|
+
for (const shapeName of shapeNames) {
|
|
669
|
+
const dependentShape = projectSchema.shapes[shapeName];
|
|
670
|
+
shapes[byId ? dependentShape.id : dependentShape.name] = dependentShape;
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
return shapes;
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
return {};
|
|
679
|
+
}
|
|
680
|
+
export function visitSchemaProperties(schema, path, callback) {
|
|
681
|
+
if (isObjectSchema(schema)) {
|
|
682
|
+
for (const [name, propSchema] of Object.entries(schema.properties)) {
|
|
683
|
+
const newPath = [...path, 'properties', name];
|
|
684
|
+
callback(propSchema, newPath);
|
|
685
|
+
|
|
686
|
+
if (isObjectSchema(propSchema) || isAllOfSchema(propSchema) || isOneOfSchema(propSchema)) {
|
|
687
|
+
visitSchemaProperties(propSchema, newPath, callback);
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
} else if (isAllOfSchema(schema)) {
|
|
691
|
+
for (const [index, propSchema] of Object.entries(schema.allOf)) {
|
|
692
|
+
const newPath = [...path, 'allOf', index];
|
|
693
|
+
callback(propSchema, newPath);
|
|
694
|
+
|
|
695
|
+
if (isObjectSchema(propSchema) || isAllOfSchema(propSchema) || isOneOfSchema(propSchema)) {
|
|
696
|
+
visitSchemaProperties(propSchema, newPath, callback);
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
} else if (isOneOfSchema(schema)) {
|
|
700
|
+
for (const [index, propSchema] of Object.entries(schema.oneOf)) {
|
|
701
|
+
const newPath = [...path, 'oneOf', index];
|
|
702
|
+
callback(propSchema, newPath);
|
|
703
|
+
|
|
704
|
+
if (isObjectSchema(propSchema) || isAllOfSchema(propSchema) || isOneOfSchema(propSchema)) {
|
|
705
|
+
visitSchemaProperties(propSchema, newPath, callback);
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
} else {
|
|
709
|
+
callback(schema, path);
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
export function visitShapeProperties(shapes, callback) {
|
|
713
|
+
for (const [shapeName, shape] of Object.entries(shapes)) {
|
|
714
|
+
visitSchemaProperties(shape.schema, ['shapes', shapeName, 'schema'], callback);
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
function addRef(refs, ref, predicate) {
|
|
719
|
+
if (ref && (!predicate || predicate(ref))) {
|
|
720
|
+
refs.push(ref);
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
/**
|
|
724
|
+
* Walk through a `ProjectSchema` and collect all the `ref`s.
|
|
725
|
+
*/
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
export function getAllRefsInShapes(projectSchema, predicate) {
|
|
729
|
+
const items = [];
|
|
730
|
+
|
|
731
|
+
const shapeVisitor = (schema, schemaPath) => {
|
|
732
|
+
var _schema$items2;
|
|
733
|
+
|
|
734
|
+
if (schema['@args'] && typeof schema['@args'] === 'object') {
|
|
735
|
+
visitSchemaProperties(schema['@args'], schemaPath.concat(['@args']), (argSchema, argPath) => {
|
|
736
|
+
addRef(items, getRefOrItemsRef(projectSchema, argSchema, argPath), predicate);
|
|
737
|
+
});
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
addRef(items, getArgsReferenceWithPath(projectSchema, schema, schemaPath), predicate);
|
|
741
|
+
const newPath = schema.items ? schemaPath.concat('items') : schemaPath;
|
|
742
|
+
const newSchema = (_schema$items2 = schema.items) !== null && _schema$items2 !== void 0 ? _schema$items2 : schema;
|
|
743
|
+
|
|
744
|
+
if (isRefSchema(newSchema)) {
|
|
745
|
+
addRef(items, getRefOrItemsRef(projectSchema, newSchema, newPath), predicate);
|
|
746
|
+
} else if (isObjectSchema(newSchema) || isAllOfSchema(newSchema) || isOneOfSchema(newSchema)) {
|
|
747
|
+
visitSchemaProperties(newSchema, newPath, shapeVisitor);
|
|
748
|
+
}
|
|
749
|
+
};
|
|
750
|
+
|
|
751
|
+
for (const [shapeName, shape] of Object.entries(projectSchema.shapes)) {
|
|
752
|
+
visitSchemaProperties(shape.schema, ['shapes', shapeName, 'schema'], shapeVisitor);
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
return items;
|
|
756
|
+
}
|
|
757
|
+
/**
|
|
758
|
+
* Get all refs
|
|
759
|
+
*/
|
|
760
|
+
|
|
761
|
+
export function getAllRefsInQuery(projectSchema, queryPath, query, predicate) {
|
|
762
|
+
const refItems = [];
|
|
763
|
+
|
|
764
|
+
const propertyVisitor = (propSchema, propSchemaPath) => {
|
|
765
|
+
addRef(refItems, getRefOrItemsRef(projectSchema, propSchema, propSchemaPath), predicate);
|
|
766
|
+
};
|
|
767
|
+
|
|
768
|
+
if (query.args && typeof query.args === 'string') {
|
|
769
|
+
addRef(refItems, getArgsReferenceWithPath(projectSchema, query, queryPath), predicate);
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
if (query.args && typeof query.args === 'object') {
|
|
773
|
+
visitSchemaProperties(dereferenceObjectSchema(projectSchema, query.args), queryPath, propertyVisitor);
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
if (query.shape) {
|
|
777
|
+
const refItem = parseReturnShape(projectSchema, query.shape).ref;
|
|
778
|
+
|
|
779
|
+
if (refItem) {
|
|
780
|
+
addRef(refItems, { ...refItem,
|
|
781
|
+
path: queryPath.concat(['shape'])
|
|
782
|
+
}, predicate);
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
return refItems;
|
|
787
|
+
}
|
|
788
|
+
/**
|
|
789
|
+
* Collect refs from `queries` or `mutations`.
|
|
790
|
+
*/
|
|
791
|
+
|
|
792
|
+
export function getAllRefsInQueries(projectSchema, rootPropertyName, predicate) {
|
|
793
|
+
const refItems = [];
|
|
794
|
+
const rootPath = [rootPropertyName];
|
|
795
|
+
|
|
796
|
+
for (const [queryName, query] of Object.entries(projectSchema[rootPropertyName])) {
|
|
797
|
+
const queryPath = rootPath.concat([queryName]);
|
|
798
|
+
const queryRefs = getAllRefsInQuery(projectSchema, queryPath, query, predicate);
|
|
799
|
+
|
|
800
|
+
if (queryRefs.length) {
|
|
801
|
+
refItems.push(...queryRefs);
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
return refItems;
|
|
806
|
+
}
|
|
807
|
+
/**
|
|
808
|
+
* Get all refs in a ProjectSchema
|
|
809
|
+
* @param predicate return true to include the ref in
|
|
810
|
+
*/
|
|
811
|
+
|
|
812
|
+
export function getAllRefs(projectSchema, predicate) {
|
|
813
|
+
return [...getAllRefsInShapes(projectSchema, predicate), ...getAllRefsInQueries(projectSchema, 'queries', predicate), ...getAllRefsInQueries(projectSchema, 'mutations', predicate)];
|
|
814
|
+
}
|
|
815
|
+
export function parseMapping(mapping) {
|
|
816
|
+
const [providerId, serviceId, coordinate] = mapping.split(':');
|
|
817
|
+
const [shapeName, key] = coordinate.split('.');
|
|
818
|
+
return {
|
|
819
|
+
providerId,
|
|
820
|
+
serviceId,
|
|
821
|
+
shapeName,
|
|
822
|
+
key
|
|
823
|
+
};
|
|
824
|
+
}
|
|
825
|
+
export function splitMapping(mapping) {
|
|
826
|
+
return mapping.split('.');
|
|
827
|
+
}
|
|
828
|
+
export function findMapping(schema, predicate) {
|
|
829
|
+
const mapping = schema['@mapping'];
|
|
830
|
+
|
|
831
|
+
if (Array.isArray(mapping)) {
|
|
832
|
+
return mapping.find(predicate);
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
return mapping && predicate(mapping) ? mapping : undefined;
|
|
836
|
+
}
|
|
837
|
+
export function getFirstMapping(schema) {
|
|
838
|
+
return Array.isArray(schema['@mapping']) ? schema['@mapping'][0] : schema['@mapping'];
|
|
839
|
+
}
|
|
840
|
+
export function getFullyQualifiedShapeName(schema) {
|
|
841
|
+
const first = getFirstMapping(schema);
|
|
842
|
+
return first ? splitMapping(first)[0] : undefined;
|
|
843
|
+
}
|
|
844
|
+
export function getKey(schema) {
|
|
845
|
+
const first = getFirstMapping(schema);
|
|
846
|
+
return first ? splitMapping(first)[1] : undefined;
|
|
847
|
+
}
|
|
848
|
+
export function getPropertyService(schema) {
|
|
849
|
+
const first = getFirstMapping(schema);
|
|
850
|
+
const split = first === null || first === void 0 ? void 0 : first.split(':');
|
|
851
|
+
return split ? `${split[0]}:${split[1]}` : undefined;
|
|
852
|
+
}
|
|
853
|
+
export function emptySchema(projectId, dataKey) {
|
|
854
|
+
const now = new Date().toISOString();
|
|
855
|
+
return {
|
|
856
|
+
projectId,
|
|
857
|
+
dataKey,
|
|
858
|
+
version: 1,
|
|
859
|
+
created: now,
|
|
860
|
+
updated: now,
|
|
861
|
+
defaultLocale: 'en-us',
|
|
862
|
+
locales: ['en-us'],
|
|
863
|
+
apiVersion: '2',
|
|
864
|
+
schemaVersion: CURRENT_SCHEMA_VERSION,
|
|
865
|
+
queries: {},
|
|
866
|
+
mutations: {},
|
|
867
|
+
shapes: {},
|
|
868
|
+
workflows: {},
|
|
869
|
+
forms: {}
|
|
870
|
+
};
|
|
871
|
+
}
|
|
872
|
+
export function getShapesById(projectSchema) {
|
|
873
|
+
return keyBy(Object.values(projectSchema.shapes), 'id');
|
|
874
|
+
}
|
|
875
|
+
/**
|
|
876
|
+
* Walk through the project schema and return an array of all
|
|
877
|
+
* referenced shape names that are in a service namespace.
|
|
878
|
+
*
|
|
879
|
+
* @param projectSchema A V3 project schema.
|
|
880
|
+
*
|
|
881
|
+
* @returns A `string` array with all the shapes that are in namespaces.
|
|
882
|
+
*/
|
|
883
|
+
|
|
884
|
+
export function getAllNamespaceShapes(projectSchema) {
|
|
885
|
+
return uniq(getAllRefs(projectSchema, refItem => Boolean(refItem.serviceNamespace)).map(refItemToShapeName));
|
|
886
|
+
}
|
|
887
|
+
/**
|
|
888
|
+
* Returns a list of all shape names in a service namespace, excluding any
|
|
889
|
+
* that have been set directly in the project schema
|
|
890
|
+
*
|
|
891
|
+
* @todo It's unsound to discard dependencies in this way, should avoid
|
|
892
|
+
* collisions with aliases or similar any remote shape that collides,
|
|
893
|
+
* e.g., Shopify_Product -> Shopify_Product_2
|
|
894
|
+
*
|
|
895
|
+
* @param projectSchema A V3 project schema with remote service configs.
|
|
896
|
+
*
|
|
897
|
+
* @returns A `Set` of shape names.
|
|
898
|
+
*/
|
|
899
|
+
|
|
900
|
+
export function getNamespacedShapeNameList(projectSchema) {
|
|
901
|
+
const requiredShapeNames = new Set(); // All the shape names
|
|
902
|
+
|
|
903
|
+
const existingShapeNames = new Set(Object.keys(projectSchema.shapes)); // All the referenced shape names with namespaces that belong to a service
|
|
904
|
+
|
|
905
|
+
const namespacedShapeNames = new Set(getAllNamespaceShapes(projectSchema)); // Add all namespaced shapes to the schema if they are not already present,
|
|
906
|
+
// e.g. Shopify_Product
|
|
907
|
+
|
|
908
|
+
namespacedShapeNames.forEach(name => {
|
|
909
|
+
if (!existingShapeNames.has(name)) {
|
|
910
|
+
requiredShapeNames.add(name);
|
|
911
|
+
}
|
|
912
|
+
});
|
|
913
|
+
return requiredShapeNames;
|
|
914
|
+
}
|
|
915
|
+
/**
|
|
916
|
+
* Filter the service map to return only services passing the GraphQL Service
|
|
917
|
+
* typeguard.
|
|
918
|
+
*
|
|
919
|
+
* @param serviceMap A service map.
|
|
920
|
+
*
|
|
921
|
+
* @returns A new map with the filtered list of services
|
|
922
|
+
*/
|
|
923
|
+
|
|
924
|
+
export function filterGraphQLServiceMap(serviceMap) {
|
|
925
|
+
const filtered = new Map();
|
|
926
|
+
|
|
927
|
+
for (const [serviceKey, serviceConfig] of serviceMap) {
|
|
928
|
+
if (serviceConfig.serviceType === 'graphql') {
|
|
929
|
+
filtered.set(serviceKey, serviceConfig);
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
return filtered;
|
|
934
|
+
}
|
|
935
|
+
/**
|
|
936
|
+
* Add all referenced shape names to the refSet meant to be used as a callback to visitSchemaProperties
|
|
937
|
+
*/
|
|
938
|
+
|
|
939
|
+
export function collectReferencedShapeNames(projectSchema, refSet) {
|
|
940
|
+
const collect = schema => {
|
|
941
|
+
const args = schema['@args'];
|
|
942
|
+
|
|
943
|
+
if (args && isObject(args) && isObjectSchema(args)) {
|
|
944
|
+
Object.values(args.properties).forEach(collect);
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
const argsRefItem = getArgsReference(projectSchema, schema);
|
|
948
|
+
|
|
949
|
+
if (argsRefItem) {
|
|
950
|
+
refSet.add(refItemToShapeName(argsRefItem));
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
const refItem = getRefOrItemsRef(projectSchema, schema);
|
|
954
|
+
|
|
955
|
+
if (refItem) {
|
|
956
|
+
refSet.add(refItemToShapeName(refItem));
|
|
957
|
+
}
|
|
958
|
+
};
|
|
959
|
+
|
|
960
|
+
return collect;
|
|
961
|
+
}
|
|
962
|
+
/**
|
|
963
|
+
* Get the subset of a schema that was automatically created or should otherwise
|
|
964
|
+
* have special protection against editing or deleting.
|
|
965
|
+
*/
|
|
966
|
+
|
|
967
|
+
export function getProtectedSchema(schema) {
|
|
968
|
+
const shapeQueriesAndMutations = getShapeQueriesAndMutations(Object.values({ ...builtInShapes,
|
|
969
|
+
...schema.shapes
|
|
970
|
+
}));
|
|
971
|
+
const namespaceShapes = getAllNamespaceShapes(schema);
|
|
972
|
+
return {
|
|
973
|
+
shapes: pickBy(schema.shapes, (_, key) => {
|
|
974
|
+
return namespaceShapes.includes(key) || isBuiltinShape(key);
|
|
975
|
+
}),
|
|
976
|
+
queries: pickBy(schema.queries, (_, key) => {
|
|
977
|
+
return shapeQueriesAndMutations.queries[key] !== undefined;
|
|
978
|
+
}),
|
|
979
|
+
mutations: pickBy(schema.mutations, (_, key) => {
|
|
980
|
+
return shapeQueriesAndMutations.mutations[key] !== undefined;
|
|
981
|
+
})
|
|
982
|
+
};
|
|
983
|
+
}
|
|
984
|
+
/**
|
|
985
|
+
* Given a schema, will resolve any `allOf` or `ref` objects. This function has
|
|
986
|
+
* to fully resolve all refs, because attempting to extend an object schema with
|
|
987
|
+
* a ref property would create an invalid schema, potentially containing both
|
|
988
|
+
* a ref and properties.
|
|
989
|
+
*
|
|
990
|
+
* If it can't create a valid schema it returns the input.
|
|
991
|
+
*/
|
|
992
|
+
|
|
993
|
+
export function dereferenceSchema(context, shapeOrFieldSchema, schemaPath = []) {
|
|
994
|
+
var _resolveAll;
|
|
995
|
+
|
|
996
|
+
const resolveAll = (resolveSchema, resolvePath) => {
|
|
997
|
+
try {
|
|
998
|
+
if (isAllOfSchema(resolveSchema)) {
|
|
999
|
+
const allOfSchema = resolveSchema.allOf.map((stub, index) => resolveAll(stub, [...resolvePath, 'allOf', index])).reduce((prev, curr) => mergeWithArrayConcat(prev, curr), {});
|
|
1000
|
+
return isEmpty(allOfSchema) ? undefined : omitAllOfAndExtend(resolveSchema, allOfSchema);
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
if (isRefSchema(resolveSchema)) {
|
|
1004
|
+
const refItem = getRefWithPath(context, resolveSchema);
|
|
1005
|
+
|
|
1006
|
+
if (!refItem) {
|
|
1007
|
+
throw new Error('ref could not be parsed');
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
const refSchema = refItemToShapeSchema(context, refItem);
|
|
1011
|
+
|
|
1012
|
+
if (refSchema) {
|
|
1013
|
+
return resolveAll(omitRefAndExtend(resolveSchema, refSchema), refItem.path);
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
return;
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
return resolveSchema;
|
|
1020
|
+
} catch (err) {
|
|
1021
|
+
throw new Error(`error at '${resolvePath.join('.')}', ${err.message}`);
|
|
1022
|
+
}
|
|
1023
|
+
};
|
|
1024
|
+
|
|
1025
|
+
return (_resolveAll = resolveAll(shapeOrFieldSchema, schemaPath)) !== null && _resolveAll !== void 0 ? _resolveAll : shapeOrFieldSchema;
|
|
1026
|
+
}
|
|
1027
|
+
export function dereferenceObjectSchema(context, shapeOrFieldSchema, schemaPath = []) {
|
|
1028
|
+
const schema = dereferenceSchema(context, shapeOrFieldSchema, schemaPath);
|
|
1029
|
+
|
|
1030
|
+
if (!isObjectSchema(schema)) {
|
|
1031
|
+
throw new Error(`provided schema at '${schemaPath.join('.')}' could not be resolved to an object schema`);
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
return schema;
|
|
1035
|
+
}
|
|
1036
|
+
/**
|
|
1037
|
+
* Creates a schema property list chainable fn, allowing you to manipulate and
|
|
1038
|
+
* iterate the properties in a schema.
|
|
1039
|
+
*/
|
|
1040
|
+
|
|
1041
|
+
export const createSchemaPropertyList = curry((context, shapeOrFieldSchema) => {
|
|
1042
|
+
const schema = isContentObjectSchema(shapeOrFieldSchema) ? dereferenceSchema(context, shapeOrFieldSchema) : shapeOrFieldSchema;
|
|
1043
|
+
const propertyIterator = new Map(isObjectSchema(schema) ? Object.entries(schema.properties) : undefined);
|
|
1044
|
+
const defaultOrderByIteratees = [n => n[0]];
|
|
1045
|
+
const defaultOrderByOrders = ['asc'];
|
|
1046
|
+
|
|
1047
|
+
const _orderBy = (iteratees = defaultOrderByIteratees, orders = defaultOrderByOrders) => {
|
|
1048
|
+
self._propertyIterator = new Map(orderBy(iteratees, orders, Array.from(self._propertyIterator)));
|
|
1049
|
+
return self;
|
|
1050
|
+
};
|
|
1051
|
+
|
|
1052
|
+
const _sortWith = comparator => {
|
|
1053
|
+
self._propertyIterator = new Map(Array.from(self._propertyIterator).sort(comparator));
|
|
1054
|
+
return self;
|
|
1055
|
+
};
|
|
1056
|
+
|
|
1057
|
+
const _sortByNamesWithList = comparatorArray => {
|
|
1058
|
+
// If no array is provided, the original sort is preserved
|
|
1059
|
+
if (comparatorArray) {
|
|
1060
|
+
self._propertyIterator = new Map(Array.from(self._propertyIterator).filter(([name]) => comparatorArray.includes(name)).sort(([nameA], [nameB]) => {
|
|
1061
|
+
const indexA = comparatorArray.indexOf(nameA);
|
|
1062
|
+
const indexB = comparatorArray.indexOf(nameB);
|
|
1063
|
+
return (indexA > -1 ? indexA : Infinity) - (indexB > -1 ? indexB : Infinity);
|
|
1064
|
+
}));
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
return self;
|
|
1068
|
+
};
|
|
1069
|
+
|
|
1070
|
+
const _filterBy = predicate => {
|
|
1071
|
+
self._propertyIterator = new Map(Array.from(self._propertyIterator).filter(predicate));
|
|
1072
|
+
return self;
|
|
1073
|
+
};
|
|
1074
|
+
|
|
1075
|
+
const _forEach = iteratee => {
|
|
1076
|
+
Array.from(self._propertyIterator).forEach(iteratee);
|
|
1077
|
+
return self;
|
|
1078
|
+
};
|
|
1079
|
+
|
|
1080
|
+
const _getNodes = () => {
|
|
1081
|
+
return Array.from(self._propertyIterator);
|
|
1082
|
+
};
|
|
1083
|
+
|
|
1084
|
+
const _getObject = () => {
|
|
1085
|
+
return Array.from(self._propertyIterator).reduce((curr, node) => Object.assign(curr, {
|
|
1086
|
+
[node[0]]: node[1]
|
|
1087
|
+
}), {});
|
|
1088
|
+
};
|
|
1089
|
+
|
|
1090
|
+
const _getNames = () => {
|
|
1091
|
+
return Array.from(self._propertyIterator, ([name]) => name);
|
|
1092
|
+
};
|
|
1093
|
+
|
|
1094
|
+
const _getValues = () => {
|
|
1095
|
+
return Array.from(self._propertyIterator, ([, value]) => value);
|
|
1096
|
+
};
|
|
1097
|
+
|
|
1098
|
+
const self = {
|
|
1099
|
+
_propertyIterator: propertyIterator,
|
|
1100
|
+
orderBy: _orderBy,
|
|
1101
|
+
sortWith: _sortWith,
|
|
1102
|
+
sortByNamesWithList: _sortByNamesWithList,
|
|
1103
|
+
filterBy: _filterBy,
|
|
1104
|
+
forEach: _forEach,
|
|
1105
|
+
getNodes: _getNodes,
|
|
1106
|
+
getObject: _getObject,
|
|
1107
|
+
getNames: _getNames,
|
|
1108
|
+
getValues: _getValues
|
|
1109
|
+
};
|
|
1110
|
+
return self;
|
|
1111
|
+
});
|
|
1112
|
+
/**
|
|
1113
|
+
* Creates a schema property accessor, allowing you to safely access schema
|
|
1114
|
+
* properties.
|
|
1115
|
+
*/
|
|
1116
|
+
|
|
1117
|
+
export const createSchemaPropertyAccessor = curry((context, shapeOrFieldSchema) => {
|
|
1118
|
+
const schema = isContentObjectSchema(shapeOrFieldSchema) ? dereferenceSchema(context, shapeOrFieldSchema) : shapeOrFieldSchema;
|
|
1119
|
+
const propertyIterator = new Map(isObjectSchema(schema) ? Object.entries(schema.properties) : undefined);
|
|
1120
|
+
|
|
1121
|
+
const _getValue = propertyName => {
|
|
1122
|
+
return propertyIterator.get(propertyName);
|
|
1123
|
+
};
|
|
1124
|
+
|
|
1125
|
+
const _findValue = predicate => {
|
|
1126
|
+
var _Array$from$find;
|
|
1127
|
+
|
|
1128
|
+
return (_Array$from$find = Array.from(propertyIterator).find(predicate)) === null || _Array$from$find === void 0 ? void 0 : _Array$from$find[1];
|
|
1129
|
+
};
|
|
1130
|
+
|
|
1131
|
+
const _findName = predicate => {
|
|
1132
|
+
var _Array$from$find2;
|
|
1133
|
+
|
|
1134
|
+
return (_Array$from$find2 = Array.from(propertyIterator).find(predicate)) === null || _Array$from$find2 === void 0 ? void 0 : _Array$from$find2[0];
|
|
1135
|
+
};
|
|
1136
|
+
|
|
1137
|
+
const _getKey = propertyName => {
|
|
1138
|
+
const value = propertyIterator.get(propertyName);
|
|
1139
|
+
|
|
1140
|
+
if (value) {
|
|
1141
|
+
return getKey(value);
|
|
1142
|
+
}
|
|
1143
|
+
};
|
|
1144
|
+
|
|
1145
|
+
return {
|
|
1146
|
+
getValue: _getValue,
|
|
1147
|
+
findValue: _findValue,
|
|
1148
|
+
findName: _findName,
|
|
1149
|
+
getKey: _getKey
|
|
1150
|
+
};
|
|
1151
|
+
});
|
|
1152
|
+
|
|
1153
|
+
const findHelper = (projectSchema, shapeName, resolver, type) => {
|
|
1154
|
+
const schemaPath = type === 'query' ? 'queries' : 'mutations';
|
|
1155
|
+
const foundQuery = Object.keys(projectSchema[schemaPath]).find(queryName => {
|
|
1156
|
+
var _options;
|
|
1157
|
+
|
|
1158
|
+
const queryOrMutation = projectSchema[schemaPath][queryName];
|
|
1159
|
+
return queryOrMutation.resolver.name === resolver && ((_options = queryOrMutation.resolver.options) === null || _options === void 0 ? void 0 : _options.model) === shapeName;
|
|
1160
|
+
});
|
|
1161
|
+
|
|
1162
|
+
if (!foundQuery) {
|
|
1163
|
+
throw new Error(`Failed find a "${resolver}" ${type} for shape "${shapeName}"`);
|
|
1164
|
+
}
|
|
1165
|
+
|
|
1166
|
+
return foundQuery;
|
|
1167
|
+
};
|
|
1168
|
+
/**
|
|
1169
|
+
* Find the query in the schema that corresponds to the given shape and resolver.
|
|
1170
|
+
*/
|
|
1171
|
+
|
|
1172
|
+
|
|
1173
|
+
export const findQuery = (projectSchema, shapeName, resolver) => {
|
|
1174
|
+
return findHelper(projectSchema, shapeName, resolver, 'query');
|
|
1175
|
+
};
|
|
1176
|
+
/**
|
|
1177
|
+
* Find the query in the schema that corresponds to the given shape and resolver.
|
|
1178
|
+
*/
|
|
1179
|
+
|
|
1180
|
+
export const findMutation = (projectSchema, shapeName, resolver) => {
|
|
1181
|
+
return findHelper(projectSchema, shapeName, resolver, 'mutation');
|
|
1182
|
+
};
|
|
1183
|
+
/**
|
|
1184
|
+
* Find the form config for a given shape name and the form key.
|
|
1185
|
+
*/
|
|
1186
|
+
|
|
1187
|
+
export function findShapeFormConfig(projectSchema, shapeName, formName) {
|
|
1188
|
+
return get(projectSchema, ['forms', shapeName, formName]);
|
|
1189
|
+
}
|
|
1190
|
+
|
|
1191
|
+
/**
|
|
1192
|
+
* Find the query in the schema that corresponds to the given shape and resolver.
|
|
1193
|
+
*/
|
|
1194
|
+
export const getDefaultMutationName = (shapeName, resolver) => {
|
|
1195
|
+
const action = resolver.substring(resolver.indexOf(':') + 1);
|
|
1196
|
+
return `${action}${shapeName}`;
|
|
1197
|
+
};
|