UNPKG

9.29 kBSource Map (JSON)View Raw
1{"version":3,"file":"index.esm.js","sources":["../../../dist/plugins/typescript/react-apollo-offix/src/visitor.js","../../../dist/plugins/typescript/react-apollo-offix/src/index.js"],"sourcesContent":["import { ClientSideBaseVisitor, DocumentMode, } from '@graphql-codegen/visitor-plugin-common';\nimport autoBind from 'auto-bind';\nimport { pascalCase } from 'pascal-case';\nexport class ReactApolloVisitor extends ClientSideBaseVisitor {\n constructor(schema, fragments, rawConfig, documents) {\n super(schema, fragments, rawConfig, {});\n this.imports = new Set();\n this._externalImportPrefix = this.config.importOperationTypesFrom ? `${this.config.importOperationTypesFrom}.` : '';\n this._documents = documents;\n autoBind(this);\n }\n getOffixReactHooksImport() {\n return `import * as OffixReactHooks from \"react-offix-hooks\";`;\n }\n getDocumentNodeVariable(node, documentVariableName) {\n return this.config.documentMode === DocumentMode.external ? `Operations.${node.name.value}` : documentVariableName;\n }\n getImports() {\n const baseImports = super.getImports({ excludeFragments: true });\n const hasOperations = this._collectedOperations.length > 0;\n if (!hasOperations) {\n return baseImports;\n }\n return [...baseImports, ...Array.from(this.imports)];\n }\n _buildHooks(node, operationType, documentVariableName, operationResultType, operationVariablesTypes) {\n const operationName = this.convertName(node.name.value, {\n useTypesPrefix: false,\n });\n this.imports.add(this.getOffixReactHooksImport());\n const hookFns = [];\n if (operationType === 'Mutation') {\n hookFns.push(`export function useOffline${operationName}(baseOptions?: OffixReactHooks.${operationType}HookOptions<${operationResultType}, ${operationVariablesTypes}>) {\n return OffixReactHooks.useOfflineMutation<${operationResultType}, ${operationVariablesTypes}>(${this.getDocumentNodeVariable(node, documentVariableName)}, baseOptions);\n}`);\n }\n return [...hookFns].join('\\n');\n }\n buildOperation(node, documentVariableName, operationType, operationResultType, operationVariablesTypes) {\n operationResultType = this._externalImportPrefix + operationResultType;\n operationVariablesTypes = this._externalImportPrefix + operationVariablesTypes;\n const hooks = this._buildHooks(node, operationType, documentVariableName, operationResultType, operationVariablesTypes);\n return [hooks].filter(a => a).join('\\n');\n }\n OperationDefinition(node) {\n if (!node.name || !node.name.value) {\n return null;\n }\n this._collectedOperations.push(node);\n const documentVariableName = this.convertName(node, {\n suffix: this.config.documentVariableSuffix,\n prefix: this.config.documentVariablePrefix,\n useTypesPrefix: false,\n });\n const operationType = pascalCase(node.operation);\n const operationTypeSuffix = this.getOperationSuffix(node, operationType);\n const operationResultType = this.convertName(node, {\n suffix: operationTypeSuffix + this._parsedConfig.operationResultSuffix,\n });\n const operationVariablesTypes = this.convertName(node, {\n suffix: operationTypeSuffix + 'Variables',\n });\n const additional = this.buildOperation(node, documentVariableName, operationType, operationResultType, operationVariablesTypes);\n return [additional].filter(a => a).join('\\n');\n }\n}\n//# sourceMappingURL=visitor.js.map","import { visit, concatAST, Kind } from 'graphql';\nimport { ReactApolloVisitor } from './visitor';\nimport { extname } from 'path';\nexport const plugin = (schema, documents, config) => {\n const allAst = concatAST(documents.map(v => v.document));\n const allFragments = [\n ...allAst.definitions.filter(d => d.kind === Kind.FRAGMENT_DEFINITION).map(fragmentDef => ({\n node: fragmentDef,\n name: fragmentDef.name.value,\n onType: fragmentDef.typeCondition.name.value,\n isExternal: false,\n })),\n ...(config.externalFragments || []),\n ];\n const visitor = new ReactApolloVisitor(schema, allFragments, config, documents);\n const visitorResult = visit(allAst, { leave: visitor });\n return {\n prepend: visitor.getImports(),\n content: [...visitorResult.definitions.filter(t => typeof t === 'string')].join('\\n'),\n };\n};\nexport const validate = async (_schema, _documents, _config, outputFile) => {\n if (extname(outputFile) !== '.tsx' && extname(outputFile) !== '.ts') {\n throw new Error(`Plugin \"react-apollo\" requires extension to be \".tsx\" or \".ts!`);\n }\n};\nexport { ReactApolloVisitor };\n//# sourceMappingURL=index.js.map"],"names":[],"mappings":";;;;;;AAGO,MAAM,kBAAkB,SAAS,qBAAqB,CAAC;AAC9D,IAAI,WAAW,CAAC,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE;AACzD,QAAQ,KAAK,CAAC,M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