@graphql-codegen/python-common
Version:
Shared Python-specific helpers (reserved keywords, scalar mapping, identifier naming) for @graphql-codegen/python and @graphql-codegen/python-operations.
42 lines (41 loc) • 2.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.pascalCase = exports.constantCase = void 0;
exports.pythonFieldName = pythonFieldName;
exports.isReservedField = isReservedField;
const change_case_all_1 = require("change-case-all");
Object.defineProperty(exports, "constantCase", { enumerable: true, get: function () { return change_case_all_1.constantCase; } });
Object.defineProperty(exports, "pascalCase", { enumerable: true, get: function () { return change_case_all_1.pascalCase; } });
const keywords_js_1 = require("./keywords.js");
/**
* This module is the single place in the Python plugin family that imports
* `change-case-all`. The plugins route their snake / pascal / constant casing
* through these exports so the generated names line up exactly and no consumer
* hand-rolls (or diverges on) the acronym-splitting rules.
*/
/**
* snake_case a GraphQL name for use as a Python identifier (field, method, or
* document-constant base name), appending a trailing `_` when the result
* collides with a reserved keyword.
*
* Uses change-case-all's `snakeCase` — the same implementation
* `@graphql-codegen/python`'s `pascalCase` (via `BaseVisitor.convertName`)
* already relies on — so acronym runs split identically on both sides, e.g.
* `httpURL` -> `http_url` lines up with `HttpUrl`. A hand-rolled
* implementation previously diverged for `pascalCase` on `GetHTTPStatus`.
*
* Each consumer previously duplicated this exact function under a locally
* meaningful name (`methodName`, `pyFieldName`, `snakeFieldName`); import and
* alias it the same way here instead of re-implementing it.
*/
function pythonFieldName(name) {
const snake = (0, change_case_all_1.snakeCase)(name);
return keywords_js_1.PYTHON_KEYWORDS.has(snake) ? `${snake}_` : snake;
}
/**
* Whether a GraphQL response key maps to a Python reserved keyword once
* snake-cased (before any trailing-`_` escaping is applied).
*/
function isReservedField(name) {
return keywords_js_1.PYTHON_KEYWORDS.has((0, change_case_all_1.snakeCase)(name));
}