@aws-amplify/graphql-transformer-core
Version:
A framework to transform from GraphQL SDL to AWS CloudFormation.
67 lines • 3.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getImplicitlyDefinedIdField = exports.getConnectionName = exports.getSubscriptionFilterInputName = exports.getConditionInputName = exports.getFilterInputName = exports.getPrimaryKeyFields = exports.getPrimaryKeyFieldNodes = void 0;
const graphql_transformer_common_1 = require("graphql-transformer-common");
const graphql_utils_1 = require("./graphql-utils");
const getPrimaryKeyFieldNodes = (type) => {
var _a, _b, _c, _d;
const primaryKeyField = (_a = type.fields) === null || _a === void 0 ? void 0 : _a.find((f) => { var _a; return (_a = f.directives) === null || _a === void 0 ? void 0 : _a.some((d) => d.name.value === 'primaryKey'); });
if (!primaryKeyField) {
return [(0, exports.getImplicitlyDefinedIdField)()];
}
const primaryKeyDirective = (_b = primaryKeyField === null || primaryKeyField === void 0 ? void 0 : primaryKeyField.directives) === null || _b === void 0 ? void 0 : _b.find((d) => d.name.value === 'primaryKey');
const result = [primaryKeyField];
const sortKeys = (_d = (_c = primaryKeyDirective === null || primaryKeyDirective === void 0 ? void 0 : primaryKeyDirective.arguments) === null || _c === void 0 ? void 0 : _c.find((a) => a.name.value === 'sortKeyFields')) === null || _d === void 0 ? void 0 : _d.value;
if (sortKeys) {
const fieldFinder = (fieldName) => (0, graphql_utils_1.getField)(type, fieldName);
const sortKeyNames = sortKeys.values.map((v) => v.value);
const sortFields = sortKeyNames.map(fieldFinder);
for (const sortField of sortFields) {
if (!sortField) {
throw new Error(`Invalid sort key field name in primary key directive: ${sortKeyNames}`);
}
}
result.push(...sortFields);
}
return result;
};
exports.getPrimaryKeyFieldNodes = getPrimaryKeyFieldNodes;
const getPrimaryKeyFields = (type) => {
return (0, exports.getPrimaryKeyFieldNodes)(type).map((f) => f.name.value);
};
exports.getPrimaryKeyFields = getPrimaryKeyFields;
const getFilterInputName = (modelName) => {
return (0, graphql_transformer_common_1.toPascalCase)(['Model', modelName, 'FilterInput']);
};
exports.getFilterInputName = getFilterInputName;
const getConditionInputName = (modelName) => {
return (0, graphql_transformer_common_1.toPascalCase)(['Model', modelName, 'ConditionInput']);
};
exports.getConditionInputName = getConditionInputName;
const getSubscriptionFilterInputName = (modelName) => {
return (0, graphql_transformer_common_1.toPascalCase)(['ModelSubscription', modelName, 'FilterInput']);
};
exports.getSubscriptionFilterInputName = getSubscriptionFilterInputName;
const getConnectionName = (modelName) => {
return (0, graphql_transformer_common_1.toPascalCase)(['Model', modelName, 'Connection']);
};
exports.getConnectionName = getConnectionName;
const getImplicitlyDefinedIdField = () => ({
kind: 'FieldDefinition',
name: {
kind: 'Name',
value: 'id',
},
type: {
kind: 'NonNullType',
type: {
kind: 'NamedType',
name: {
kind: 'Name',
value: 'ID',
},
},
},
});
exports.getImplicitlyDefinedIdField = getImplicitlyDefinedIdField;
//# sourceMappingURL=model-util.js.map