@aws-amplify/data-schema 0.18.4 → 1.0.1
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/dist/cjs/CustomOperation.js +0 -5
- package/dist/cjs/CustomOperation.js.map +1 -1
- package/dist/cjs/ModelRelationalField.js +1 -5
- package/dist/cjs/ModelRelationalField.js.map +1 -1
- package/dist/cjs/SchemaProcessor.js +19 -7
- package/dist/cjs/SchemaProcessor.js.map +1 -1
- package/dist/esm/CustomOperation.d.ts +0 -16
- package/dist/esm/CustomOperation.mjs +0 -5
- package/dist/esm/CustomOperation.mjs.map +1 -1
- package/dist/esm/MappedTypes/CustomOperations.d.ts +0 -1
- package/dist/esm/ModelRelationalField.d.ts +3 -9
- package/dist/esm/ModelRelationalField.mjs +1 -5
- package/dist/esm/ModelRelationalField.mjs.map +1 -1
- package/dist/esm/ModelSchema.d.ts +3 -2
- package/dist/esm/SchemaProcessor.mjs +19 -7
- package/dist/esm/SchemaProcessor.mjs.map +1 -1
- package/dist/meta/cjs.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/CustomOperation.ts +0 -29
- package/src/MappedTypes/CustomOperations.ts +0 -1
- package/src/ModelRelationalField.ts +3 -18
- package/src/ModelSchema.ts +4 -2
- package/src/SchemaProcessor.ts +37 -11
package/package.json
CHANGED
package/src/CustomOperation.ts
CHANGED
|
@@ -34,8 +34,6 @@ type SubscriptionSource = RefType<any, any>;
|
|
|
34
34
|
type InternalSubscriptionSource = InternalRef;
|
|
35
35
|
|
|
36
36
|
type CustomReturnType = RefType<any> | CustomType<any>;
|
|
37
|
-
type CustomFunctionRefType = string; // extend to include reference
|
|
38
|
-
|
|
39
37
|
type InternalCustomArguments = Record<string, InternalField>;
|
|
40
38
|
type InternalCustomReturnType = InternalRef;
|
|
41
39
|
type HandlerInputType = FunctionHandler[] | CustomHandler[] | Handler;
|
|
@@ -50,7 +48,6 @@ type CustomOperationName = (typeof CustomOperationNames)[number];
|
|
|
50
48
|
type CustomData = {
|
|
51
49
|
arguments: CustomArguments;
|
|
52
50
|
returnType: CustomReturnType | null;
|
|
53
|
-
functionRef: string | null; // extend to include reference
|
|
54
51
|
authorization: Authorization<any, any, any>[];
|
|
55
52
|
typeName: CustomOperationName;
|
|
56
53
|
handlers: Handler[] | null;
|
|
@@ -60,7 +57,6 @@ type CustomData = {
|
|
|
60
57
|
type InternalCustomData = CustomData & {
|
|
61
58
|
arguments: InternalCustomArguments;
|
|
62
59
|
returnType: InternalCustomReturnType;
|
|
63
|
-
functionRef: string | null;
|
|
64
60
|
subscriptionSource: InternalSubscriptionSource[];
|
|
65
61
|
authorization: Authorization<any, any, any>[];
|
|
66
62
|
};
|
|
@@ -68,7 +64,6 @@ type InternalCustomData = CustomData & {
|
|
|
68
64
|
export type CustomOperationParamShape = {
|
|
69
65
|
arguments: CustomArguments | null;
|
|
70
66
|
returnType: CustomReturnType | null;
|
|
71
|
-
functionRef: string | null;
|
|
72
67
|
authorization: Authorization<any, any, any>[];
|
|
73
68
|
typeName: CustomOperationName;
|
|
74
69
|
handlers: Handler | null;
|
|
@@ -94,21 +89,6 @@ export type CustomOperation<
|
|
|
94
89
|
K | 'returns',
|
|
95
90
|
B
|
|
96
91
|
>;
|
|
97
|
-
/**
|
|
98
|
-
*
|
|
99
|
-
* @deprecated
|
|
100
|
-
* `.function` should no longer be used and will be removed
|
|
101
|
-
* in the next minor version of this package.
|
|
102
|
-
*
|
|
103
|
-
* Use `.handler(a.handler.function())` instead
|
|
104
|
-
*/
|
|
105
|
-
function<FunctionRef extends CustomFunctionRefType>(
|
|
106
|
-
functionRefOrName: FunctionRef,
|
|
107
|
-
): CustomOperation<
|
|
108
|
-
SetTypeSubArg<T, 'functionRef', FunctionRef>,
|
|
109
|
-
K | 'function',
|
|
110
|
-
B
|
|
111
|
-
>;
|
|
112
92
|
authorization<AuthRuleType extends Authorization<any, any, any>>(
|
|
113
93
|
callback: (
|
|
114
94
|
allow: AllowModifierForCustomOperation,
|
|
@@ -165,7 +145,6 @@ function _custom<
|
|
|
165
145
|
const data: CustomData = {
|
|
166
146
|
arguments: {},
|
|
167
147
|
returnType: null,
|
|
168
|
-
functionRef: null,
|
|
169
148
|
authorization: [],
|
|
170
149
|
typeName: typeName,
|
|
171
150
|
handlers: null,
|
|
@@ -184,11 +163,6 @@ function _custom<
|
|
|
184
163
|
|
|
185
164
|
return this;
|
|
186
165
|
},
|
|
187
|
-
function(functionRefOrName: CustomFunctionRefType) {
|
|
188
|
-
data.functionRef = functionRefOrName;
|
|
189
|
-
|
|
190
|
-
return this;
|
|
191
|
-
},
|
|
192
166
|
authorization<AuthRuleType extends Authorization<any, any, any>>(
|
|
193
167
|
callback: (
|
|
194
168
|
allow: AllowModifierForCustomOperation,
|
|
@@ -232,7 +206,6 @@ export function query(): CustomOperation<
|
|
|
232
206
|
{
|
|
233
207
|
arguments: null;
|
|
234
208
|
returnType: null;
|
|
235
|
-
functionRef: null;
|
|
236
209
|
authorization: [];
|
|
237
210
|
typeName: 'Query';
|
|
238
211
|
handlers: null;
|
|
@@ -253,7 +226,6 @@ export function mutation(): CustomOperation<
|
|
|
253
226
|
{
|
|
254
227
|
arguments: null;
|
|
255
228
|
returnType: null;
|
|
256
|
-
functionRef: null;
|
|
257
229
|
authorization: [];
|
|
258
230
|
typeName: 'Mutation';
|
|
259
231
|
handlers: null;
|
|
@@ -274,7 +246,6 @@ export function subscription(): CustomOperation<
|
|
|
274
246
|
{
|
|
275
247
|
arguments: null;
|
|
276
248
|
returnType: null;
|
|
277
|
-
functionRef: null;
|
|
278
249
|
authorization: [];
|
|
279
250
|
typeName: 'Subscription';
|
|
280
251
|
handlers: null;
|
|
@@ -59,15 +59,6 @@ type ModelRelationalFieldFunctions<
|
|
|
59
59
|
SetTypeSubArg<T, 'arrayRequired', true>,
|
|
60
60
|
K | 'required'
|
|
61
61
|
>;
|
|
62
|
-
/**
|
|
63
|
-
* When set, it requires the relationship to always return an array value
|
|
64
|
-
* @deprecated this modifier should not be used and will be removed
|
|
65
|
-
* in the next minor version of this package.
|
|
66
|
-
*/
|
|
67
|
-
arrayRequired(): ModelRelationalField<
|
|
68
|
-
SetTypeSubArg<T, 'arrayRequired', true>,
|
|
69
|
-
K | 'arrayRequired'
|
|
70
|
-
>;
|
|
71
62
|
/**
|
|
72
63
|
* Configures field-level authorization rules. Pass in an array of authorizations `(a.allow.____)` to mix and match
|
|
73
64
|
* multiple authorization rules for this field.
|
|
@@ -102,7 +93,6 @@ export type InternalRelationalField = ModelRelationalField<
|
|
|
102
93
|
|
|
103
94
|
const relationalModifiers = [
|
|
104
95
|
'required',
|
|
105
|
-
'arrayRequired',
|
|
106
96
|
'valueRequired',
|
|
107
97
|
'authorization',
|
|
108
98
|
] as const;
|
|
@@ -112,18 +102,18 @@ const relationModifierMap: Record<
|
|
|
112
102
|
(typeof relationalModifiers)[number][]
|
|
113
103
|
> = {
|
|
114
104
|
belongsTo: ['authorization'],
|
|
115
|
-
hasMany: ['
|
|
105
|
+
hasMany: ['valueRequired', 'authorization'],
|
|
116
106
|
hasOne: ['required', 'authorization'],
|
|
117
107
|
};
|
|
118
108
|
|
|
119
109
|
export type RelationTypeFunctionOmitMapping<
|
|
120
110
|
Type extends ModelRelationshipTypes,
|
|
121
111
|
> = Type extends ModelRelationshipTypes.belongsTo
|
|
122
|
-
? 'required' | '
|
|
112
|
+
? 'required' | 'valueRequired'
|
|
123
113
|
: Type extends ModelRelationshipTypes.hasMany
|
|
124
114
|
? 'required'
|
|
125
115
|
: Type extends ModelRelationshipTypes.hasOne
|
|
126
|
-
? '
|
|
116
|
+
? 'valueRequired'
|
|
127
117
|
: never;
|
|
128
118
|
|
|
129
119
|
function _modelRelationalField<
|
|
@@ -149,11 +139,6 @@ function _modelRelationalField<
|
|
|
149
139
|
|
|
150
140
|
return this;
|
|
151
141
|
},
|
|
152
|
-
arrayRequired() {
|
|
153
|
-
data.arrayRequired = true;
|
|
154
|
-
|
|
155
|
-
return this;
|
|
156
|
-
},
|
|
157
142
|
valueRequired() {
|
|
158
143
|
data.valueRequired = true;
|
|
159
144
|
|
package/src/ModelSchema.ts
CHANGED
|
@@ -46,6 +46,8 @@ type SchemaContent =
|
|
|
46
46
|
| EnumType<EnumTypeParamShape>
|
|
47
47
|
| CustomOperation<CustomOperationParamShape, any>;
|
|
48
48
|
|
|
49
|
+
type NonEmpty<T> = keyof T extends never ? never : T;
|
|
50
|
+
|
|
49
51
|
type ModelSchemaContents = Record<string, SchemaContent>;
|
|
50
52
|
type InternalSchemaModels = Record<
|
|
51
53
|
string,
|
|
@@ -383,7 +385,7 @@ type SchemaReturnType<
|
|
|
383
385
|
function bindConfigToSchema<DE extends DatasourceEngine>(
|
|
384
386
|
config: SchemaConfiguration<DE, DataSourceConfiguration<DE>>,
|
|
385
387
|
): <Types extends ModelSchemaContents>(
|
|
386
|
-
types: Types
|
|
388
|
+
types: NonEmpty<Types>,
|
|
387
389
|
) => SchemaReturnType<DE, Types> {
|
|
388
390
|
return (types) => {
|
|
389
391
|
return (
|
|
@@ -414,7 +416,7 @@ export function configure<DE extends DatasourceEngine>(
|
|
|
414
416
|
config: SchemaConfiguration<DE, DataSourceConfiguration<DE>>,
|
|
415
417
|
): {
|
|
416
418
|
schema: <Types extends ModelSchemaContents>(
|
|
417
|
-
types: Types
|
|
419
|
+
types: NonEmpty<Types>,
|
|
418
420
|
) => SchemaReturnType<DE, Types>;
|
|
419
421
|
} {
|
|
420
422
|
return {
|
package/src/SchemaProcessor.ts
CHANGED
|
@@ -304,7 +304,6 @@ function customOperationToGql(
|
|
|
304
304
|
arguments: fieldArgs,
|
|
305
305
|
typeName: opType,
|
|
306
306
|
returnType,
|
|
307
|
-
functionRef,
|
|
308
307
|
handlers,
|
|
309
308
|
subscriptionSource,
|
|
310
309
|
} = typeDef.data;
|
|
@@ -390,8 +389,6 @@ function customOperationToGql(
|
|
|
390
389
|
handlers,
|
|
391
390
|
typeName,
|
|
392
391
|
));
|
|
393
|
-
} else if (functionRef) {
|
|
394
|
-
gqlHandlerContent = `@function(name: "${functionRef}") `;
|
|
395
392
|
} else if (databaseType === 'sql' && handler && brand === 'inlineSql') {
|
|
396
393
|
gqlHandlerContent = `@sql(statement: ${escapeGraphQlString(
|
|
397
394
|
String(getHandlerData(handler)),
|
|
@@ -555,6 +552,32 @@ function validateImpliedFields(
|
|
|
555
552
|
}
|
|
556
553
|
}
|
|
557
554
|
|
|
555
|
+
function validateRefUseCases(
|
|
556
|
+
referrerName: string,
|
|
557
|
+
referrerType: 'customType' | 'model',
|
|
558
|
+
fields: Record<string, any>,
|
|
559
|
+
getRefType: ReturnType<typeof getRefTypeForSchema>,
|
|
560
|
+
) {
|
|
561
|
+
const check = (fieldName: string, refLink: string, targetType: string) => {
|
|
562
|
+
const { def } = getRefType(refLink, referrerName);
|
|
563
|
+
if (isInternalModel(def)) {
|
|
564
|
+
throw new Error(
|
|
565
|
+
`Cannot use \`.ref()\` to refer a model from a \`${targetType}\`. Field \`${fieldName}\` of \`${referrerName}\` refers to model \`${refLink}\``,
|
|
566
|
+
);
|
|
567
|
+
}
|
|
568
|
+
};
|
|
569
|
+
|
|
570
|
+
for (const [fieldName, field] of Object.entries(fields)) {
|
|
571
|
+
if (isRefField(field)) {
|
|
572
|
+
check(
|
|
573
|
+
fieldName,
|
|
574
|
+
field.data.link,
|
|
575
|
+
referrerType === 'customType' ? 'custom type' : 'model',
|
|
576
|
+
);
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
|
|
558
581
|
/**
|
|
559
582
|
* Given a list of authorization rules, produces a set of the implied owner and/or
|
|
560
583
|
* group fields, along with the associated graphql `@auth` string directive.
|
|
@@ -1075,6 +1098,9 @@ const schemaPreprocessor = (
|
|
|
1075
1098
|
gqlModels.push(enumType);
|
|
1076
1099
|
} else if (isCustomType(typeDef)) {
|
|
1077
1100
|
const fields = typeDef.data.fields;
|
|
1101
|
+
|
|
1102
|
+
validateRefUseCases(typeName, 'customType', fields, getRefType);
|
|
1103
|
+
|
|
1078
1104
|
const fieldAuthApplicableFields = Object.fromEntries(
|
|
1079
1105
|
Object.entries(fields).filter(
|
|
1080
1106
|
(
|
|
@@ -1159,6 +1185,9 @@ const schemaPreprocessor = (
|
|
|
1159
1185
|
string,
|
|
1160
1186
|
ModelField<any, any>
|
|
1161
1187
|
>;
|
|
1188
|
+
|
|
1189
|
+
validateRefUseCases(typeName, 'model', fields, getRefType);
|
|
1190
|
+
|
|
1162
1191
|
const identifier = typeDef.data.identifier;
|
|
1163
1192
|
const [partitionKey] = identifier;
|
|
1164
1193
|
|
|
@@ -1200,6 +1229,9 @@ const schemaPreprocessor = (
|
|
|
1200
1229
|
string,
|
|
1201
1230
|
ModelField<any, any>
|
|
1202
1231
|
>;
|
|
1232
|
+
|
|
1233
|
+
validateRefUseCases(typeName, 'model', fields, getRefType);
|
|
1234
|
+
|
|
1203
1235
|
const identifier = typeDef.data.identifier;
|
|
1204
1236
|
const [partitionKey] = identifier;
|
|
1205
1237
|
|
|
@@ -1266,15 +1298,9 @@ function validateCustomOperations(
|
|
|
1266
1298
|
authRules: Authorization<any, any, any>[],
|
|
1267
1299
|
getRefType: ReturnType<typeof getRefTypeForSchema>,
|
|
1268
1300
|
) {
|
|
1269
|
-
const {
|
|
1270
|
-
functionRef,
|
|
1271
|
-
handlers,
|
|
1272
|
-
typeName: opType,
|
|
1273
|
-
subscriptionSource,
|
|
1274
|
-
} = typeDef.data;
|
|
1301
|
+
const { handlers, typeName: opType, subscriptionSource } = typeDef.data;
|
|
1275
1302
|
|
|
1276
|
-
|
|
1277
|
-
const handlerConfigured = functionRef !== null || handlers?.length;
|
|
1303
|
+
const handlerConfigured = handlers?.length;
|
|
1278
1304
|
const authConfigured = authRules.length > 0;
|
|
1279
1305
|
|
|
1280
1306
|
if (
|