|
| 1 | +import { |
| 2 | + AmplifyAppSyncSimulatorAuthenticationType as AuthTypes, |
| 3 | +} from 'amplify-appsync-simulator'; |
| 4 | +import { invoke } from 'amplify-util-mock/lib/utils/lambda/invoke'; |
| 5 | +import fs from 'fs'; |
| 6 | +import { find } from 'lodash'; |
| 7 | +import path from 'path'; |
| 8 | + |
| 9 | +export default function getAppSyncConfig(context, appSyncConfig) { |
| 10 | + const getFileMap = (basePath, filePath) => ({ |
| 11 | + path: filePath, |
| 12 | + content: fs.readFileSync(path.join(basePath, filePath), { encoding: 'utf8' }), |
| 13 | + }); |
| 14 | + |
| 15 | + const makeDataSource = (source) => { |
| 16 | + if (source.name === undefined || source.type === undefined) { |
| 17 | + return null; |
| 18 | + } |
| 19 | + |
| 20 | + const dataSource = { |
| 21 | + name: source.name, |
| 22 | + type: source.type, |
| 23 | + }; |
| 24 | + |
| 25 | + switch (source.type) { |
| 26 | + case 'AMAZON_DYNAMODB': { |
| 27 | + const { port } = context.options.dynamoDb; |
| 28 | + return { |
| 29 | + ...dataSource, |
| 30 | + config: { |
| 31 | + endpoint: `http://localhost:${port}`, |
| 32 | + region: 'localhost', |
| 33 | + tableName: source.config.tableName, // FIXME: Handle Ref: |
| 34 | + }, |
| 35 | + }; |
| 36 | + } |
| 37 | + case 'AWS_LAMBDA': { |
| 38 | + const { functionName } = source.config; |
| 39 | + if (context.serverless.service.functions[functionName] === undefined) { |
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + const [fileName, handler] = context.serverless.service.functions[functionName].handler.split('.'); |
| 44 | + return { |
| 45 | + ...dataSource, |
| 46 | + invoke: (payload) => invoke({ |
| 47 | + packageFolder: context.serverless.config.servicePath, |
| 48 | + handler, |
| 49 | + fileName: path.join(context.options.location, fileName), |
| 50 | + event: payload, |
| 51 | + environment: context.serverless.service.provider.environment || {}, |
| 52 | + }), |
| 53 | + }; |
| 54 | + } |
| 55 | + default: |
| 56 | + return dataSource; |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + const makeResolver = (resolver) => ({ |
| 61 | + kind: 'UNIT', |
| 62 | + fieldName: resolver.field, |
| 63 | + typeName: resolver.type, |
| 64 | + dataSourceName: resolver.dataSource, |
| 65 | + requestMappingTemplateLocation: resolver.request, |
| 66 | + responseMappingTemplateLocation: resolver.response, |
| 67 | + }); |
| 68 | + |
| 69 | + const makeAuthType = (authType) => { |
| 70 | + const auth = { |
| 71 | + authenticationType: authType.authenticationType, |
| 72 | + }; |
| 73 | + |
| 74 | + if (auth.authenticationType === AuthTypes.AMAZON_COGNITO_USER_POOLS) { |
| 75 | + auth.cognitoUserPoolConfig = { |
| 76 | + AppIdClientRegex: authType.userPoolConfig.appIdClientRegex, |
| 77 | + }; |
| 78 | + } else if (auth.authenticationType === AuthTypes.OPENID_CONNECT) { |
| 79 | + auth.openIDConnectConfig = { |
| 80 | + Issuer: authType.openIdConnectConfig.issuer, |
| 81 | + ClientId: authType.openIdConnectConfig.clientId, |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + return auth; |
| 86 | + }; |
| 87 | + |
| 88 | + const makeAppSync = () => ({ |
| 89 | + name: appSyncConfig.name, |
| 90 | + apiKey: context.options.apiKey || '123456', |
| 91 | + defaultAuthenticationType: makeAuthType(appSyncConfig), |
| 92 | + additionalAuthenticationProviders: (appSyncConfig.additionalAuthenticationProviders || []) |
| 93 | + .map(makeAuthType), |
| 94 | + }); |
| 95 | + |
| 96 | + const mappingTemplatesLocation = path.join( |
| 97 | + context.serverless.config.servicePath, |
| 98 | + appSyncConfig.mappingTemplatesLocation || 'mapping-templates', |
| 99 | + ); |
| 100 | + |
| 101 | + return { |
| 102 | + appSync: makeAppSync(), |
| 103 | + schema: getFileMap(context.serverless.config.servicePath, appSyncConfig.schema || 'schema.graphql'), |
| 104 | + resolvers: appSyncConfig.mappingTemplates.map(makeResolver), |
| 105 | + dataSources: appSyncConfig.dataSources.map(makeDataSource).filter((v) => v !== null), |
| 106 | + mappingTemplates: appSyncConfig.mappingTemplates.reduce((acc, template) => { |
| 107 | + const requestTemplate = template.request || `${template.type}.${template.field}.request.vtl`; |
| 108 | + if (!find(acc, (e) => e.path === requestTemplate)) { |
| 109 | + acc.push(getFileMap(mappingTemplatesLocation, requestTemplate)); |
| 110 | + } |
| 111 | + const responseTemplate = template.response || `${template.type}.${template.field}.response.vtl`; |
| 112 | + if (!find(acc, (e) => e.path === responseTemplate)) { |
| 113 | + acc.push(getFileMap(mappingTemplatesLocation, responseTemplate)); |
| 114 | + } |
| 115 | + |
| 116 | + return acc; |
| 117 | + }, []), |
| 118 | + }; |
| 119 | +} |
0 commit comments