UNPKG

@graphql-codegen/near-operation-file-preset

Version:

GraphQL Code Generator preset for generating operation code near the operation file

74 lines (73 loc) 3.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.resolveDocumentImports = resolveDocumentImports; const tslib_1 = require("tslib"); const graphql_1 = require("graphql"); const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers"); const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common"); const fragment_resolver_js_1 = tslib_1.__importDefault(require("./fragment-resolver.js")); /** * Transform the preset's provided documents into single-file generator sources, while resolving fragment and user-defined imports * * Resolves user provided imports and fragment imports using the `DocumentImportResolverOptions`. * Does not define specific plugins, but rather returns a string[] of `importStatements` for the calling plugin to make use of */ function resolveDocumentImports(presetOptions, schemaObject, importResolverOptions, // Original type is `schemaTypesSource: string | ImportSource`. We manually override `schemaTypesSource` type because we only use the `ImportSource` use case dedupeFragments = false) { const resolveFragments = (0, fragment_resolver_js_1.default)(importResolverOptions, presetOptions, schemaObject, dedupeFragments); const { baseOutputDir, documents } = presetOptions; const { generateFilePath, schemaTypesSource, baseDir, typesImport } = importResolverOptions; return documents .filter(documentFile => documentFile.type !== 'external') .map(documentFile => { try { const meta = { operations: [], fragments: [], }; for (const definition of documentFile.document.definitions) { if (definition.kind === graphql_1.Kind.OPERATION_DEFINITION) { meta.operations.push(definition); } else if (definition.kind === graphql_1.Kind.FRAGMENT_DEFINITION) { meta.fragments.push(definition); } } const generatedFilePath = generateFilePath({ location: documentFile.location, meta }); const importStatements = []; const { externalFragments, fragmentImports } = resolveFragments(generatedFilePath, documentFile.document); const externalFragmentsInjectedDocument = { ...documentFile.document, definitions: [ ...documentFile.document.definitions, ...externalFragments.map(fragment => fragment.node), ], }; if ((0, plugin_helpers_1.isUsingTypes)(externalFragmentsInjectedDocument, [], schemaObject) && schemaTypesSource.namespace) { const schemaTypesImportStatement = (0, visitor_plugin_common_1.generateImportStatement)({ baseDir, emitLegacyCommonJSImports: presetOptions.config.emitLegacyCommonJSImports, importExtension: presetOptions.config.importExtension, importSource: (0, visitor_plugin_common_1.resolveImportSource)(schemaTypesSource), baseOutputDir, outputPath: generatedFilePath, typesImport, }); importStatements.unshift(schemaTypesImportStatement); } return { filename: generatedFilePath, documents: [documentFile], importStatements, fragmentImports, externalFragments, }; } catch (e) { throw new Error(`Unable to validate GraphQL document! \n File ${documentFile.location} caused error: ${e.message || e.toString()}`); } }); }