@graphql-codegen/urql-svelte-operations-store
Version:
GraphQL Code Generator plugin for generating wrapper for OperationsStore for Svelte & Urql
46 lines (45 loc) • 2.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.plugin = void 0;
const change_case_all_1 = require("change-case-all");
const graphql_1 = require("graphql");
const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
function getOperationSuffix(config, node, operationType) {
const { omitOperationSuffix = false, dedupeOperationSuffix = false } = config || {};
const operationName = typeof node === 'string' ? node : node.name ? node.name.value : '';
return omitOperationSuffix
? ''
: dedupeOperationSuffix && operationName.toLowerCase().endsWith(operationType.toLowerCase())
? ''
: operationType;
}
const plugin = (schema, documents, config) => {
const allAst = (0, graphql_1.concatAST)(documents.map(v => v.document));
const convertName = (0, visitor_plugin_common_1.convertFactory)(config);
const operationResultSuffix = (0, visitor_plugin_common_1.getConfigValue)(config.operationResultSuffix, '');
const out = allAst.definitions
.map(node => {
var _a;
if (node.kind === 'OperationDefinition' && ((_a = node.name) === null || _a === void 0 ? void 0 : _a.value)) {
const operationType = (0, change_case_all_1.pascalCase)(node.operation);
const operationTypeSuffix = getOperationSuffix(config, node, operationType);
const operationVariablesTypes = convertName(node, {
suffix: operationTypeSuffix + 'Variables',
});
const storeTypeName = convertName(node, {
suffix: operationTypeSuffix + 'Store',
});
const operationResultType = convertName(node, {
suffix: operationTypeSuffix + operationResultSuffix,
});
return `export type ${storeTypeName} = OperationStore<${operationResultType}, ${operationVariablesTypes}>;`;
}
return null;
})
.filter(Boolean);
return {
prepend: [`import type { OperationStore } from '@urql/svelte';`],
content: out.filter(Boolean).join('\n'),
};
};
exports.plugin = plugin;