@aws-amplify/data-schema 0.13.9 → 0.13.11
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/lib-esm/src/Authorization.d.ts +24 -1
- package/lib-esm/src/Authorization.js +26 -1
- package/lib-esm/src/MappedTypes/CustomOperations.d.ts +8 -9
- package/lib-esm/src/MappedTypes/ResolveFieldProperties.d.ts +9 -4
- package/lib-esm/src/MappedTypes/ResolveSchema.d.ts +8 -4
- package/lib-esm/src/ModelSchema.d.ts +4 -4
- package/lib-esm/src/RefType.d.ts +14 -4
- package/lib-esm/src/RefType.js +13 -2
- package/lib-esm/src/SchemaProcessor.js +61 -13
- package/lib-esm/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { UnionToIntersection } from '@aws-amplify/data-schema-types';
|
|
1
|
+
import type { UnionToIntersection, FunctionSchemaAccess } from '@aws-amplify/data-schema-types';
|
|
2
2
|
declare const __data: unique symbol;
|
|
3
3
|
/**
|
|
4
4
|
* All possible providers.
|
|
@@ -41,6 +41,24 @@ export type Strategy = (typeof Strategies)[number];
|
|
|
41
41
|
*/
|
|
42
42
|
export declare const Operations: readonly ["create", "update", "delete", "read", "get", "list", "sync", "listen", "search"];
|
|
43
43
|
export type Operation = (typeof Operations)[number];
|
|
44
|
+
/**
|
|
45
|
+
* The operations that can be performed against an API by a Lambda function.
|
|
46
|
+
*/
|
|
47
|
+
export declare const ResourceOperations: readonly ["query", "mutate", "listen"];
|
|
48
|
+
export type ResourceOperation = (typeof ResourceOperations)[number];
|
|
49
|
+
/**
|
|
50
|
+
* Super-set of regular auth type; includes schema-level resource access configuration
|
|
51
|
+
*/
|
|
52
|
+
export type SchemaAuthorization<AuthStrategy extends Strategy, AuthField extends string | undefined, AuthFieldPlurality extends boolean> = Authorization<AuthStrategy, AuthField, AuthFieldPlurality> | ResourceAuthorization;
|
|
53
|
+
export type ResourceAuthorization = {
|
|
54
|
+
[__data]: ResourceAuthorizationData;
|
|
55
|
+
};
|
|
56
|
+
type DefineFunction = FunctionSchemaAccess['resourceProvider'];
|
|
57
|
+
export type ResourceAuthorizationData = {
|
|
58
|
+
strategy: 'resource';
|
|
59
|
+
resource: DefineFunction;
|
|
60
|
+
operations?: ResourceOperation[];
|
|
61
|
+
};
|
|
44
62
|
export type Authorization<AuthStrategy extends Strategy, AuthField extends string | undefined, AuthFieldPlurality extends boolean> = {
|
|
45
63
|
[__data]: {
|
|
46
64
|
strategy?: AuthStrategy;
|
|
@@ -200,7 +218,11 @@ export declare const allow: {
|
|
|
200
218
|
readonly custom: (provider?: CustomProvider) => Authorization<"custom", undefined, false> & {
|
|
201
219
|
to: typeof to;
|
|
202
220
|
};
|
|
221
|
+
readonly resource: (fn: DefineFunction) => ResourceAuthorization & {
|
|
222
|
+
to: typeof resourceTo;
|
|
223
|
+
};
|
|
203
224
|
};
|
|
225
|
+
declare function resourceTo<SELF extends ResourceAuthorization>(this: SELF, operations: ResourceOperation[]): Omit<SELF, "to">;
|
|
204
226
|
/**
|
|
205
227
|
* Turns the type from a list of `Authorization` rules like this:
|
|
206
228
|
*
|
|
@@ -261,4 +283,5 @@ export declare const accessData: <T extends Authorization<any, any, any>>(author
|
|
|
261
283
|
identityClaim?: string | undefined;
|
|
262
284
|
groupClaim?: string | undefined;
|
|
263
285
|
};
|
|
286
|
+
export declare const accessSchemaData: <T extends SchemaAuthorization<any, any, any>>(authorization: T) => T[typeof __data];
|
|
264
287
|
export {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.accessData = exports.allow = exports.Operations = exports.Strategies = exports.CustomProviders = exports.GroupProviders = exports.OwnerProviders = exports.PrivateProviders = exports.PublicProviders = exports.Providers = void 0;
|
|
3
|
+
exports.accessSchemaData = exports.accessData = exports.allow = exports.ResourceOperations = exports.Operations = exports.Strategies = exports.CustomProviders = exports.GroupProviders = exports.OwnerProviders = exports.PrivateProviders = exports.PublicProviders = exports.Providers = void 0;
|
|
4
4
|
const __data = Symbol('data');
|
|
5
5
|
/**
|
|
6
6
|
* All possible providers.
|
|
@@ -57,6 +57,10 @@ exports.Operations = [
|
|
|
57
57
|
'listen',
|
|
58
58
|
'search',
|
|
59
59
|
];
|
|
60
|
+
/**
|
|
61
|
+
* The operations that can be performed against an API by a Lambda function.
|
|
62
|
+
*/
|
|
63
|
+
exports.ResourceOperations = ['query', 'mutate', 'listen'];
|
|
60
64
|
/**
|
|
61
65
|
* Creates a shallow copy of an object with an individual field pruned away.
|
|
62
66
|
*
|
|
@@ -302,6 +306,27 @@ exports.allow = {
|
|
|
302
306
|
to,
|
|
303
307
|
});
|
|
304
308
|
},
|
|
309
|
+
resource(fn) {
|
|
310
|
+
return resourceAuthData(fn, {
|
|
311
|
+
to: resourceTo,
|
|
312
|
+
});
|
|
313
|
+
},
|
|
305
314
|
};
|
|
315
|
+
function resourceTo(operations) {
|
|
316
|
+
this[__data].operations = operations;
|
|
317
|
+
return omit(this, 'to');
|
|
318
|
+
}
|
|
319
|
+
function resourceAuthData(resource, builderMethods) {
|
|
320
|
+
return {
|
|
321
|
+
[__data]: {
|
|
322
|
+
strategy: 'resource',
|
|
323
|
+
resource,
|
|
324
|
+
},
|
|
325
|
+
...builderMethods,
|
|
326
|
+
};
|
|
327
|
+
}
|
|
306
328
|
const accessData = (authorization) => authorization[__data];
|
|
307
329
|
exports.accessData = accessData;
|
|
330
|
+
// TODO: delete when we make resource auth available at each level in the schema (model, field)
|
|
331
|
+
const accessSchemaData = (authorization) => authorization[__data];
|
|
332
|
+
exports.accessSchemaData = accessSchemaData;
|
|
@@ -2,7 +2,8 @@ import type { ModelSchema } from '../ModelSchema';
|
|
|
2
2
|
import type { NonModelTypesShape } from './ExtractNonModelTypes';
|
|
3
3
|
import type { CustomOperation, CustomOperationParamShape } from '../CustomOperation';
|
|
4
4
|
import type { ModelField } from '../ModelField';
|
|
5
|
-
import { RefType, RefTypeParamShape } from '../RefType';
|
|
5
|
+
import type { RefType, RefTypeParamShape } from '../RefType';
|
|
6
|
+
import type { ResolveRefsOfCustomType, ResolveRefValueArrayTraits } from './ResolveFieldProperties';
|
|
6
7
|
/**
|
|
7
8
|
* Creates meta types for custom operations from a schema.
|
|
8
9
|
*/
|
|
@@ -10,7 +11,7 @@ export type ResolveCustomOperations<Schema extends ModelSchema<any, any>, FullyR
|
|
|
10
11
|
customOperations: {
|
|
11
12
|
[OpName in keyof CustomOpShapes<Schema>]: {
|
|
12
13
|
arguments: CustomOpArguments<CustomOpShapes<Schema>[OpName]>;
|
|
13
|
-
returnType: CustomOpReturnType<CustomOpShapes<Schema>[OpName],
|
|
14
|
+
returnType: CustomOpReturnType<CustomOpShapes<Schema>[OpName], FullyResolvedSchema, NonModelTypes>;
|
|
14
15
|
functionRef: CustomOpShapes<Schema>[OpName]['functionRef'];
|
|
15
16
|
typeName: CustomOpShapes<Schema>[OpName]['typeName'];
|
|
16
17
|
authorization: CustomOpShapes<Schema>[OpName]['authorization'];
|
|
@@ -34,14 +35,12 @@ export type CustomOpArguments<Shape extends CustomOperationParamShape> = {
|
|
|
34
35
|
*
|
|
35
36
|
* This entails dereferencing refs and inferring graphql types from field-type defs.
|
|
36
37
|
*/
|
|
37
|
-
export type CustomOpReturnType<Shape extends CustomOperationParamShape,
|
|
38
|
+
export type CustomOpReturnType<Shape extends CustomOperationParamShape, FullyResolvedSchema extends Record<string, unknown>, NonModelTypes extends NonModelTypesShape> = Shape['returnType'] extends RefType<infer RefShape, any, any> ? ResolveRef<RefShape, FullyResolvedSchema, NonModelTypes> : Shape['returnType'] extends ModelField<infer R, any, any> ? R : never;
|
|
38
39
|
/**
|
|
39
40
|
* `a.ref()` resolution specific to custom operations, for which `a.ref()`
|
|
40
41
|
* can refer to a model, custom type, or enum.
|
|
42
|
+
*
|
|
43
|
+
* This utility is a duplication of ResolveRef (src/MappedTypes/ResolveFieldProperties.ts)
|
|
44
|
+
* with the addition that allows .ref() a model with custom operations.
|
|
41
45
|
*/
|
|
42
|
-
export type ResolveRef<Shape extends RefTypeParamShape,
|
|
43
|
-
/**
|
|
44
|
-
* Small utility to provided a merged view of models, custom types, and enums
|
|
45
|
-
* from a schema and nonModel-types.
|
|
46
|
-
*/
|
|
47
|
-
export type Bag<FullyResolvedSchema extends Record<string, unknown>, NonModelTypes extends NonModelTypesShape> = FullyResolvedSchema & NonModelTypes['customTypes'] & NonModelTypes['enums'];
|
|
46
|
+
export type ResolveRef<Shape extends RefTypeParamShape, FullyResolvedSchema extends Record<string, unknown>, NonModelTypes extends NonModelTypesShape, Link = Shape['link'], RefValue = Link extends keyof FullyResolvedSchema ? FullyResolvedSchema[Link] : Link extends keyof NonModelTypes['enums'] ? NonModelTypes['enums'][Link] : Link extends keyof NonModelTypes['customTypes'] ? ResolveRefsOfCustomType<NonModelTypes, NonModelTypes['customTypes'][Link]> : never, Value = Shape['valueRequired'] extends true ? RefValue : RefValue | null> = ResolveRefValueArrayTraits<Shape, Value>;
|
|
@@ -28,10 +28,15 @@ type GetRelationshipRef<T, RM extends keyof T, TypeArg extends ModelRelationalFi
|
|
|
28
28
|
type ResolveRelationalFieldsForModel<Schema, ModelName extends keyof Schema, Flat extends boolean> = {
|
|
29
29
|
[FieldName in keyof Schema[ModelName]]: Schema[ModelName][FieldName] extends ModelRelationalFieldParamShape ? Schema[ModelName][FieldName]['relatedModel'] extends keyof Schema ? GetRelationshipRef<Schema, Schema[ModelName][FieldName]['relatedModel'], Schema[ModelName][FieldName], Flat> : never : Schema[ModelName][FieldName];
|
|
30
30
|
};
|
|
31
|
-
export type ResolveRef<NonModelTypes extends NonModelTypesShape, Ref extends RefTypeParamShape, Link extends string = Ref['link'],
|
|
32
|
-
|
|
31
|
+
export type ResolveRef<NonModelTypes extends NonModelTypesShape, Ref extends RefTypeParamShape, Link extends string = Ref['link'], RefValue = Link extends keyof NonModelTypes['enums'] ? NonModelTypes['enums'][Link] : Link extends keyof NonModelTypes['customTypes'] ? ResolveRefsOfCustomType<NonModelTypes, NonModelTypes['customTypes'][Link]> : never, Value = Ref['valueRequired'] extends true ? RefValue : RefValue | null> = ResolveRefValueArrayTraits<Ref, Value>;
|
|
32
|
+
/**
|
|
33
|
+
* Converts the resolved RefType Value type into Array<> according to the
|
|
34
|
+
* `array` and `arrayRequired` properties of the RefType
|
|
35
|
+
*/
|
|
36
|
+
export type ResolveRefValueArrayTraits<Ref extends RefTypeParamShape, Value> = Ref['array'] extends false ? Value : Ref['arrayRequired'] extends true ? Array<Value> : Array<Value> | null;
|
|
37
|
+
export type ResolveRefsOfCustomType<NonModelTypes extends NonModelTypesShape, T> = {
|
|
33
38
|
[Prop in keyof T]: T[Prop] extends RefType<infer R extends RefTypeParamShape, any, any> | null ? ResolveRef<NonModelTypes, R> : T[Prop];
|
|
34
|
-
} extends infer Resolved ? ExtractNullableFieldsToOptionalFields<Resolved
|
|
39
|
+
} extends infer Resolved ? Intersection<ExtractNullableFieldsToOptionalFields<Resolved>, ExtractNonNullableFieldsToRequiredFields<Resolved>> : never;
|
|
35
40
|
type ResolveRelationships<Schema, NonModelTypes extends NonModelTypesShape, Flat extends boolean = false> = {
|
|
36
41
|
[ModelProp in keyof Schema]: {
|
|
37
42
|
[FieldProp in keyof Schema[ModelProp]]: Schema[ModelProp][FieldProp] extends RefType<infer R extends RefTypeParamShape, any, any> | null ? ResolveRef<NonModelTypes, R> : Schema[ModelProp][FieldProp] extends ModelRelationalFieldParamShape ? Schema[ModelProp][FieldProp]['relatedModel'] extends keyof Schema ? Schema[ModelProp][FieldProp]['relationshipType'] extends 'manyToMany' ? Schema[ModelProp][FieldProp]['relationName'] extends keyof Schema ? GetRelationshipRef<Schema, Schema[ModelProp][FieldProp]['relationName'], Schema[ModelProp][FieldProp], Flat> : never : GetRelationshipRef<Schema, Schema[ModelProp][FieldProp]['relatedModel'], Schema[ModelProp][FieldProp], Flat> : never : Schema[ModelProp][FieldProp];
|
|
@@ -54,7 +59,7 @@ type ExtractNonNullableFieldsToRequiredFields<Fields> = {
|
|
|
54
59
|
type MarkModelsNonNullableFieldsRequired<Schema> = {
|
|
55
60
|
[ModelProp in keyof Schema]: ExtractNonNullableFieldsToRequiredFields<Schema[ModelProp]>;
|
|
56
61
|
};
|
|
57
|
-
type Intersection<A, B, C, D
|
|
62
|
+
type Intersection<A = Record<never, never>, B = Record<never, never>, C = Record<never, never>, D = Record<never, never>> = A & B & C & D extends infer U ? {
|
|
58
63
|
[P in keyof U]: U[P];
|
|
59
64
|
} : never;
|
|
60
65
|
export type ModelImpliedAuthFields<Schema extends ModelSchema<any, any>> = {
|
|
@@ -32,10 +32,12 @@ export type ModelAndCustomTypes<Schema> = {
|
|
|
32
32
|
*/
|
|
33
33
|
export type FieldTypes<T> = {
|
|
34
34
|
[ModelProp in keyof T]: {
|
|
35
|
-
[FieldProp in keyof T[ModelProp]]: T[ModelProp][FieldProp] extends ModelField<infer R, any, any> ? R : T[ModelProp][FieldProp] extends RefType<infer R extends RefTypeParamShape, any, any> ? R['
|
|
35
|
+
[FieldProp in keyof T[ModelProp]]: T[ModelProp][FieldProp] extends ModelField<infer R, any, any> ? R : T[ModelProp][FieldProp] extends RefType<infer R extends RefTypeParamShape, any, any> ? R['valueRequired'] extends true ? T[ModelProp][FieldProp] : T[ModelProp][FieldProp] | null : T[ModelProp][FieldProp] extends EnumType<EnumTypeParamShape> | CustomType<CustomTypeParamShape> ? RefType<{
|
|
36
36
|
link: `${Capitalize<ModelProp & string>}${Capitalize<FieldProp & string>}`;
|
|
37
37
|
type: 'ref';
|
|
38
|
-
|
|
38
|
+
valueRequired: false;
|
|
39
|
+
array: false;
|
|
40
|
+
arrayRequired: false;
|
|
39
41
|
authorization: [];
|
|
40
42
|
}> | null : T[ModelProp][FieldProp] extends ModelRelationalField<infer R, string, RelationTypeFunctionOmitMapping<ModelRelationshipTypes>, any> ? R : never;
|
|
41
43
|
};
|
|
@@ -50,10 +52,12 @@ export type FieldTypes<T> = {
|
|
|
50
52
|
*/
|
|
51
53
|
export type FieldTypesOfCustomType<T> = {
|
|
52
54
|
[CustomTypeName in keyof T]: {
|
|
53
|
-
[FieldProp in keyof T[CustomTypeName]]: T[CustomTypeName][FieldProp] extends ModelField<infer R, any, any> ? R : T[CustomTypeName][FieldProp] extends RefType<infer R extends RefTypeParamShape, any, any> ? R['
|
|
55
|
+
[FieldProp in keyof T[CustomTypeName]]: T[CustomTypeName][FieldProp] extends ModelField<infer R, any, any> ? R : T[CustomTypeName][FieldProp] extends RefType<infer R extends RefTypeParamShape, any, any> ? R['valueRequired'] extends true ? T[CustomTypeName][FieldProp] : T[CustomTypeName][FieldProp] | null : T[CustomTypeName][FieldProp] extends EnumType<EnumTypeParamShape> | CustomType<CustomTypeParamShape> ? RefType<{
|
|
54
56
|
link: `${Capitalize<CustomTypeName & string>}${Capitalize<FieldProp & string>}`;
|
|
55
57
|
type: 'ref';
|
|
56
|
-
|
|
58
|
+
valueRequired: false;
|
|
59
|
+
array: false;
|
|
60
|
+
arrayRequired: false;
|
|
57
61
|
authorization: [];
|
|
58
62
|
}> | null : never;
|
|
59
63
|
};
|
|
@@ -3,7 +3,7 @@ import { type ModelType, type ModelTypeParamShape, type InternalModel, SchemaMod
|
|
|
3
3
|
import type { EnumType, EnumTypeParamShape } from './EnumType';
|
|
4
4
|
import type { CustomType, CustomTypeParamShape } from './CustomType';
|
|
5
5
|
import type { CustomOperation, CustomOperationParamShape, InternalCustom, MutationCustomOperation, QueryCustomOperation, SubscriptionCustomOperation } from './CustomOperation';
|
|
6
|
-
import {
|
|
6
|
+
import { SchemaAuthorization } from './Authorization';
|
|
7
7
|
type SchemaContent = ModelType<ModelTypeParamShape, any> | CustomType<CustomTypeParamShape> | EnumType<EnumTypeParamShape> | CustomOperation<CustomOperationParamShape, any>;
|
|
8
8
|
type ModelSchemaContents = Record<string, SchemaContent>;
|
|
9
9
|
type InternalSchemaModels = Record<string, InternalModel | EnumType<any> | CustomType<any> | InternalCustom>;
|
|
@@ -35,7 +35,7 @@ export type SchemaConfig<DE extends DatasourceEngine, DC extends DatasourceConfi
|
|
|
35
35
|
};
|
|
36
36
|
export type ModelSchemaParamShape = {
|
|
37
37
|
types: ModelSchemaContents;
|
|
38
|
-
authorization:
|
|
38
|
+
authorization: SchemaAuthorization<any, any, any>[];
|
|
39
39
|
configuration: SchemaConfig<any, any>;
|
|
40
40
|
};
|
|
41
41
|
export type SQLModelSchemaParamShape = ModelSchemaParamShape & {
|
|
@@ -44,11 +44,11 @@ export type SQLModelSchemaParamShape = ModelSchemaParamShape & {
|
|
|
44
44
|
export type InternalSchema = {
|
|
45
45
|
data: {
|
|
46
46
|
types: InternalSchemaModels;
|
|
47
|
-
authorization:
|
|
47
|
+
authorization: SchemaAuthorization<any, any, any>[];
|
|
48
48
|
};
|
|
49
49
|
};
|
|
50
50
|
export type ModelSchema<T extends ModelSchemaParamShape, UsedMethods extends 'authorization' = never> = Omit<{
|
|
51
|
-
authorization: <AuthRules extends
|
|
51
|
+
authorization: <AuthRules extends SchemaAuthorization<any, any, any>>(auth: AuthRules[]) => ModelSchema<SetTypeSubArg<T, 'authorization', AuthRules[]>, UsedMethods | 'authorization'>;
|
|
52
52
|
}, UsedMethods> & {
|
|
53
53
|
data: T;
|
|
54
54
|
models: {
|
package/lib-esm/src/RefType.d.ts
CHANGED
|
@@ -6,20 +6,28 @@ declare const brandName = "ref";
|
|
|
6
6
|
type RefTypeData = {
|
|
7
7
|
type: 'ref';
|
|
8
8
|
link: string;
|
|
9
|
-
|
|
9
|
+
valueRequired: boolean;
|
|
10
|
+
array: boolean;
|
|
11
|
+
arrayRequired: boolean;
|
|
10
12
|
authorization: Authorization<any, any, any>[];
|
|
11
13
|
};
|
|
12
14
|
export type RefTypeParamShape = {
|
|
13
15
|
type: 'ref';
|
|
14
16
|
link: string;
|
|
15
|
-
|
|
17
|
+
valueRequired: boolean;
|
|
18
|
+
array: boolean;
|
|
19
|
+
arrayRequired: boolean;
|
|
16
20
|
authorization: Authorization<any, any, any>[];
|
|
17
21
|
};
|
|
18
22
|
export type RefType<T extends RefTypeParamShape, K extends keyof RefType<T> = never, Auth = undefined> = Omit<{
|
|
19
23
|
/**
|
|
20
24
|
* Marks a field as required.
|
|
21
25
|
*/
|
|
22
|
-
required(): RefType<SetTypeSubArg<T, '
|
|
26
|
+
required(): RefType<SetTypeSubArg<T, T['array'] extends true ? 'arrayRequired' : 'valueRequired', true>, K | 'required'>;
|
|
27
|
+
/**
|
|
28
|
+
* Marks a field as an array of the specified ref type.
|
|
29
|
+
*/
|
|
30
|
+
array(): RefType<SetTypeSubArg<T, 'array', true>, Exclude<K, 'required'> | 'array'>;
|
|
23
31
|
/**
|
|
24
32
|
* Configures field-level authorization rules. Pass in an array of authorizations `(a.allow.____)` to mix and match
|
|
25
33
|
* multiple authorization rules for this field.
|
|
@@ -38,7 +46,9 @@ export type InternalRef = RefType<RefTypeParamShape> & {
|
|
|
38
46
|
type RefTypeArgFactory<Link extends string> = {
|
|
39
47
|
type: 'ref';
|
|
40
48
|
link: Link;
|
|
41
|
-
|
|
49
|
+
valueRequired: false;
|
|
50
|
+
array: false;
|
|
51
|
+
arrayRequired: false;
|
|
42
52
|
authorization: [];
|
|
43
53
|
};
|
|
44
54
|
export declare function ref<Value extends string, T extends Value>(link: T): RefType<RefTypeArgFactory<T>, never, undefined>;
|
package/lib-esm/src/RefType.js
CHANGED
|
@@ -10,12 +10,23 @@ function _ref(link) {
|
|
|
10
10
|
const data = {
|
|
11
11
|
type: 'ref',
|
|
12
12
|
link,
|
|
13
|
-
|
|
13
|
+
valueRequired: false,
|
|
14
|
+
array: false,
|
|
15
|
+
arrayRequired: false,
|
|
14
16
|
authorization: [],
|
|
15
17
|
};
|
|
16
18
|
const builder = brandedBuilder({
|
|
17
19
|
required() {
|
|
18
|
-
data.
|
|
20
|
+
if (data.array) {
|
|
21
|
+
data.arrayRequired = true;
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
data.valueRequired = true;
|
|
25
|
+
}
|
|
26
|
+
return this;
|
|
27
|
+
},
|
|
28
|
+
array() {
|
|
29
|
+
data.array = true;
|
|
19
30
|
return this;
|
|
20
31
|
},
|
|
21
32
|
authorization(rules) {
|
|
@@ -142,17 +142,17 @@ function modelFieldToGql(fieldDef) {
|
|
|
142
142
|
return field;
|
|
143
143
|
}
|
|
144
144
|
function refFieldToGql(fieldDef) {
|
|
145
|
-
const { link,
|
|
145
|
+
const { link, valueRequired, array, arrayRequired } = fieldDef;
|
|
146
146
|
let field = link;
|
|
147
|
-
if (
|
|
147
|
+
if (valueRequired === true) {
|
|
148
|
+
field += '!';
|
|
149
|
+
}
|
|
150
|
+
if (array === true) {
|
|
151
|
+
field = `[${field}]`;
|
|
152
|
+
}
|
|
153
|
+
if (arrayRequired === true) {
|
|
148
154
|
field += '!';
|
|
149
155
|
}
|
|
150
|
-
// if (array) {
|
|
151
|
-
// field = `[${field}]`;
|
|
152
|
-
// }
|
|
153
|
-
// if (arrayRequired === true) {
|
|
154
|
-
// field += '!';
|
|
155
|
-
// }
|
|
156
156
|
return field;
|
|
157
157
|
}
|
|
158
158
|
function customOperationToGql(typeName, typeDef, authorization, isCustom = false) {
|
|
@@ -243,6 +243,18 @@ function mergeFieldObjects(...fieldsObjects) {
|
|
|
243
243
|
}
|
|
244
244
|
return result;
|
|
245
245
|
}
|
|
246
|
+
/**
|
|
247
|
+
* Throws if resource/lambda auth is configured at the model or field level
|
|
248
|
+
*
|
|
249
|
+
* @param authorization A list of authorization rules.
|
|
250
|
+
*/
|
|
251
|
+
function validateAuth(authorization = []) {
|
|
252
|
+
for (const entry of authorization) {
|
|
253
|
+
if (ruleIsResourceAuth(entry)) {
|
|
254
|
+
throw new Error('Lambda resource authorization is only confiugrable at the schema level');
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
246
258
|
/**
|
|
247
259
|
* Given a list of authorization rules, produces a set of the implied owner and/or
|
|
248
260
|
* group fields, along with the associated graphql `@auth` string directive.
|
|
@@ -530,7 +542,9 @@ const idFields = (model) => {
|
|
|
530
542
|
function processFieldLevelAuthRules(fields, authFields) {
|
|
531
543
|
const fieldLevelAuthRules = {};
|
|
532
544
|
for (const [fieldName, fieldDef] of Object.entries(fields)) {
|
|
533
|
-
const
|
|
545
|
+
const fieldAuth = fieldDef?.data?.authorization || [];
|
|
546
|
+
validateAuth(fieldAuth);
|
|
547
|
+
const { authString, authFields: fieldAuthField } = calculateAuth(fieldAuth);
|
|
534
548
|
if (authString)
|
|
535
549
|
fieldLevelAuthRules[fieldName] = authString;
|
|
536
550
|
if (fieldAuthField) {
|
|
@@ -621,6 +635,38 @@ const transformedSecondaryIndexesForModel = (secondaryIndexes) => {
|
|
|
621
635
|
return acc;
|
|
622
636
|
}, {});
|
|
623
637
|
};
|
|
638
|
+
const ruleIsResourceAuth = (authRule) => {
|
|
639
|
+
const data = (0, Authorization_1.accessSchemaData)(authRule);
|
|
640
|
+
return data.strategy === 'resource';
|
|
641
|
+
};
|
|
642
|
+
/**
|
|
643
|
+
* Separates out lambda resource auth rules from remaining schema rules.
|
|
644
|
+
*
|
|
645
|
+
* @param authRules schema auth rules
|
|
646
|
+
*/
|
|
647
|
+
const extractFunctionSchemaAccess = (authRules) => {
|
|
648
|
+
const schemaAuth = [];
|
|
649
|
+
const functionSchemaAccess = [];
|
|
650
|
+
const defaultActions = [
|
|
651
|
+
'query',
|
|
652
|
+
'mutate',
|
|
653
|
+
'listen',
|
|
654
|
+
];
|
|
655
|
+
for (const rule of authRules) {
|
|
656
|
+
if (ruleIsResourceAuth(rule)) {
|
|
657
|
+
const ruleData = (0, Authorization_1.accessSchemaData)(rule);
|
|
658
|
+
const fnAccess = {
|
|
659
|
+
resourceProvider: ruleData.resource,
|
|
660
|
+
actions: ruleData.operations || defaultActions,
|
|
661
|
+
};
|
|
662
|
+
functionSchemaAccess.push(fnAccess);
|
|
663
|
+
}
|
|
664
|
+
else {
|
|
665
|
+
schemaAuth.push(rule);
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
return { schemaAuth, functionSchemaAccess };
|
|
669
|
+
};
|
|
624
670
|
const schemaPreprocessor = (schema) => {
|
|
625
671
|
const gqlModels = [];
|
|
626
672
|
const customQueries = [];
|
|
@@ -629,10 +675,12 @@ const schemaPreprocessor = (schema) => {
|
|
|
629
675
|
const jsFunctions = [];
|
|
630
676
|
const fkFields = allImpliedFKs(schema);
|
|
631
677
|
const topLevelTypes = Object.entries(schema.data.types);
|
|
678
|
+
const { schemaAuth, functionSchemaAccess } = extractFunctionSchemaAccess(schema.data.authorization);
|
|
632
679
|
for (const [typeName, typeDef] of topLevelTypes) {
|
|
680
|
+
validateAuth(typeDef.data?.authorization);
|
|
633
681
|
const mostRelevantAuthRules = typeDef.data?.authorization?.length > 0
|
|
634
682
|
? typeDef.data.authorization
|
|
635
|
-
:
|
|
683
|
+
: schemaAuth;
|
|
636
684
|
if (!isInternalModel(typeDef)) {
|
|
637
685
|
if (isEnumType(typeDef)) {
|
|
638
686
|
if (typeDef.values.some((value) => /\s/.test(value))) {
|
|
@@ -706,7 +754,7 @@ const schemaPreprocessor = (schema) => {
|
|
|
706
754
|
};
|
|
707
755
|
gqlModels.push(...generateCustomOperationTypes(customOperations));
|
|
708
756
|
const processedSchema = gqlModels.join('\n\n');
|
|
709
|
-
return { schema: processedSchema, jsFunctions };
|
|
757
|
+
return { schema: processedSchema, jsFunctions, functionSchemaAccess };
|
|
710
758
|
};
|
|
711
759
|
function validateCustomOperations(typeDef, typeName, authRules) {
|
|
712
760
|
const { functionRef, handlers } = typeDef.data;
|
|
@@ -819,7 +867,7 @@ function generateCustomOperationTypes({ queries, mutations, subscriptions, }) {
|
|
|
819
867
|
* @returns DerivedApiDefinition that conforms to IAmplifyGraphqlDefinition
|
|
820
868
|
*/
|
|
821
869
|
function processSchema(arg) {
|
|
822
|
-
const { schema, jsFunctions } = schemaPreprocessor(arg.schema);
|
|
823
|
-
return { schema, functionSlots: [], jsFunctions };
|
|
870
|
+
const { schema, jsFunctions, functionSchemaAccess } = schemaPreprocessor(arg.schema);
|
|
871
|
+
return { schema, functionSlots: [], jsFunctions, functionSchemaAccess };
|
|
824
872
|
}
|
|
825
873
|
exports.processSchema = processSchema;
|