1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
6 |
|
7 | const graphql = require('graphql');
|
8 | const visitorPluginCommon = require('@graphql-codegen/visitor-plugin-common');
|
9 | const autoBind = _interopDefault(require('auto-bind'));
|
10 | const pascalCase = require('pascal-case');
|
11 | const path = require('path');
|
12 |
|
13 | class ReactApolloVisitor extends visitorPluginCommon.ClientSideBaseVisitor {
|
14 | constructor(schema, fragments, rawConfig, documents) {
|
15 | super(schema, fragments, rawConfig, {});
|
16 | this.imports = new Set();
|
17 | this._externalImportPrefix = this.config.importOperationTypesFrom ? `${this.config.importOperationTypesFrom}.` : '';
|
18 | this._documents = documents;
|
19 | autoBind(this);
|
20 | }
|
21 | getOffixReactHooksImport() {
|
22 | return `import * as OffixReactHooks from "react-offix-hooks";`;
|
23 | }
|
24 | getDocumentNodeVariable(node, documentVariableName) {
|
25 | return this.config.documentMode === visitorPluginCommon.DocumentMode.external ? `Operations.${node.name.value}` : documentVariableName;
|
26 | }
|
27 | getImports() {
|
28 | const baseImports = super.getImports({ excludeFragments: true });
|
29 | const hasOperations = this._collectedOperations.length > 0;
|
30 | if (!hasOperations) {
|
31 | return baseImports;
|
32 | }
|
33 | return [...baseImports, ...Array.from(this.imports)];
|
34 | }
|
35 | _buildHooks(node, operationType, documentVariableName, operationResultType, operationVariablesTypes) {
|
36 | const operationName = this.convertName(node.name.value, {
|
37 | useTypesPrefix: false,
|
38 | });
|
39 | this.imports.add(this.getOffixReactHooksImport());
|
40 | const hookFns = [];
|
41 | if (operationType === 'Mutation') {
|
42 | hookFns.push(`export function useOffline${operationName}(baseOptions?: OffixReactHooks.${operationType}HookOptions<${operationResultType}, ${operationVariablesTypes}>) {
|
43 | return OffixReactHooks.useOfflineMutation<${operationResultType}, ${operationVariablesTypes}>(${this.getDocumentNodeVariable(node, documentVariableName)}, baseOptions);
|
44 | }`);
|
45 | }
|
46 | return [...hookFns].join('\n');
|
47 | }
|
48 | buildOperation(node, documentVariableName, operationType, operationResultType, operationVariablesTypes) {
|
49 | operationResultType = this._externalImportPrefix + operationResultType;
|
50 | operationVariablesTypes = this._externalImportPrefix + operationVariablesTypes;
|
51 | const hooks = this._buildHooks(node, operationType, documentVariableName, operationResultType, operationVariablesTypes);
|
52 | return [hooks].filter(a => a).join('\n');
|
53 | }
|
54 | OperationDefinition(node) {
|
55 | if (!node.name || !node.name.value) {
|
56 | return null;
|
57 | }
|
58 | this._collectedOperations.push(node);
|
59 | const documentVariableName = this.convertName(node, {
|
60 | suffix: this.config.documentVariableSuffix,
|
61 | prefix: this.config.documentVariablePrefix,
|
62 | useTypesPrefix: false,
|
63 | });
|
64 | const operationType = pascalCase.pascalCase(node.operation);
|
65 | const operationTypeSuffix = this.getOperationSuffix(node, operationType);
|
66 | const operationResultType = this.convertName(node, {
|
67 | suffix: operationTypeSuffix + this._parsedConfig.operationResultSuffix,
|
68 | });
|
69 | const operationVariablesTypes = this.convertName(node, {
|
70 | suffix: operationTypeSuffix + 'Variables',
|
71 | });
|
72 | const additional = this.buildOperation(node, documentVariableName, operationType, operationResultType, operationVariablesTypes);
|
73 | return [additional].filter(a => a).join('\n');
|
74 | }
|
75 | }
|
76 |
|
77 | const plugin = (schema, documents, config) => {
|
78 | const allAst = graphql.concatAST(documents.map(v => v.document));
|
79 | const allFragments = [
|
80 | ...allAst.definitions.filter(d => d.kind === graphql.Kind.FRAGMENT_DEFINITION).map(fragmentDef => ({
|
81 | node: fragmentDef,
|
82 | name: fragmentDef.name.value,
|
83 | onType: fragmentDef.typeCondition.name.value,
|
84 | isExternal: false,
|
85 | })),
|
86 | ...(config.externalFragments || []),
|
87 | ];
|
88 | const visitor = new ReactApolloVisitor(schema, allFragments, config, documents);
|
89 | const visitorResult = graphql.visit(allAst, { leave: visitor });
|
90 | return {
|
91 | prepend: visitor.getImports(),
|
92 | content: [...visitorResult.definitions.filter(t => typeof t === 'string')].join('\n'),
|
93 | };
|
94 | };
|
95 | const validate = async (_schema, _documents, _config, outputFile) => {
|
96 | if (path.extname(outputFile) !== '.tsx' && path.extname(outputFile) !== '.ts') {
|
97 | throw new Error(`Plugin "react-apollo" requires extension to be ".tsx" or ".ts!`);
|
98 | }
|
99 | };
|
100 |
|
101 | exports.ReactApolloVisitor = ReactApolloVisitor;
|
102 | exports.plugin = plugin;
|
103 | exports.validate = validate;
|
104 |
|