@graphql-codegen/typescript-apollo-angular 3.4.4 → 3.4.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.js +27 -14
  2. package/index.mjs +28 -15
  3. package/package.json +2 -2
package/index.js CHANGED
@@ -203,6 +203,7 @@ class ApolloAngularVisitor extends visitorPluginCommon.ClientSideBaseVisitor {
203
203
  operationVariablesTypes,
204
204
  serviceName,
205
205
  });
206
+ const namedClient = this._namedClient(node);
206
207
  operationResultType = this._externalImportPrefix + operationResultType;
207
208
  operationVariablesTypes = this._externalImportPrefix + operationVariablesTypes;
208
209
  const content = `
@@ -211,7 +212,7 @@ class ApolloAngularVisitor extends visitorPluginCommon.ClientSideBaseVisitor {
211
212
  })
212
213
  export class ${serviceName} extends Apollo.${operationType}<${operationResultType}, ${operationVariablesTypes}> {
213
214
  ${this.config.addExplicitOverride ? 'override ' : ''}document = ${this._getDocumentNodeVariable(node, documentVariableName)};
214
- ${this._namedClient(node)}
215
+ ${namedClient !== '' ? (this.config.addExplicitOverride ? 'override ' : '') + namedClient : ''}
215
216
  constructor(${this.dependencyInjections}) {
216
217
  super(${this.dependencyInjectionArgs});
217
218
  }
@@ -229,6 +230,9 @@ class ApolloAngularVisitor extends visitorPluginCommon.ClientSideBaseVisitor {
229
230
  return 'fetch';
230
231
  }
231
232
  };
233
+ const hasMutations = this._operationsToInclude.find(o => o.operationType === 'Mutation');
234
+ const hasSubscriptions = this._operationsToInclude.find(o => o.operationType === 'Subscription');
235
+ const hasQueries = this._operationsToInclude.find(o => o.operationType === 'Query');
232
236
  const allPossibleActions = this._operationsToInclude
233
237
  .map(o => {
234
238
  const operationResultType = this._externalImportPrefix + o.operationResultType;
@@ -266,20 +270,29 @@ ${changeCaseAll.camelCase(o.node.name.value)}Watch(variables${optionalVariables
266
270
  : this.config.serviceProvidedInRoot === false
267
271
  ? ''
268
272
  : `{ providedIn: 'root' }`;
273
+ // Generate these types only if they're going to be used,
274
+ // to avoid "unused variable" compile errors in generated code
275
+ const omitType = hasQueries || hasMutations || hasSubscriptions
276
+ ? `type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;`
277
+ : '';
278
+ const watchType = hasQueries
279
+ ? `interface WatchQueryOptionsAlone<V> extends Omit<ApolloCore.WatchQueryOptions<V>, 'query' | 'variables'> {}`
280
+ : '';
281
+ const queryType = hasQueries
282
+ ? `interface QueryOptionsAlone<V> extends Omit<ApolloCore.QueryOptions<V>, 'query' | 'variables'> {}`
283
+ : '';
284
+ const mutationType = hasMutations
285
+ ? `interface MutationOptionsAlone<T, V> extends Omit<ApolloCore.MutationOptions<T, V>, 'mutation' | 'variables'> {}`
286
+ : '';
287
+ const subscriptionType = hasSubscriptions
288
+ ? `interface SubscriptionOptionsAlone<V> extends Omit<ApolloCore.SubscriptionOptions<V>, 'query' | 'variables'> {}`
289
+ : '';
290
+ const types = [omitType, watchType, queryType, mutationType, subscriptionType]
291
+ .filter(s => s)
292
+ .map(s => visitorPluginCommon.indent(s, 1))
293
+ .join('\n\n');
269
294
  return `
270
- type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
271
-
272
- interface WatchQueryOptionsAlone<V>
273
- extends Omit<ApolloCore.WatchQueryOptions<V>, 'query' | 'variables'> {}
274
-
275
- interface QueryOptionsAlone<V>
276
- extends Omit<ApolloCore.QueryOptions<V>, 'query' | 'variables'> {}
277
-
278
- interface MutationOptionsAlone<T, V>
279
- extends Omit<ApolloCore.MutationOptions<T, V>, 'mutation' | 'variables'> {}
280
-
281
- interface SubscriptionOptionsAlone<V>
282
- extends Omit<ApolloCore.SubscriptionOptions<V>, 'query' | 'variables'> {}
295
+ ${types}
283
296
 
284
297
  @Injectable(${providedIn})
285
298
  export class ${serviceName} {
package/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { oldVisit } from '@graphql-codegen/plugin-helpers';
2
2
  import { print, visit, Kind, concatAST, parse } from 'graphql';
3
- import { ClientSideBaseVisitor, getConfigValue, DocumentMode, indentMultiline } from '@graphql-codegen/visitor-plugin-common';
3
+ import { ClientSideBaseVisitor, getConfigValue, DocumentMode, indentMultiline, indent } from '@graphql-codegen/visitor-plugin-common';
4
4
  import autoBind from 'auto-bind';
5
5
  import { camelCase } from 'change-case-all';
6
6
  import { extname } from 'path';
@@ -197,6 +197,7 @@ class ApolloAngularVisitor extends ClientSideBaseVisitor {
197
197
  operationVariablesTypes,
198
198
  serviceName,
199
199
  });
200
+ const namedClient = this._namedClient(node);
200
201
  operationResultType = this._externalImportPrefix + operationResultType;
201
202
  operationVariablesTypes = this._externalImportPrefix + operationVariablesTypes;
202
203
  const content = `
@@ -205,7 +206,7 @@ class ApolloAngularVisitor extends ClientSideBaseVisitor {
205
206
  })
206
207
  export class ${serviceName} extends Apollo.${operationType}<${operationResultType}, ${operationVariablesTypes}> {
207
208
  ${this.config.addExplicitOverride ? 'override ' : ''}document = ${this._getDocumentNodeVariable(node, documentVariableName)};
208
- ${this._namedClient(node)}
209
+ ${namedClient !== '' ? (this.config.addExplicitOverride ? 'override ' : '') + namedClient : ''}
209
210
  constructor(${this.dependencyInjections}) {
210
211
  super(${this.dependencyInjectionArgs});
211
212
  }
@@ -223,6 +224,9 @@ class ApolloAngularVisitor extends ClientSideBaseVisitor {
223
224
  return 'fetch';
224
225
  }
225
226
  };
227
+ const hasMutations = this._operationsToInclude.find(o => o.operationType === 'Mutation');
228
+ const hasSubscriptions = this._operationsToInclude.find(o => o.operationType === 'Subscription');
229
+ const hasQueries = this._operationsToInclude.find(o => o.operationType === 'Query');
226
230
  const allPossibleActions = this._operationsToInclude
227
231
  .map(o => {
228
232
  const operationResultType = this._externalImportPrefix + o.operationResultType;
@@ -260,20 +264,29 @@ ${camelCase(o.node.name.value)}Watch(variables${optionalVariables ? '?' : ''}: $
260
264
  : this.config.serviceProvidedInRoot === false
261
265
  ? ''
262
266
  : `{ providedIn: 'root' }`;
267
+ // Generate these types only if they're going to be used,
268
+ // to avoid "unused variable" compile errors in generated code
269
+ const omitType = hasQueries || hasMutations || hasSubscriptions
270
+ ? `type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;`
271
+ : '';
272
+ const watchType = hasQueries
273
+ ? `interface WatchQueryOptionsAlone<V> extends Omit<ApolloCore.WatchQueryOptions<V>, 'query' | 'variables'> {}`
274
+ : '';
275
+ const queryType = hasQueries
276
+ ? `interface QueryOptionsAlone<V> extends Omit<ApolloCore.QueryOptions<V>, 'query' | 'variables'> {}`
277
+ : '';
278
+ const mutationType = hasMutations
279
+ ? `interface MutationOptionsAlone<T, V> extends Omit<ApolloCore.MutationOptions<T, V>, 'mutation' | 'variables'> {}`
280
+ : '';
281
+ const subscriptionType = hasSubscriptions
282
+ ? `interface SubscriptionOptionsAlone<V> extends Omit<ApolloCore.SubscriptionOptions<V>, 'query' | 'variables'> {}`
283
+ : '';
284
+ const types = [omitType, watchType, queryType, mutationType, subscriptionType]
285
+ .filter(s => s)
286
+ .map(s => indent(s, 1))
287
+ .join('\n\n');
263
288
  return `
264
- type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
265
-
266
- interface WatchQueryOptionsAlone<V>
267
- extends Omit<ApolloCore.WatchQueryOptions<V>, 'query' | 'variables'> {}
268
-
269
- interface QueryOptionsAlone<V>
270
- extends Omit<ApolloCore.QueryOptions<V>, 'query' | 'variables'> {}
271
-
272
- interface MutationOptionsAlone<T, V>
273
- extends Omit<ApolloCore.MutationOptions<T, V>, 'mutation' | 'variables'> {}
274
-
275
- interface SubscriptionOptionsAlone<V>
276
- extends Omit<ApolloCore.SubscriptionOptions<V>, 'query' | 'variables'> {}
289
+ ${types}
277
290
 
278
291
  @Injectable(${providedIn})
279
292
  export class ${serviceName} {
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@graphql-codegen/typescript-apollo-angular",
3
- "version": "3.4.4",
3
+ "version": "3.4.7",
4
4
  "description": "GraphQL Code Generator plugin for generating ready-to-use Angular Components based on GraphQL operations",
5
5
  "peerDependencies": {
6
6
  "graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
7
7
  },
8
8
  "dependencies": {
9
9
  "@graphql-codegen/plugin-helpers": "^2.4.0",
10
- "@graphql-codegen/visitor-plugin-common": "2.7.3",
10
+ "@graphql-codegen/visitor-plugin-common": "2.7.4",
11
11
  "auto-bind": "~4.0.0",
12
12
  "change-case-all": "1.0.14",
13
13
  "tslib": "~2.3.0"