@aws-amplify/graphql-transformer-core
Version:
A framework to transform from GraphQL SDL to AWS CloudFormation.
43 lines • 2.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getStrategyDbTypeFromTypeNode = exports.getSortKeyFieldNames = exports.getTable = exports.getKeySchema = void 0;
const graphql_transformer_common_1 = require("graphql-transformer-common");
const model_datasource_strategy_utils_1 = require("./model-datasource-strategy-utils");
const getKeySchema = (table, indexName) => {
var _a, _b, _c;
return (_c = (_b = ((_a = table.globalSecondaryIndexes.find((gsi) => gsi.indexName === indexName)) !== null && _a !== void 0 ? _a : table.localSecondaryIndexes.find((gsi) => gsi.indexName === indexName))) === null || _b === void 0 ? void 0 : _b.keySchema) !== null && _c !== void 0 ? _c : table.keySchema;
};
exports.getKeySchema = getKeySchema;
const getTable = (ctx, object) => {
const ddbDataSource = ctx.dataSources.get(object);
const tableName = graphql_transformer_common_1.ModelResourceIDs.ModelTableResourceID(object.name.value);
const table = ddbDataSource.ds.stack.node.findChild(tableName);
if (!table) {
throw new Error(`Table not found for name ${tableName}`);
}
return table;
};
exports.getTable = getTable;
const getSortKeyFieldNames = (type) => {
const sortKeyFieldNames = [];
type.fields.forEach((field) => {
field.directives.forEach((directive) => {
var _a, _b;
if (directive.name.value === 'primaryKey') {
const values = (_b = (_a = directive.arguments) === null || _a === void 0 ? void 0 : _a.find((arg) => arg.name.value === 'sortKeyFields')) === null || _b === void 0 ? void 0 : _b.value;
if (values) {
sortKeyFieldNames.push(...values.values.map((it) => it.value));
}
}
});
});
return sortKeyFieldNames;
};
exports.getSortKeyFieldNames = getSortKeyFieldNames;
const getStrategyDbTypeFromTypeNode = (type, ctx) => {
const baseTypeName = (0, graphql_transformer_common_1.getBaseType)(type);
const strategy = (0, model_datasource_strategy_utils_1.getModelDataSourceStrategy)(ctx, baseTypeName);
return strategy.dbType;
};
exports.getStrategyDbTypeFromTypeNode = getStrategyDbTypeFromTypeNode;
//# sourceMappingURL=schema-utils.js.map