UNPKG

@aws-amplify/graphql-types-generator

Version:

Generate API code or type annotations based on a GraphQL schema and statements

70 lines 3.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.propertyFromFragmentSpread = exports.propertyFromInlineFragment = exports.propertyFromField = exports.caseClassNameForInlineFragment = exports.caseClassNameForFragmentName = exports.caseClassNameForPropertyName = exports.operationClassName = exports.enumCaseName = void 0; const change_case_1 = require("change-case"); const Inflector = require("inflected"); const printing_1 = require("../utilities/printing"); const language_1 = require("./language"); const types_1 = require("./types"); const graphql_1 = require("graphql"); function enumCaseName(name) { return (0, change_case_1.camelCase)(name); } exports.enumCaseName = enumCaseName; function operationClassName(name) { return (0, change_case_1.pascalCase)(name); } exports.operationClassName = operationClassName; function caseClassNameForPropertyName(propertyName) { return (0, change_case_1.pascalCase)(Inflector.singularize(propertyName)); } exports.caseClassNameForPropertyName = caseClassNameForPropertyName; function caseClassNameForFragmentName(fragmentName) { return (0, change_case_1.pascalCase)(fragmentName); } exports.caseClassNameForFragmentName = caseClassNameForFragmentName; function caseClassNameForInlineFragment(inlineFragment) { return 'As' + (0, change_case_1.pascalCase)(String(inlineFragment.typeCondition)); } exports.caseClassNameForInlineFragment = caseClassNameForInlineFragment; function propertyFromField(context, field, namespace) { const name = field.name || field.responseName; const unescapedPropertyName = isMetaFieldName(name) ? name : (0, change_case_1.camelCase)(name); const propertyName = (0, language_1.escapeIdentifierIfNeeded)(unescapedPropertyName); const type = field.type; const isList = type instanceof graphql_1.GraphQLList || type.ofType instanceof graphql_1.GraphQLList; const isOptional = field.isConditional || !(type instanceof graphql_1.GraphQLNonNull); const bareType = (0, graphql_1.getNamedType)(type); if ((0, graphql_1.isCompositeType)(bareType)) { const bareTypeName = (0, printing_1.join)([namespace, (0, language_1.escapeIdentifierIfNeeded)((0, change_case_1.pascalCase)(Inflector.singularize(name)))], '.'); const typeName = (0, types_1.typeNameFromGraphQLType)(context, type, bareTypeName, isOptional); return Object.assign(Object.assign({}, field), { propertyName, typeName, bareTypeName, isOptional, isList, isComposite: true }); } else { const typeName = (0, types_1.typeNameFromGraphQLType)(context, type, undefined, isOptional); return Object.assign(Object.assign({}, field), { propertyName, typeName, isOptional, isList, isComposite: false }); } } exports.propertyFromField = propertyFromField; function propertyFromInlineFragment(context, inlineFragment) { const structName = caseClassNameForInlineFragment(inlineFragment); const propertyName = (0, change_case_1.camelCase)(structName); const typeName = structName + '?'; return Object.assign({ propertyName, typeName, structName, isComposite: true }, inlineFragment); } exports.propertyFromInlineFragment = propertyFromInlineFragment; function propertyFromFragmentSpread(context, fragmentSpread) { const fragmentName = fragmentSpread; const fragment = context.fragments[fragmentName]; if (!fragment) { throw new graphql_1.GraphQLError(`Cannot find fragment "${fragmentName}"`); } const propertyName = (0, change_case_1.camelCase)(fragmentName); const typeName = caseClassNameForFragmentName(fragmentName); return { propertyName, typeName, fragment, isComposite: true }; } exports.propertyFromFragmentSpread = propertyFromFragmentSpread; function isMetaFieldName(name) { return name.startsWith('__'); } //# sourceMappingURL=naming.js.map