UNPKG

@graphql-codegen/flutter-freezed

Version:

GraphQL Code Generator plugin to generate Freezed models from your GraphQL schema

27 lines (26 loc) 1.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.plugin = void 0; const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers"); const schema_ast_1 = require("@graphql-codegen/schema-ast"); const index_js_1 = require("./freezed-declaration-blocks/index.js"); const schema_visitor_js_1 = require("./schema-visitor.js"); const utils_js_1 = require("./utils.js"); const plugin = (schema, _documents, config) => { // sets the defaults for the config config = { ...new utils_js_1.DefaultFreezedPluginConfig(config) }; const { schema: _schema, ast } = (0, schema_ast_1.transformSchemaAST)(schema, config); const { freezedFactoryBlockRepository, ...visitor } = (0, schema_visitor_js_1.schemaVisitor)(_schema, config); const visitorResult = (0, plugin_helpers_1.oldVisit)(ast, { leave: visitor }); const generated = visitorResult.definitions.filter((def) => def instanceof index_js_1.FreezedDeclarationBlock); return ((0, utils_js_1.addFreezedImportStatements)(config.fileName) + generated .map(freezedDeclarationBlock => freezedDeclarationBlock.toString().replace(/==>factory==>.+\n/gm, s => { const pattern = s.replace('==>factory==>', '').trim(); const [key, appliesOn, name, typeName] = pattern.split('==>'); return freezedFactoryBlockRepository.retrieve(key, appliesOn, name, typeName !== null && typeName !== void 0 ? typeName : null); })) .join('') .trim()); }; exports.plugin = plugin;