Skip to content

Commit 6470e6c

Browse files
authored
fix(plugin-helpers): remove unnecessary import (#6555)
* fix(plugin-helpers): remove unnecessary import * Add getRootTypeNames just in case
1 parent 263570e commit 6470e6c

File tree

10 files changed

+57
-82
lines changed

10 files changed

+57
-82
lines changed

.changeset/early-roses-flash.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@graphql-codegen/visitor-plugin-common': patch
3+
'@graphql-codegen/plugin-helpers': patch
4+
---
5+
6+
fix(plugin-helpers): remove unnecessary import

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"name": "graphql-code-generator",
33
"private": true,
44
"scripts": {
5-
"preinstall": "npx yarn-deduplicate",
65
"postinstall": "patch-package && husky install",
76
"clean": "rimraf node_modules packages/{*,plugins/*/*,presets/*,utils/*}/node_modules",
87
"prebuild": "rimraf packages/{*,plugins/*/*,presets/*,utils/*}/dist",

packages/plugins/other/visitor-plugin-common/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"@graphql-tools/optimize": "^1.0.1",
1717
"@graphql-codegen/plugin-helpers": "^2.1.0",
1818
"@graphql-tools/relay-operation-optimizer": "^6.3.7",
19-
"array.prototype.flatmap": "^1.2.4",
19+
"@graphql-tools/utils": "8.1.1",
2020
"auto-bind": "~4.0.0",
2121
"dependency-graph": "^0.11.0",
2222
"graphql-tag": "^2.11.0",
@@ -28,7 +28,6 @@
2828
"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"
2929
},
3030
"devDependencies": {
31-
"@types/array.prototype.flatmap": "1.2.2",
3231
"@types/parse-filepath": "1.0.0"
3332
},
3433
"main": "dist/index.js",

packages/plugins/other/visitor-plugin-common/src/base-resolvers-visitor.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
indent,
1616
getBaseTypeNode,
1717
getConfigValue,
18-
getRootTypeNames,
1918
stripMapperTypeInterpolation,
2019
OMIT_TYPE,
2120
REQUIRE_FIELDS_TYPE,
@@ -50,6 +49,7 @@ import { OperationVariablesToObject } from './variables-to-object';
5049
import { ParsedMapper, parseMapper, transformMappers, ExternalParsedMapper, buildMapperImport } from './mappers';
5150
import { parseEnumValues } from './enum-values';
5251
import { ApolloFederation, getBaseType } from '@graphql-codegen/plugin-helpers';
52+
import { getRootTypeNames } from '@graphql-tools/utils';
5353

5454
export interface ParsedResolversConfig extends ParsedConfig {
5555
contextType: ParsedMapper;
@@ -349,8 +349,8 @@ export class BaseResolversVisitor<
349349
protected _usedMappers: { [key: string]: boolean } = {};
350350
protected _resolversTypes: ResolverTypes = {};
351351
protected _resolversParentTypes: ResolverParentTypes = {};
352-
protected _rootTypeNames: string[] = [];
353-
protected _globalDeclarations: Set<string> = new Set();
352+
protected _rootTypeNames = new Set<string>();
353+
protected _globalDeclarations = new Set<string>();
354354
protected _federation: ApolloFederation;
355355
protected _hasScalars = false;
356356
protected _hasFederation = false;
@@ -501,7 +501,7 @@ export class BaseResolversVisitor<
501501
}
502502

503503
let shouldApplyOmit = false;
504-
const isRootType = this._rootTypeNames.includes(typeName);
504+
const isRootType = this._rootTypeNames.has(typeName);
505505
const isMapped = this.config.mappers[typeName];
506506
const isScalar = this.config.scalars[typeName];
507507
const hasDefaultMapper = !!(this.config.defaultMapper && this.config.defaultMapper.type);

packages/plugins/other/visitor-plugin-common/src/base-types-visitor.ts

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {
2020
Kind,
2121
GraphQLEnumType,
2222
} from 'graphql';
23-
import flatMap from 'array.prototype.flatmap';
2423
import { BaseVisitor, ParsedConfig, RawConfig } from './base-visitor';
2524
import { DEFAULT_SCALARS } from './scalars';
2625
import { normalizeDeclarationKind } from './declaration-kinds';
@@ -329,7 +328,7 @@ export class BaseTypesVisitor<
329328
}
330329

331330
NonNullType(node: NonNullTypeNode): string {
332-
const asString = (node.type as any) as string;
331+
const asString = node.type as any as string;
333332

334333
return asString;
335334
}
@@ -339,7 +338,7 @@ export class BaseTypesVisitor<
339338
.export()
340339
.asKind(this._parsedConfig.declarationKind.input)
341340
.withName(this.convertName(node))
342-
.withComment((node.description as any) as string)
341+
.withComment(node.description as any as string)
343342
.withBlock(node.fields.join('\n'));
344343
}
345344

@@ -348,7 +347,7 @@ export class BaseTypesVisitor<
348347
}
349348

350349
InputValueDefinition(node: InputValueDefinitionNode): string {
351-
const comment = transformComment((node.description as any) as string, 1);
350+
const comment = transformComment(node.description as any as string, 1);
352351
const { input } = this._parsedConfig.declarationKind;
353352

354353
return comment + indent(`${node.name}: ${node.type}${this.getPunctuation(input)}`);
@@ -359,7 +358,7 @@ export class BaseTypesVisitor<
359358
}
360359

361360
FieldDefinition(node: FieldDefinitionNode): string {
362-
const typeString = (node.type as any) as string;
361+
const typeString = node.type as any as string;
363362
const { type } = this._parsedConfig.declarationKind;
364363
const comment = this.getFieldComment(node);
365364

@@ -377,7 +376,7 @@ export class BaseTypesVisitor<
377376
.export()
378377
.asKind('type')
379378
.withName(this.convertName(node))
380-
.withComment((node.description as any) as string)
379+
.withComment(node.description as any as string)
381380
.withContent(possibleTypes).string;
382381
}
383382

@@ -414,7 +413,7 @@ export class BaseTypesVisitor<
414413
.export()
415414
.asKind(type)
416415
.withName(this.convertName(node))
417-
.withComment((node.description as any) as string);
416+
.withComment(node.description as any as string);
418417

419418
if (type === 'interface' || type === 'class') {
420419
if (interfacesNames.length > 0) {
@@ -463,7 +462,7 @@ export class BaseTypesVisitor<
463462
.export()
464463
.asKind(this._parsedConfig.declarationKind.interface)
465464
.withName(this.convertName(node))
466-
.withComment((node.description as any) as string);
465+
.withComment(node.description as any as string);
467466

468467
return declarationBlock.withBlock(node.fields.join('\n'));
469468
}
@@ -509,28 +508,30 @@ export class BaseTypesVisitor<
509508
}
510509

511510
public getEnumsImports(): string[] {
512-
return flatMap(Object.keys(this.config.enumValues), enumName => {
513-
const mappedValue = this.config.enumValues[enumName];
514-
515-
if (mappedValue.sourceFile) {
516-
if (mappedValue.isDefault) {
517-
return [this._buildTypeImport(mappedValue.typeIdentifier, mappedValue.sourceFile, true)];
511+
return Object.keys(this.config.enumValues)
512+
.flatMap(enumName => {
513+
const mappedValue = this.config.enumValues[enumName];
514+
515+
if (mappedValue.sourceFile) {
516+
if (mappedValue.isDefault) {
517+
return [this._buildTypeImport(mappedValue.typeIdentifier, mappedValue.sourceFile, true)];
518+
}
519+
520+
return this.handleEnumValueMapper(
521+
mappedValue.typeIdentifier,
522+
mappedValue.importIdentifier,
523+
mappedValue.sourceIdentifier,
524+
mappedValue.sourceFile
525+
);
518526
}
519527

520-
return this.handleEnumValueMapper(
521-
mappedValue.typeIdentifier,
522-
mappedValue.importIdentifier,
523-
mappedValue.sourceIdentifier,
524-
mappedValue.sourceFile
525-
);
526-
}
527-
528-
return [];
529-
}).filter(a => a);
528+
return [];
529+
})
530+
.filter(Boolean);
530531
}
531532

532533
EnumTypeDefinition(node: EnumTypeDefinitionNode): string {
533-
const enumName = (node.name as any) as string;
534+
const enumName = node.name as any as string;
534535

535536
// In case of mapped external enum string
536537
if (this.config.enumValues[enumName] && this.config.enumValues[enumName].sourceFile) {
@@ -541,7 +542,7 @@ export class BaseTypesVisitor<
541542
.export()
542543
.asKind('enum')
543544
.withName(this.convertName(node, { useTypesPrefix: this.config.enumPrefix }))
544-
.withComment((node.description as any) as string)
545+
.withComment(node.description as any as string)
545546
.withBlock(this.buildEnumValuesBlock(enumName, node.values)).string;
546547
}
547548

@@ -567,7 +568,7 @@ export class BaseTypesVisitor<
567568
const optionName = this.makeValidEnumIdentifier(
568569
this.convertName(enumOption, { useTypesPrefix: false, transformUnderscore: true })
569570
);
570-
const comment = transformComment((enumOption.description as any) as string, 1);
571+
const comment = transformComment(enumOption.description as any as string, 1);
571572
const schemaEnumValue =
572573
schemaEnumType && !this.config.ignoreEnumValuesFromSchema
573574
? schemaEnumType.getValue(enumOption.name as any).value
@@ -644,7 +645,7 @@ export class BaseTypesVisitor<
644645
}
645646

646647
protected _getTypeForNode(node: NamedTypeNode): string {
647-
const typeAsString = (node.name as any) as string;
648+
const typeAsString = node.name as any as string;
648649

649650
if (this.scalars[typeAsString]) {
650651
return this._getScalar(typeAsString);
@@ -674,7 +675,7 @@ export class BaseTypesVisitor<
674675
}
675676

676677
ListType(node: ListTypeNode): string {
677-
const asString = (node.type as any) as string;
678+
const asString = node.type as any as string;
678679

679680
return this.wrapWithListType(asString);
680681
}

packages/plugins/other/visitor-plugin-common/src/optimize-operations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ export function optimizeOperations(
1313
options
1414
);
1515

16-
return newDocuments.map(document => ({
17-
location: 'optimized by relay',
16+
return newDocuments.map((document, index) => ({
17+
location: documents[index]?.location || 'optimized by relay',
1818
document,
1919
}));
2020
}

packages/plugins/other/visitor-plugin-common/src/selection-set-to-object.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
NameAndType,
4141
} from './selection-set-processor/base';
4242
import autoBind from 'auto-bind';
43+
import { getRootTypes } from '@graphql-tools/utils';
4344

4445
type FragmentSpreadUsage = {
4546
fragmentName: string;
@@ -485,22 +486,15 @@ export class SelectionSetToObject<Config extends ParsedDocumentsConfig = ParsedD
485486
return this._processor.buildSelectionSetFromStrings(fields);
486487
}
487488

488-
protected isRootType(type: GraphQLObjectType): boolean {
489-
const rootType = [this._schema.getQueryType(), this._schema.getMutationType(), this._schema.getSubscriptionType()]
490-
.filter(Boolean)
491-
.map(t => t.name);
492-
493-
return rootType.includes(type.name);
494-
}
495-
496489
protected buildTypeNameField(
497490
type: GraphQLObjectType,
498491
nonOptionalTypename: boolean = this._config.nonOptionalTypename,
499492
addTypename: boolean = this._config.addTypename,
500493
queriedForTypename: boolean = this._queriedForTypename,
501494
skipTypeNameForRoot: boolean = this._config.skipTypeNameForRoot
502495
): { name: string; type: string } {
503-
if (this.isRootType(type) && skipTypeNameForRoot && !queriedForTypename) {
496+
const rootTypes = getRootTypes(this._schema);
497+
if (rootTypes.has(type) && skipTypeNameForRoot && !queriedForTypename) {
504498
return null;
505499
}
506500

packages/plugins/other/visitor-plugin-common/src/utils.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
GraphQLSchema,
1010
GraphQLScalarType,
1111
StringValueNode,
12-
isEqualType,
1312
SelectionSetNode,
1413
FieldNode,
1514
SelectionNode,
@@ -340,14 +339,7 @@ function isStringValueNode(node: any): node is StringValueNode {
340339
return node && typeof node === 'object' && node.kind === Kind.STRING;
341340
}
342341

343-
export function isRootType(type: GraphQLNamedType, schema: GraphQLSchema): type is GraphQLObjectType {
344-
return (
345-
isEqualType(type, schema.getQueryType()) ||
346-
isEqualType(type, schema.getMutationType()) ||
347-
isEqualType(type, schema.getSubscriptionType())
348-
);
349-
}
350-
342+
// will be removed on next release because tools already has it
351343
export function getRootTypeNames(schema: GraphQLSchema): string[] {
352344
return [schema.getQueryType(), schema.getMutationType(), schema.getSubscriptionType()]
353345
.filter(t => t)

packages/utils/plugins-helpers/src/federation.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ import {
1414
} from 'graphql';
1515
import merge from 'lodash/merge.js';
1616
import { getBaseType } from './utils';
17-
import { MapperKind, mapSchema, astFromObjectType } from '@graphql-tools/utils';
18-
import { getRootTypeNames } from '@graphql-codegen/visitor-plugin-common';
17+
import { MapperKind, mapSchema, astFromObjectType, getRootTypeNames } from '@graphql-tools/utils';
1918

2019
/**
2120
* Federation Spec
@@ -296,7 +295,7 @@ function isFederationObjectType(node: ObjectTypeDefinitionNode | GraphQLObjectTy
296295
} = isObjectType(node) ? astFromObjectType(node, schema) : node;
297296

298297
const rootTypeNames = getRootTypeNames(schema);
299-
const isNotRoot = !rootTypeNames.includes(name);
298+
const isNotRoot = !rootTypeNames.has(name);
300299
const isNotIntrospection = !name.startsWith('__');
301300
const hasKeyDirective = directives.some(d => d.name.value === 'key');
302301

yarn.lock

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2938,11 +2938,6 @@
29382938
dependencies:
29392939
"@types/node" "*"
29402940

2941-
"@types/array.prototype.flatmap@1.2.2":
2942-
version "1.2.2"
2943-
resolved "https://registry.yarnpkg.com/@types/array.prototype.flatmap/-/array.prototype.flatmap-1.2.2.tgz#9041c2dc907d583ffb80b8882a782b42436d57c1"
2944-
integrity sha512-dto5M/8GxPzjaScvQeft2IG0EkoZZfPg2+1noM2BWiU1VR2zsGHf76LonTOnLQKDuJlKDLzKaru4b+5Sci0Yhg==
2945-
29462941
"@types/babel__core@^7.0.0", "@types/babel__core@^7.1.14":
29472942
version "7.1.15"
29482943
resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.1.15.tgz#2ccfb1ad55a02c83f8e0ad327cbc332f55eb1024"
@@ -3221,7 +3216,12 @@
32213216
resolved "https://registry.yarnpkg.com/@types/ms/-/ms-0.7.31.tgz#31b7ca6407128a3d2bbc27fe2d21b345397f6197"
32223217
integrity sha512-iiUgKzV9AuaEkZqkOLDIvlQiL6ltuZd9tGcW3gwpnX8JbuiuhFlEGmmFXEXkN50Cvq7Os88IY2v0dkDqXYWVgA==
32233218

3224-
"@types/node@*", "@types/node@14.17.11", "@types/node@^14.14.33":
3219+
"@types/node@*", "@types/node@^16.4.10":
3220+
version "16.4.13"
3221+
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.4.13.tgz#7dfd9c14661edc65cccd43a29eb454174642370d"
3222+
integrity sha512-bLL69sKtd25w7p1nvg9pigE4gtKVpGTPojBFLMkGHXuUgap2sLqQt2qUnqmVCDfzGUL0DRNZP+1prIZJbMeAXg==
3223+
3224+
"@types/node@14.17.11", "@types/node@^14.14.33":
32253225
version "14.17.11"
32263226
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.17.11.tgz#82d266d657aec5ff01ca59f2ffaff1bb43f7bf0f"
32273227
integrity sha512-n2OQ+0Bz6WEsUjrvcHD1xZ8K+Kgo4cn9/w94s1bJS690QMUWfJPW/m7CCb7gPkA1fcYwL2UpjXP/rq/Eo41m6w==
@@ -3241,11 +3241,6 @@
32413241
resolved "https://registry.yarnpkg.com/@types/node/-/node-15.14.7.tgz#29fea9a5b14e2b75c19028e1c7a32edd1e89fe92"
32423242
integrity sha512-FA45p37/mLhpebgbPWWCKfOisTjxGK9lwcHlJ6XVLfu3NgfcazOJHdYUZCWPMK8QX4LhNZdmfo6iMz9FqpUbaw==
32433243

3244-
"@types/node@^16.4.10":
3245-
version "16.4.13"
3246-
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.4.13.tgz#7dfd9c14661edc65cccd43a29eb454174642370d"
3247-
integrity sha512-bLL69sKtd25w7p1nvg9pigE4gtKVpGTPojBFLMkGHXuUgap2sLqQt2qUnqmVCDfzGUL0DRNZP+1prIZJbMeAXg==
3248-
32493244
"@types/normalize-package-data@^2.4.0":
32503245
version "2.4.1"
32513246
resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.1.tgz#d3357479a0fdfdd5907fe67e17e0a85c906e1301"
@@ -4170,16 +4165,6 @@ array.prototype.flat@^1.2.4:
41704165
define-properties "^1.1.3"
41714166
es-abstract "^1.18.0-next.1"
41724167

4173-
array.prototype.flatmap@^1.2.4:
4174-
version "1.2.4"
4175-
resolved "https://registry.yarnpkg.com/array.prototype.flatmap/-/array.prototype.flatmap-1.2.4.tgz#94cfd47cc1556ec0747d97f7c7738c58122004c9"
4176-
integrity sha512-r9Z0zYoxqHz60vvQbWEdXIEtCwHF0yxaWfno9qzXeNHvfyl3BZqygmGzb84dsubyaXLH4husF+NFgMSdpZhk2Q==
4177-
dependencies:
4178-
call-bind "^1.0.0"
4179-
define-properties "^1.1.3"
4180-
es-abstract "^1.18.0-next.1"
4181-
function-bind "^1.1.1"
4182-
41834168
arrify@^1.0.1:
41844169
version "1.0.1"
41854170
resolved "https://registry.yarnpkg.com/arrify/-/arrify-1.0.1.tgz#898508da2226f380df904728456849c1501a4b0d"
@@ -13518,12 +13503,12 @@ rxjs@^6.3.3, rxjs@^6.4.0, rxjs@^6.5.1, rxjs@^6.6.0, rxjs@^6.6.3, rxjs@^6.6.7:
1351813503
dependencies:
1351913504
tslib "^1.9.0"
1352013505

13521-
safe-buffer@5.1.2, safe-buffer@>=5.1.0, safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.1, safe-buffer@^5.1.2, safe-buffer@~5.1.0, safe-buffer@~5.1.1:
13506+
safe-buffer@5.1.2, safe-buffer@~5.1.0, safe-buffer@~5.1.1:
1352213507
version "5.1.2"
1352313508
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d"
1352413509
integrity sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==
1352513510

13526-
safe-buffer@^5.2.0, safe-buffer@~5.2.0:
13511+
safe-buffer@>=5.1.0, safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.1, safe-buffer@^5.1.2, safe-buffer@^5.2.0, safe-buffer@~5.2.0:
1352713512
version "5.2.1"
1352813513
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6"
1352913514
integrity sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==

0 commit comments

Comments
 (0)