|
| 1 | +// @ts-nocheck |
| 2 | +"use strict"; |
| 3 | + |
| 4 | +/** |
| 5 | + * This file is a copy of document-typed-node but it outputs graphql strings |
| 6 | + * instead of compiled graphql document nodes. The reason to do this is that to |
| 7 | + * parse the documents again to send with queries requires importing print from |
| 8 | + * graphql, which adds a significant amount of code to the output bundle. |
| 9 | + */ |
| 10 | + |
| 11 | +const { print } = require("graphql"); |
| 12 | +const { oldVisit } = require("@graphql-codegen/plugin-helpers"); |
| 13 | +const { |
| 14 | + optimizeOperations, |
| 15 | +} = require("@graphql-codegen/visitor-plugin-common"); |
| 16 | +const { concatAST, Kind } = require("graphql"); |
| 17 | +const tslib_1 = require("tslib"); |
| 18 | +const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common"); |
| 19 | +const auto_bind_1 = tslib_1.__importDefault(require("auto-bind")); |
| 20 | + |
| 21 | +class TypeScriptDocumentNodesVisitor extends visitor_plugin_common_1.ClientSideBaseVisitor { |
| 22 | + constructor(schema, fragments, config, documents) { |
| 23 | + super( |
| 24 | + schema, |
| 25 | + fragments, |
| 26 | + { |
| 27 | + documentMode: |
| 28 | + visitor_plugin_common_1.DocumentMode.documentNodeImportFragments, |
| 29 | + documentNodeImport: |
| 30 | + "@graphql-typed-document-node/core#TypedDocumentNode", |
| 31 | + ...config, |
| 32 | + }, |
| 33 | + {}, |
| 34 | + documents |
| 35 | + ); |
| 36 | + this.pluginConfig = config; |
| 37 | + (0, auto_bind_1.default)(this); |
| 38 | + // We need to make sure it's there because in this mode, the base plugin doesn't add the import |
| 39 | + if ( |
| 40 | + this.config.documentMode === |
| 41 | + visitor_plugin_common_1.DocumentMode.graphQLTag |
| 42 | + ) { |
| 43 | + const documentNodeImport = this._parseImport( |
| 44 | + this.config.documentNodeImport || "graphql#DocumentNode" |
| 45 | + ); |
| 46 | + const tagImport = this._generateImport( |
| 47 | + documentNodeImport, |
| 48 | + "DocumentNode", |
| 49 | + true |
| 50 | + ); |
| 51 | + this._imports.add(tagImport); |
| 52 | + } |
| 53 | + } |
| 54 | + SelectionSet(node, _, parent) { |
| 55 | + if (!this.pluginConfig.addTypenameToSelectionSets) { |
| 56 | + return; |
| 57 | + } |
| 58 | + // Don't add __typename to OperationDefinitions. |
| 59 | + if (parent && parent.kind === "OperationDefinition") { |
| 60 | + return; |
| 61 | + } |
| 62 | + // No changes if no selections. |
| 63 | + const { selections } = node; |
| 64 | + if (!selections) { |
| 65 | + return; |
| 66 | + } |
| 67 | + // If selections already have a __typename or is introspection do nothing. |
| 68 | + const hasTypename = selections.some( |
| 69 | + (selection) => |
| 70 | + selection.kind === "Field" && |
| 71 | + (selection.name.value === "__typename" || |
| 72 | + selection.name.value.lastIndexOf("__", 0) === 0) |
| 73 | + ); |
| 74 | + if (hasTypename) { |
| 75 | + return; |
| 76 | + } |
| 77 | + return { |
| 78 | + ...node, |
| 79 | + selections: [ |
| 80 | + ...selections, |
| 81 | + { |
| 82 | + kind: "Field", |
| 83 | + name: { |
| 84 | + kind: "Name", |
| 85 | + value: "__typename", |
| 86 | + }, |
| 87 | + }, |
| 88 | + ], |
| 89 | + }; |
| 90 | + } |
| 91 | + getDocumentNodeSignature(resultType, variablesTypes, node) { |
| 92 | + if ( |
| 93 | + this.config.documentMode === |
| 94 | + visitor_plugin_common_1.DocumentMode.documentNode || |
| 95 | + this.config.documentMode === |
| 96 | + visitor_plugin_common_1.DocumentMode.documentNodeImportFragments || |
| 97 | + this.config.documentMode === |
| 98 | + visitor_plugin_common_1.DocumentMode.graphQLTag |
| 99 | + ) { |
| 100 | + return ` as unknown as DocumentNode<${resultType}, ${variablesTypes}>`; |
| 101 | + } |
| 102 | + return super.getDocumentNodeSignature(resultType, variablesTypes, node); |
| 103 | + } |
| 104 | + _gql(node) { |
| 105 | + const document = super._gql(node); |
| 106 | + return "`" + print(JSON.parse(document)) + "`"; |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +module.exports = { |
| 111 | + plugin: (schema, rawDocuments, config) => { |
| 112 | + const documents = config.flattenGeneratedTypes |
| 113 | + ? optimizeOperations(schema, rawDocuments) |
| 114 | + : rawDocuments; |
| 115 | + const allAst = concatAST(documents.map((v) => v.document)); |
| 116 | + const allFragments = [ |
| 117 | + ...allAst.definitions |
| 118 | + .filter((d) => d.kind === Kind.FRAGMENT_DEFINITION) |
| 119 | + .map((fragmentDef) => ({ |
| 120 | + node: fragmentDef, |
| 121 | + name: fragmentDef.name.value, |
| 122 | + onType: fragmentDef.typeCondition.name.value, |
| 123 | + isExternal: false, |
| 124 | + })), |
| 125 | + ...(config.externalFragments || []), |
| 126 | + ]; |
| 127 | + const visitor = new TypeScriptDocumentNodesVisitor( |
| 128 | + schema, |
| 129 | + allFragments, |
| 130 | + config, |
| 131 | + documents |
| 132 | + ); |
| 133 | + const visitorResult = oldVisit(allAst, { leave: visitor }); |
| 134 | + return { |
| 135 | + prepend: allAst.definitions.length === 0 ? [] : visitor.getImports(), |
| 136 | + content: [ |
| 137 | + visitor.fragments, |
| 138 | + ...visitorResult.definitions.filter((t) => typeof t === "string"), |
| 139 | + ].join("\n"), |
| 140 | + }; |
| 141 | + }, |
| 142 | +}; |
0 commit comments