@aws-amplify/data-schema 0.13.11 → 0.13.12

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.
@@ -5,7 +5,7 @@ import { Authorization } from './Authorization';
5
5
  import { RefType, InternalRef } from './RefType';
6
6
  import { EnumType, EnumTypeParamShape } from './EnumType';
7
7
  import { CustomType } from './CustomType';
8
- import type { HandlerType as Handler } from './Handler';
8
+ import type { CustomHandler, FunctionHandler, HandlerType as Handler } from './Handler';
9
9
  declare const queryBrand = "queryCustomOperation";
10
10
  declare const mutationBrand = "mutationCustomOperation";
11
11
  declare const subscriptionBrand = "subscriptionCustomOperation";
@@ -15,7 +15,7 @@ type CustomReturnType = RefType<any> | CustomType<any>;
15
15
  type CustomFunctionRefType = string;
16
16
  type InternalCustomArguments = Record<string, InternalField>;
17
17
  type InternalCustomReturnType = InternalRef;
18
- type HandlerInputType = Handler | Handler[number];
18
+ type HandlerInputType = FunctionHandler[] | CustomHandler[] | Handler;
19
19
  export declare const CustomOperationNames: readonly ["Query", "Mutation", "Subscription"];
20
20
  type CustomOperationName = (typeof CustomOperationNames)[number];
21
21
  type CustomData = {
@@ -24,7 +24,7 @@ type CustomData = {
24
24
  functionRef: string | null;
25
25
  authorization: Authorization<any, any, any>[];
26
26
  typeName: CustomOperationName;
27
- handlers: Handler | null;
27
+ handlers: Handler[] | null;
28
28
  };
29
29
  type InternalCustomData = CustomData & {
30
30
  arguments: InternalCustomArguments;
@@ -1,6 +1,6 @@
1
1
  import { Brand } from './util';
2
2
  import { RefType } from './RefType';
3
- export type HandlerType = InlineSqlHandler[] | SqlReferenceHandler[] | CustomHandler[] | FunctionHandler[];
3
+ export type HandlerType = InlineSqlHandler | SqlReferenceHandler | CustomHandler | FunctionHandler;
4
4
  declare const dataSymbol: unique symbol;
5
5
  type AllHandlers = InlineSqlHandler | SqlReferenceHandler | CustomHandler | FunctionHandler;
6
6
  export declare function getHandlerData<H extends AllHandlers>(handler: H): H[typeof dataSymbol];
@@ -45,6 +45,7 @@ export type InternalSchema = {
45
45
  data: {
46
46
  types: InternalSchemaModels;
47
47
  authorization: SchemaAuthorization<any, any, any>[];
48
+ configuration: SchemaConfig<any, any>;
48
49
  };
49
50
  };
50
51
  export type ModelSchema<T extends ModelSchemaParamShape, UsedMethods extends 'authorization' = never> = Omit<{
@@ -155,8 +155,8 @@ function refFieldToGql(fieldDef) {
155
155
  }
156
156
  return field;
157
157
  }
158
- function customOperationToGql(typeName, typeDef, authorization, isCustom = false) {
159
- const { arguments: fieldArgs, returnType, functionRef } = typeDef.data;
158
+ function customOperationToGql(typeName, typeDef, authorization, isCustom = false, databaseType) {
159
+ const { arguments: fieldArgs, returnType, functionRef, handlers, } = typeDef.data;
160
160
  let callSignature = typeName;
161
161
  const implicitModels = [];
162
162
  const { authString } = isCustom
@@ -181,10 +181,29 @@ function customOperationToGql(typeName, typeDef, authorization, isCustom = false
181
181
  callSignature += `(${gqlFields.join(', ')})`;
182
182
  implicitModels.push(...models);
183
183
  }
184
- const fnString = functionRef ? `@function(name: "${functionRef}") ` : '';
185
- const gqlField = `${callSignature}: ${returnTypeName} ${fnString}${authString}`;
184
+ const handler = handlers && handlers[0];
185
+ const brand = handler && (0, util_1.getBrand)(handler);
186
+ let gqlHandlerContent = '';
187
+ if (functionRef) {
188
+ gqlHandlerContent = `@function(name: "${functionRef}") `;
189
+ }
190
+ else if (databaseType === 'sql' && handler && brand === 'inlineSql') {
191
+ gqlHandlerContent = `@sql(statement: ${escapeGraphQlString(String((0, Handler_1.getHandlerData)(handler)))}) `;
192
+ }
193
+ else if (databaseType === 'sql' && handler && brand === 'sqlReference') {
194
+ gqlHandlerContent = `@sql(reference: "${(0, Handler_1.getHandlerData)(handler)}") `;
195
+ }
196
+ const gqlField = `${callSignature}: ${returnTypeName} ${gqlHandlerContent}${authString}`;
186
197
  return { gqlField, models: implicitModels };
187
198
  }
199
+ /**
200
+ * Escape a string that will be used inside of a graphql string.
201
+ * @param str The input string to be escaped
202
+ * @returns The string with special charactars escaped
203
+ */
204
+ function escapeGraphQlString(str) {
205
+ return JSON.stringify(str);
206
+ }
188
207
  /**
189
208
  * Tests whether two ModelField definitions are in conflict.
190
209
  *
@@ -673,6 +692,9 @@ const schemaPreprocessor = (schema) => {
673
692
  const customMutations = [];
674
693
  const customSubscriptions = [];
675
694
  const jsFunctions = [];
695
+ const databaseType = schema.data.configuration.database.engine === 'dynamodb'
696
+ ? 'dynamodb'
697
+ : 'sql';
676
698
  const fkFields = allImpliedFKs(schema);
677
699
  const topLevelTypes = Object.entries(schema.data.types);
678
700
  const { schemaAuth, functionSchemaAccess } = extractFunctionSchemaAccess(schema.data.authorization);
@@ -703,7 +725,7 @@ const schemaPreprocessor = (schema) => {
703
725
  }
704
726
  else if (isCustomOperation(typeDef)) {
705
727
  const { typeName: opType } = typeDef.data;
706
- const { gqlField, models, jsFunctionForField } = transformCustomOperations(typeDef, typeName, mostRelevantAuthRules);
728
+ const { gqlField, models, jsFunctionForField } = transformCustomOperations(typeDef, typeName, mostRelevantAuthRules, databaseType);
707
729
  topLevelTypes.push(...models);
708
730
  if (jsFunctionForField) {
709
731
  jsFunctions.push(jsFunctionForField);
@@ -837,7 +859,7 @@ const handleCustom = (handlers, opType, typeName) => {
837
859
  };
838
860
  return jsFn;
839
861
  };
840
- function transformCustomOperations(typeDef, typeName, authRules) {
862
+ function transformCustomOperations(typeDef, typeName, authRules, databaseType) {
841
863
  const { typeName: opType, handlers } = typeDef.data;
842
864
  let jsFunctionForField = undefined;
843
865
  validateCustomOperations(typeDef, typeName, authRules);
@@ -845,7 +867,7 @@ function transformCustomOperations(typeDef, typeName, authRules) {
845
867
  jsFunctionForField = handleCustom(handlers, opType, typeName);
846
868
  }
847
869
  const isCustom = Boolean(jsFunctionForField);
848
- const { gqlField, models } = customOperationToGql(typeName, typeDef, authRules, isCustom);
870
+ const { gqlField, models } = customOperationToGql(typeName, typeDef, authRules, isCustom, databaseType);
849
871
  return { gqlField, models, jsFunctionForField };
850
872
  }
851
873
  function generateCustomOperationTypes({ queries, mutations, subscriptions, }) {