@aws-amplify/data-schema 0.13.13 → 0.13.14

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.
@@ -1,4 +1,4 @@
1
- import type { UnionToIntersection, FunctionSchemaAccess } from '@aws-amplify/data-schema-types';
1
+ import type { UnionToIntersection, DefineFunction } from '@aws-amplify/data-schema-types';
2
2
  declare const __data: unique symbol;
3
3
  /**
4
4
  * All possible providers.
@@ -53,7 +53,6 @@ export type SchemaAuthorization<AuthStrategy extends Strategy, AuthField extends
53
53
  export type ResourceAuthorization = {
54
54
  [__data]: ResourceAuthorizationData;
55
55
  };
56
- type DefineFunction = FunctionSchemaAccess['resourceProvider'];
57
56
  export type ResourceAuthorizationData = {
58
57
  strategy: 'resource';
59
58
  resource: DefineFunction;
@@ -43,6 +43,14 @@ export type CustomOperationParamShape = {
43
43
  export type CustomOperation<T extends CustomOperationParamShape, K extends keyof CustomOperation<T> = never, B extends CustomOperationBrand = CustomOperationBrand> = Omit<{
44
44
  arguments<Arguments extends CustomArguments>(args: Arguments): CustomOperation<SetTypeSubArg<T, 'arguments', Arguments>, K | 'arguments', B>;
45
45
  returns<ReturnType extends CustomReturnType>(returnType: ReturnType): CustomOperation<SetTypeSubArg<T, 'returnType', ReturnType>, K | 'returns', B>;
46
+ /**
47
+ *
48
+ * @deprecated
49
+ * `.function` should no longer be used and will be removed
50
+ * in the next minor version of this package.
51
+ *
52
+ * Use `.handler(a.handler.function())` instead
53
+ */
46
54
  function<FunctionRef extends CustomFunctionRefType>(functionRefOrName: FunctionRef): CustomOperation<SetTypeSubArg<T, 'functionRef', FunctionRef>, K | 'function', B>;
47
55
  authorization<AuthRuleType extends Authorization<any, any, any>>(rules: AuthRuleType[]): CustomOperation<SetTypeSubArg<T, 'authorization', AuthRuleType[]>, K | 'authorization', B>;
48
56
  handler<H extends HandlerInputType>(handlers: H): CustomOperation<T, K | 'handler', B>;
@@ -1,3 +1,4 @@
1
+ import type { DefineFunction } from '@aws-amplify/data-schema-types';
1
2
  import { Brand } from './util';
2
3
  import { RefType } from './RefType';
3
4
  export type HandlerType = InlineSqlHandler | SqlReferenceHandler | CustomHandler | FunctionHandler;
@@ -37,11 +38,12 @@ export type CustomHandler = {
37
38
  [dataSymbol]: CustomHandlerData;
38
39
  } & Brand<typeof customHandlerBrand>;
39
40
  declare function custom(customHandler: CustomHandlerInput): CustomHandler;
41
+ export type FunctionHandlerData = DefineFunction | string;
40
42
  declare const functionHandlerBrand = "functionHandler";
41
43
  export type FunctionHandler = {
42
- [dataSymbol]: (...args: any[]) => any;
44
+ [dataSymbol]: FunctionHandlerData;
43
45
  } & Brand<typeof functionHandlerBrand>;
44
- declare function fcn(fn: (...args: any[]) => any): FunctionHandler;
46
+ declare function fcn(fn: FunctionHandlerData): FunctionHandler;
45
47
  export declare const handler: {
46
48
  inlineSql: typeof inlineSql;
47
49
  sqlReference: typeof sqlReference;
@@ -10,10 +10,13 @@ function getHandlerData(handler) {
10
10
  return handler[dataSymbol];
11
11
  }
12
12
  exports.getHandlerData = getHandlerData;
13
+ //#region handler.inlineSql
13
14
  const inlineSqlBrand = 'inlineSql';
14
15
  function inlineSql(sql) {
15
16
  return { [dataSymbol]: sql, ...buildHandler(inlineSqlBrand) };
16
17
  }
18
+ //#endregion
19
+ //#region handler.sqlReference
17
20
  const sqlReferenceBrand = 'sqlReference';
18
21
  function sqlReference(sqlReference) {
19
22
  return { [dataSymbol]: sqlReference, ...buildHandler(sqlReferenceBrand) };
@@ -31,6 +34,7 @@ const functionHandlerBrand = 'functionHandler';
31
34
  function fcn(fn) {
32
35
  return { [dataSymbol]: fn, ...buildHandler(functionHandlerBrand) };
33
36
  }
37
+ //#endregion
34
38
  exports.handler = {
35
39
  inlineSql,
36
40
  sqlReference,
@@ -155,6 +155,27 @@ function refFieldToGql(fieldDef) {
155
155
  }
156
156
  return field;
157
157
  }
158
+ function transformFunctionHandler(handlers, callSignature) {
159
+ let gqlHandlerContent = '';
160
+ const lambdaFunctionDefinition = {};
161
+ handlers.forEach((handler, idx) => {
162
+ const handlerData = (0, Handler_1.getHandlerData)(handler);
163
+ if (typeof handlerData === 'string') {
164
+ gqlHandlerContent += `@function(name: "${handlerData}") `;
165
+ }
166
+ else if (typeof handlerData.getInstance === 'function') {
167
+ const fnBaseName = `Fn_${callSignature}`;
168
+ const fnNameSuffix = idx === 0 ? '' : `_${idx + 1}`;
169
+ const fnName = fnBaseName + fnNameSuffix;
170
+ lambdaFunctionDefinition[fnName] = handlerData;
171
+ gqlHandlerContent += `@function(name: "${fnName}") `;
172
+ }
173
+ else {
174
+ throw new Error(`Invalid value specified for ${callSignature} handler.function(). Expected: defineFunction or string.`);
175
+ }
176
+ });
177
+ return { gqlHandlerContent, lambdaFunctionDefinition };
178
+ }
158
179
  function customOperationToGql(typeName, typeDef, authorization, isCustom = false, databaseType) {
159
180
  const { arguments: fieldArgs, returnType, functionRef, handlers, } = typeDef.data;
160
181
  let callSignature = typeName;
@@ -184,7 +205,11 @@ function customOperationToGql(typeName, typeDef, authorization, isCustom = false
184
205
  const handler = handlers && handlers[0];
185
206
  const brand = handler && (0, util_1.getBrand)(handler);
186
207
  let gqlHandlerContent = '';
187
- if (functionRef) {
208
+ let lambdaFunctionDefinition = {};
209
+ if (isFunctionHandler(handlers)) {
210
+ ({ gqlHandlerContent, lambdaFunctionDefinition } = transformFunctionHandler(handlers, callSignature));
211
+ }
212
+ else if (functionRef) {
188
213
  gqlHandlerContent = `@function(name: "${functionRef}") `;
189
214
  }
190
215
  else if (databaseType === 'sql' && handler && brand === 'inlineSql') {
@@ -194,7 +219,7 @@ function customOperationToGql(typeName, typeDef, authorization, isCustom = false
194
219
  gqlHandlerContent = `@sql(reference: "${(0, Handler_1.getHandlerData)(handler)}") `;
195
220
  }
196
221
  const gqlField = `${callSignature}: ${returnTypeName} ${gqlHandlerContent}${authString}`;
197
- return { gqlField, models: implicitModels };
222
+ return { gqlField, models: implicitModels, lambdaFunctionDefinition };
198
223
  }
199
224
  /**
200
225
  * Escape a string that will be used inside of a graphql string.
@@ -692,6 +717,7 @@ const schemaPreprocessor = (schema) => {
692
717
  const customMutations = [];
693
718
  const customSubscriptions = [];
694
719
  const jsFunctions = [];
720
+ let lambdaFunctions = {};
695
721
  const databaseType = schema.data.configuration.database.engine === 'dynamodb'
696
722
  ? 'dynamodb'
697
723
  : 'sql';
@@ -725,7 +751,8 @@ const schemaPreprocessor = (schema) => {
725
751
  }
726
752
  else if (isCustomOperation(typeDef)) {
727
753
  const { typeName: opType } = typeDef.data;
728
- const { gqlField, models, jsFunctionForField } = transformCustomOperations(typeDef, typeName, mostRelevantAuthRules, databaseType);
754
+ const { gqlField, models, jsFunctionForField, lambdaFunctionDefinition, } = transformCustomOperations(typeDef, typeName, mostRelevantAuthRules, databaseType);
755
+ lambdaFunctions = lambdaFunctionDefinition;
729
756
  topLevelTypes.push(...models);
730
757
  if (jsFunctionForField) {
731
758
  jsFunctions.push(jsFunctionForField);
@@ -776,7 +803,12 @@ const schemaPreprocessor = (schema) => {
776
803
  };
777
804
  gqlModels.push(...generateCustomOperationTypes(customOperations));
778
805
  const processedSchema = gqlModels.join('\n\n');
779
- return { schema: processedSchema, jsFunctions, functionSchemaAccess };
806
+ return {
807
+ schema: processedSchema,
808
+ jsFunctions,
809
+ functionSchemaAccess,
810
+ lambdaFunctions,
811
+ };
780
812
  };
781
813
  function validateCustomOperations(typeDef, typeName, authRules) {
782
814
  const { functionRef, handlers } = typeDef.data;
@@ -806,6 +838,9 @@ function validateCustomOperations(typeDef, typeName, authRules) {
806
838
  const isCustomHandler = (handler) => {
807
839
  return Array.isArray(handler) && (0, util_1.getBrand)(handler[0]) === 'customHandler';
808
840
  };
841
+ const isFunctionHandler = (handler) => {
842
+ return Array.isArray(handler) && (0, util_1.getBrand)(handler[0]) === 'functionHandler';
843
+ };
809
844
  const normalizeDataSourceName = (dataSource) => {
810
845
  // default data source
811
846
  const noneDataSourceName = 'NONE_DS';
@@ -867,8 +902,8 @@ function transformCustomOperations(typeDef, typeName, authRules, databaseType) {
867
902
  jsFunctionForField = handleCustom(handlers, opType, typeName);
868
903
  }
869
904
  const isCustom = Boolean(jsFunctionForField);
870
- const { gqlField, models } = customOperationToGql(typeName, typeDef, authRules, isCustom, databaseType);
871
- return { gqlField, models, jsFunctionForField };
905
+ const { gqlField, models, lambdaFunctionDefinition } = customOperationToGql(typeName, typeDef, authRules, isCustom, databaseType);
906
+ return { gqlField, models, jsFunctionForField, lambdaFunctionDefinition };
872
907
  }
873
908
  function generateCustomOperationTypes({ queries, mutations, subscriptions, }) {
874
909
  const types = [];
@@ -889,7 +924,13 @@ function generateCustomOperationTypes({ queries, mutations, subscriptions, }) {
889
924
  * @returns DerivedApiDefinition that conforms to IAmplifyGraphqlDefinition
890
925
  */
891
926
  function processSchema(arg) {
892
- const { schema, jsFunctions, functionSchemaAccess } = schemaPreprocessor(arg.schema);
893
- return { schema, functionSlots: [], jsFunctions, functionSchemaAccess };
927
+ const { schema, jsFunctions, functionSchemaAccess, lambdaFunctions } = schemaPreprocessor(arg.schema);
928
+ return {
929
+ schema,
930
+ functionSlots: [],
931
+ jsFunctions,
932
+ functionSchemaAccess,
933
+ lambdaFunctions,
934
+ };
894
935
  }
895
936
  exports.processSchema = processSchema;