1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.VueApolloVisitor = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
|
6 | const auto_bind_1 = tslib_1.__importDefault(require("auto-bind"));
|
7 | const change_case_all_1 = require("change-case-all");
|
8 | function insertIf(condition, ...elements) {
|
9 | return condition ? elements : [];
|
10 | }
|
11 | class VueApolloVisitor extends visitor_plugin_common_1.ClientSideBaseVisitor {
|
12 | constructor(schema, fragments, rawConfig, documents) {
|
13 | super(schema, fragments, rawConfig, {
|
14 | withSmartOperationFunctions: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.withSmartOperationFunctions, true),
|
15 | vueApolloOperationFunctionsImportFrom: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.vueApolloOperationFunctionsImportFrom, 'vue-apollo-smart-ops'),
|
16 | vueApolloErrorType: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.vueApolloErrorType, 'ApolloError'),
|
17 | vueApolloErrorTypeImportFrom: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.vueApolloErrorTypeImportFrom, 'apollo-client'),
|
18 | vueApolloErrorHandlerFunction: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.vueApolloErrorHandlerFunction, undefined),
|
19 | vueApolloErrorHandlerFunctionImportFrom: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.vueApolloErrorHandlerFunctionImportFrom, undefined),
|
20 | vueAppType: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.vueAppType, undefined),
|
21 | vueAppTypeImportFrom: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.vueAppTypeImportFrom, undefined),
|
22 | addDocBlocks: (0, visitor_plugin_common_1.getConfigValue)(rawConfig.addDocBlocks, true),
|
23 | });
|
24 | this.imports = new Set();
|
25 | this.externalImportPrefix = this.config.importOperationTypesFrom ? `${this.config.importOperationTypesFrom}.` : '';
|
26 | this._documents = documents;
|
27 | (0, auto_bind_1.default)(this);
|
28 | }
|
29 | get vueApolloOperationFunctionsImport() {
|
30 | return `import { createMutationFunction, createSmartQueryOptionsFunction, createSmartSubscriptionOptionsFunction } from '${this.config.vueApolloOperationFunctionsImportFrom}';`;
|
31 | }
|
32 | get vueApolloErrorTypeImport() {
|
33 | return `import { ${this.config.vueApolloErrorType} } from '${this.config.vueApolloErrorTypeImportFrom}';`;
|
34 | }
|
35 | get vueApolloErrorHandlerFunctionImport() {
|
36 | if (!this.config.vueApolloErrorHandlerFunction || !this.config.vueApolloErrorHandlerFunctionImportFrom) {
|
37 | return '';
|
38 | }
|
39 | return `import { ${this.config.vueApolloErrorHandlerFunction} } from '${this.config.vueApolloErrorHandlerFunctionImportFrom}';`;
|
40 | }
|
41 | get vueAppTypeImport() {
|
42 | if (!this.config.vueAppType || !this.config.vueAppTypeImportFrom) {
|
43 | return '';
|
44 | }
|
45 | return `import { ${this.config.vueAppType} } from '${this.config.vueAppTypeImportFrom}';`;
|
46 | }
|
47 | getDocumentNodeVariable(node, documentVariableName) {
|
48 | var _a;
|
49 | return this.config.documentMode === visitor_plugin_common_1.DocumentMode.external ? `Operations.${(_a = node.name) === null || _a === void 0 ? void 0 : _a.value}` : documentVariableName;
|
50 | }
|
51 | getImports() {
|
52 | const baseImports = super.getImports();
|
53 | const hasOperations = this._collectedOperations.length > 0;
|
54 | if (!hasOperations) {
|
55 | return baseImports;
|
56 | }
|
57 | return [...baseImports, ...Array.from(this.imports)];
|
58 | }
|
59 | buildOperationFunctionsJSDoc(node, operationName, operationType) {
|
60 | var _a;
|
61 | const operationFunctionName = operationType === 'Mutation' ? (0, change_case_all_1.camelCase)(operationName) : `use${operationName}`;
|
62 | const operationNameWithoutSuffix = (0, change_case_all_1.camelCase)(operationName).replace(/(Query|Mutation|Subscription)$/, '');
|
63 | const exampleVariables = ((_a = node.variableDefinitions) !== null && _a !== void 0 ? _a : []).map(variableDefinition => {
|
64 | const name = variableDefinition.variable.name.value;
|
65 | return `${name}: // value for '${name}'`;
|
66 | });
|
67 | switch (operationType) {
|
68 | case 'Query':
|
69 | return `
|
70 | /**
|
71 | * __${operationFunctionName}__
|
72 | *
|
73 | * To use a Smart Query within a Vue component, call \`${operationFunctionName}\` as the value for a query key
|
74 | * in the component's \`apollo\` config, passing any options required for the query.
|
75 | *
|
76 | * @param options that will be passed into the query, supported options are listed on: https://www.apollographql.com/docs/react/api/core/ApolloClient/#ApolloClient.query
|
77 | *
|
78 | * @example
|
79 | * {
|
80 | * apollo: {
|
81 | * ${operationNameWithoutSuffix}: ${operationFunctionName}({
|
82 | * variables: {${exampleVariables.length > 0
|
83 | ? `
|
84 | * ${exampleVariables.join(`
|
85 | * `)}
|
86 | * `
|
87 | : ''}},
|
88 | * loadingKey: 'loading',
|
89 | * fetchPolicy: 'no-cache',
|
90 | * }),
|
91 | * }
|
92 | * }
|
93 | */`;
|
94 | case 'Mutation':
|
95 | return `
|
96 | /**
|
97 | * __${operationFunctionName}__
|
98 | *
|
99 | * To run a mutation, you call \`${operationFunctionName}\` within a Vue component and pass it
|
100 | * your Vue app instance along with any options that fit your needs.
|
101 | *
|
102 | * @param app, a reference to your Vue app instance (which must have a \`$apollo\` property)
|
103 | * @param options that will be passed into the mutation, supported options are listed on: https://www.apollographql.com/docs/react/api/core/ApolloClient/#ApolloClient.mutate
|
104 | * @param client (optional), which can be an instance of \`DollarApollo\` or the \`mutate()\` function provided by an \`<ApolloMutation>\` component
|
105 | *
|
106 | * @example
|
107 | * const { success, data, errors } = ${operationFunctionName}(this, {
|
108 | * variables: {${exampleVariables.length > 0
|
109 | ? `
|
110 | * ${exampleVariables.join(`
|
111 | * `)}
|
112 | * `
|
113 | : ''}},
|
114 | * });
|
115 | */`;
|
116 | case 'Subscription':
|
117 | return `
|
118 | /**
|
119 | * __${operationFunctionName}__
|
120 | *
|
121 | * To use a Smart Subscription within a Vue component, call \`${operationFunctionName}\` as the value for a \`$subscribe\` key
|
122 | * in the component's \`apollo\` config, passing any options required for the subscription.
|
123 | *
|
124 | * @param options that will be passed into the subscription, supported options are listed on: https://www.apollographql.com/docs/react/api/core/ApolloClient/#ApolloClient.subscribe
|
125 | *
|
126 | * @example
|
127 | * {
|
128 | * apollo: {
|
129 | * $subscribe: {
|
130 | * ${operationNameWithoutSuffix}: ${operationFunctionName}({
|
131 | * variables: {${exampleVariables.length > 0
|
132 | ? `
|
133 | * ${exampleVariables.join(`
|
134 | * `)}
|
135 | * `
|
136 | : ''}},
|
137 | * loadingKey: 'loading',
|
138 | * fetchPolicy: 'no-cache',
|
139 | * }),
|
140 | * },
|
141 | * }
|
142 | * }
|
143 | */`;
|
144 | }
|
145 | }
|
146 | getOperationFunctionSuffix(name, operationType) {
|
147 | if (!this.config.dedupeOperationSuffix) {
|
148 | return this.config.omitOperationSuffix ? '' : (0, change_case_all_1.pascalCase)(operationType);
|
149 | }
|
150 | if (name.includes('Query') || name.includes('Mutation') || name.includes('Subscription')) {
|
151 | return '';
|
152 | }
|
153 | return (0, change_case_all_1.pascalCase)(operationType);
|
154 | }
|
155 | buildOperation(node, documentVariableName, operationType, operationResultType, operationVariablesTypes) {
|
156 | var _a, _b, _c;
|
157 | operationResultType = this.externalImportPrefix + operationResultType;
|
158 | operationVariablesTypes = this.externalImportPrefix + operationVariablesTypes;
|
159 | if (!this.config.withSmartOperationFunctions) {
|
160 |
|
161 | return '';
|
162 | }
|
163 | if (!((_a = node.name) === null || _a === void 0 ? void 0 : _a.value)) {
|
164 |
|
165 | return '';
|
166 | }
|
167 | const suffix = this.getOperationFunctionSuffix(node.name.value, operationType);
|
168 | const operationName = this.convertName(node.name.value, {
|
169 | suffix,
|
170 | useTypesPrefix: false,
|
171 | });
|
172 | const operationHasVariables = ((_b = node.variableDefinitions) !== null && _b !== void 0 ? _b : []).length > 0;
|
173 | const operationHasNonNullableVariable = !!((_c = node.variableDefinitions) === null || _c === void 0 ? void 0 : _c.some(({ type }) => type.kind === 'NonNullType'));
|
174 | this.imports.add(this.vueApolloOperationFunctionsImport);
|
175 | this.imports.add(this.vueApolloErrorTypeImport);
|
176 | if (this.vueApolloErrorHandlerFunctionImport) {
|
177 | this.imports.add(this.vueApolloErrorHandlerFunctionImport);
|
178 | }
|
179 | if (this.vueAppTypeImport) {
|
180 | this.imports.add(this.vueAppTypeImport);
|
181 | }
|
182 | const documentNodeVariable = this.getDocumentNodeVariable(node, documentVariableName);
|
183 | const operationFunction = this.buildOperationFunction({
|
184 | operationName,
|
185 | operationType,
|
186 | operationResultType,
|
187 | operationVariablesTypes,
|
188 | operationHasNonNullableVariable,
|
189 | operationHasVariables,
|
190 | documentNodeVariable,
|
191 | });
|
192 | return [
|
193 | ...insertIf(this.config.addDocBlocks, [this.buildOperationFunctionsJSDoc(node, operationName, operationType)]),
|
194 | operationFunction,
|
195 | '',
|
196 | ].join('\n');
|
197 | }
|
198 | buildOperationFunction({ operationName, operationType, operationResultType, operationVariablesTypes, documentNodeVariable, }) {
|
199 | const operationArguments = [documentNodeVariable];
|
200 | if (this.config.vueApolloErrorHandlerFunction) {
|
201 | operationArguments.push(this.config.vueApolloErrorHandlerFunction);
|
202 | }
|
203 | const genericTypeArguments = [
|
204 | operationResultType,
|
205 | operationVariablesTypes,
|
206 | this.config.vueApolloErrorType,
|
207 | ];
|
208 | if (this.config.vueAppType) {
|
209 | genericTypeArguments.push(this.config.vueAppType);
|
210 | }
|
211 | switch (operationType) {
|
212 | case 'Query': {
|
213 | return `export const use${operationName} = createSmartQueryOptionsFunction<
|
214 | ${genericTypeArguments.join(',\n ')}
|
215 | >(${operationArguments.join(', ')});`;
|
216 | }
|
217 | case 'Mutation': {
|
218 | return `export const ${(0, change_case_all_1.camelCase)(operationName)} = createMutationFunction<
|
219 | ${genericTypeArguments.join(',\n ')}
|
220 | >(${operationArguments.join(', ')});`;
|
221 | }
|
222 | case 'Subscription': {
|
223 | return `export const use${operationName} = createSmartSubscriptionOptionsFunction<
|
224 | ${genericTypeArguments.join(',\n ')}
|
225 | >(${operationArguments.join(', ')});`;
|
226 | }
|
227 | }
|
228 | }
|
229 | }
|
230 | exports.VueApolloVisitor = VueApolloVisitor;
|