diff --git a/.changeset/early-roses-flash.md b/.changeset/early-roses-flash.md new file mode 100644 index 00000000000..dadcbcb5ccf --- /dev/null +++ b/.changeset/early-roses-flash.md @@ -0,0 +1,6 @@ +--- +'@graphql-codegen/visitor-plugin-common': patch +'@graphql-codegen/plugin-helpers': patch +--- + +fix(plugin-helpers): remove unnecessary import diff --git a/package.json b/package.json index ff8ae1bbd35..a552002697f 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,6 @@ "name": "graphql-code-generator", "private": true, "scripts": { - "preinstall": "npx yarn-deduplicate", "postinstall": "patch-package && husky install", "clean": "rimraf node_modules packages/{*,plugins/*/*,presets/*,utils/*}/node_modules", "prebuild": "rimraf packages/{*,plugins/*/*,presets/*,utils/*}/dist", diff --git a/packages/plugins/other/visitor-plugin-common/package.json b/packages/plugins/other/visitor-plugin-common/package.json index dc6e14323b8..21826751d35 100644 --- a/packages/plugins/other/visitor-plugin-common/package.json +++ b/packages/plugins/other/visitor-plugin-common/package.json @@ -16,7 +16,7 @@ "@graphql-tools/optimize": "^1.0.1", "@graphql-codegen/plugin-helpers": "^2.1.0", "@graphql-tools/relay-operation-optimizer": "^6.3.7", - "array.prototype.flatmap": "^1.2.4", + "@graphql-tools/utils": "8.1.1", "auto-bind": "~4.0.0", "dependency-graph": "^0.11.0", "graphql-tag": "^2.11.0", @@ -28,7 +28,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" }, "devDependencies": { - "@types/array.prototype.flatmap": "1.2.2", "@types/parse-filepath": "1.0.0" }, "main": "dist/index.js", diff --git a/packages/plugins/other/visitor-plugin-common/src/base-resolvers-visitor.ts b/packages/plugins/other/visitor-plugin-common/src/base-resolvers-visitor.ts index d4307a58d38..16bf7f2c1c7 100644 --- a/packages/plugins/other/visitor-plugin-common/src/base-resolvers-visitor.ts +++ b/packages/plugins/other/visitor-plugin-common/src/base-resolvers-visitor.ts @@ -15,7 +15,6 @@ import { indent, getBaseTypeNode, getConfigValue, - getRootTypeNames, stripMapperTypeInterpolation, OMIT_TYPE, REQUIRE_FIELDS_TYPE, @@ -50,6 +49,7 @@ import { OperationVariablesToObject } from './variables-to-object'; import { ParsedMapper, parseMapper, transformMappers, ExternalParsedMapper, buildMapperImport } from './mappers'; import { parseEnumValues } from './enum-values'; import { ApolloFederation, getBaseType } from '@graphql-codegen/plugin-helpers'; +import { getRootTypeNames } from '@graphql-tools/utils'; export interface ParsedResolversConfig extends ParsedConfig { contextType: ParsedMapper; @@ -349,8 +349,8 @@ export class BaseResolversVisitor< protected _usedMappers: { [key: string]: boolean } = {}; protected _resolversTypes: ResolverTypes = {}; protected _resolversParentTypes: ResolverParentTypes = {}; - protected _rootTypeNames: string[] = []; - protected _globalDeclarations: Set = new Set(); + protected _rootTypeNames = new Set(); + protected _globalDeclarations = new Set(); protected _federation: ApolloFederation; protected _hasScalars = false; protected _hasFederation = false; @@ -501,7 +501,7 @@ export class BaseResolversVisitor< } let shouldApplyOmit = false; - const isRootType = this._rootTypeNames.includes(typeName); + const isRootType = this._rootTypeNames.has(typeName); const isMapped = this.config.mappers[typeName]; const isScalar = this.config.scalars[typeName]; const hasDefaultMapper = !!(this.config.defaultMapper && this.config.defaultMapper.type); diff --git a/packages/plugins/other/visitor-plugin-common/src/base-types-visitor.ts b/packages/plugins/other/visitor-plugin-common/src/base-types-visitor.ts index d630d383a1d..02677bc19b0 100644 --- a/packages/plugins/other/visitor-plugin-common/src/base-types-visitor.ts +++ b/packages/plugins/other/visitor-plugin-common/src/base-types-visitor.ts @@ -20,7 +20,6 @@ import { Kind, GraphQLEnumType, } from 'graphql'; -import flatMap from 'array.prototype.flatmap'; import { BaseVisitor, ParsedConfig, RawConfig } from './base-visitor'; import { DEFAULT_SCALARS } from './scalars'; import { normalizeDeclarationKind } from './declaration-kinds'; @@ -329,7 +328,7 @@ export class BaseTypesVisitor< } NonNullType(node: NonNullTypeNode): string { - const asString = (node.type as any) as string; + const asString = node.type as any as string; return asString; } @@ -339,7 +338,7 @@ export class BaseTypesVisitor< .export() .asKind(this._parsedConfig.declarationKind.input) .withName(this.convertName(node)) - .withComment((node.description as any) as string) + .withComment(node.description as any as string) .withBlock(node.fields.join('\n')); } @@ -348,7 +347,7 @@ export class BaseTypesVisitor< } InputValueDefinition(node: InputValueDefinitionNode): string { - const comment = transformComment((node.description as any) as string, 1); + const comment = transformComment(node.description as any as string, 1); const { input } = this._parsedConfig.declarationKind; return comment + indent(`${node.name}: ${node.type}${this.getPunctuation(input)}`); @@ -359,7 +358,7 @@ export class BaseTypesVisitor< } FieldDefinition(node: FieldDefinitionNode): string { - const typeString = (node.type as any) as string; + const typeString = node.type as any as string; const { type } = this._parsedConfig.declarationKind; const comment = this.getFieldComment(node); @@ -377,7 +376,7 @@ export class BaseTypesVisitor< .export() .asKind('type') .withName(this.convertName(node)) - .withComment((node.description as any) as string) + .withComment(node.description as any as string) .withContent(possibleTypes).string; } @@ -414,7 +413,7 @@ export class BaseTypesVisitor< .export() .asKind(type) .withName(this.convertName(node)) - .withComment((node.description as any) as string); + .withComment(node.description as any as string); if (type === 'interface' || type === 'class') { if (interfacesNames.length > 0) { @@ -463,7 +462,7 @@ export class BaseTypesVisitor< .export() .asKind(this._parsedConfig.declarationKind.interface) .withName(this.convertName(node)) - .withComment((node.description as any) as string); + .withComment(node.description as any as string); return declarationBlock.withBlock(node.fields.join('\n')); } @@ -509,28 +508,30 @@ export class BaseTypesVisitor< } public getEnumsImports(): string[] { - return flatMap(Object.keys(this.config.enumValues), enumName => { - const mappedValue = this.config.enumValues[enumName]; - - if (mappedValue.sourceFile) { - if (mappedValue.isDefault) { - return [this._buildTypeImport(mappedValue.typeIdentifier, mappedValue.sourceFile, true)]; + return Object.keys(this.config.enumValues) + .flatMap(enumName => { + const mappedValue = this.config.enumValues[enumName]; + + if (mappedValue.sourceFile) { + if (mappedValue.isDefault) { + return [this._buildTypeImport(mappedValue.typeIdentifier, mappedValue.sourceFile, true)]; + } + + return this.handleEnumValueMapper( + mappedValue.typeIdentifier, + mappedValue.importIdentifier, + mappedValue.sourceIdentifier, + mappedValue.sourceFile + ); } - return this.handleEnumValueMapper( - mappedValue.typeIdentifier, - mappedValue.importIdentifier, - mappedValue.sourceIdentifier, - mappedValue.sourceFile - ); - } - - return []; - }).filter(a => a); + return []; + }) + .filter(Boolean); } EnumTypeDefinition(node: EnumTypeDefinitionNode): string { - const enumName = (node.name as any) as string; + const enumName = node.name as any as string; // In case of mapped external enum string if (this.config.enumValues[enumName] && this.config.enumValues[enumName].sourceFile) { @@ -541,7 +542,7 @@ export class BaseTypesVisitor< .export() .asKind('enum') .withName(this.convertName(node, { useTypesPrefix: this.config.enumPrefix })) - .withComment((node.description as any) as string) + .withComment(node.description as any as string) .withBlock(this.buildEnumValuesBlock(enumName, node.values)).string; } @@ -567,7 +568,7 @@ export class BaseTypesVisitor< const optionName = this.makeValidEnumIdentifier( this.convertName(enumOption, { useTypesPrefix: false, transformUnderscore: true }) ); - const comment = transformComment((enumOption.description as any) as string, 1); + const comment = transformComment(enumOption.description as any as string, 1); const schemaEnumValue = schemaEnumType && !this.config.ignoreEnumValuesFromSchema ? schemaEnumType.getValue(enumOption.name as any).value @@ -644,7 +645,7 @@ export class BaseTypesVisitor< } protected _getTypeForNode(node: NamedTypeNode): string { - const typeAsString = (node.name as any) as string; + const typeAsString = node.name as any as string; if (this.scalars[typeAsString]) { return this._getScalar(typeAsString); @@ -674,7 +675,7 @@ export class BaseTypesVisitor< } ListType(node: ListTypeNode): string { - const asString = (node.type as any) as string; + const asString = node.type as any as string; return this.wrapWithListType(asString); } diff --git a/packages/plugins/other/visitor-plugin-common/src/optimize-operations.ts b/packages/plugins/other/visitor-plugin-common/src/optimize-operations.ts index 0824e3d58e2..a54f22edcb9 100644 --- a/packages/plugins/other/visitor-plugin-common/src/optimize-operations.ts +++ b/packages/plugins/other/visitor-plugin-common/src/optimize-operations.ts @@ -13,8 +13,8 @@ export function optimizeOperations( options ); - return newDocuments.map(document => ({ - location: 'optimized by relay', + return newDocuments.map((document, index) => ({ + location: documents[index]?.location || 'optimized by relay', document, })); } diff --git a/packages/plugins/other/visitor-plugin-common/src/selection-set-to-object.ts b/packages/plugins/other/visitor-plugin-common/src/selection-set-to-object.ts index 32aeff80beb..00db53c56ce 100644 --- a/packages/plugins/other/visitor-plugin-common/src/selection-set-to-object.ts +++ b/packages/plugins/other/visitor-plugin-common/src/selection-set-to-object.ts @@ -40,6 +40,7 @@ import { NameAndType, } from './selection-set-processor/base'; import autoBind from 'auto-bind'; +import { getRootTypes } from '@graphql-tools/utils'; type FragmentSpreadUsage = { fragmentName: string; @@ -485,14 +486,6 @@ export class SelectionSetToObject t.name); - - return rootType.includes(type.name); - } - protected buildTypeNameField( type: GraphQLObjectType, nonOptionalTypename: boolean = this._config.nonOptionalTypename, @@ -500,7 +493,8 @@ export class SelectionSetToObject t) diff --git a/packages/utils/plugins-helpers/src/federation.ts b/packages/utils/plugins-helpers/src/federation.ts index 22d4a1d4a0f..2d9e4c65f20 100644 --- a/packages/utils/plugins-helpers/src/federation.ts +++ b/packages/utils/plugins-helpers/src/federation.ts @@ -14,8 +14,7 @@ import { } from 'graphql'; import merge from 'lodash/merge.js'; import { getBaseType } from './utils'; -import { MapperKind, mapSchema, astFromObjectType } from '@graphql-tools/utils'; -import { getRootTypeNames } from '@graphql-codegen/visitor-plugin-common'; +import { MapperKind, mapSchema, astFromObjectType, getRootTypeNames } from '@graphql-tools/utils'; /** * Federation Spec @@ -296,7 +295,7 @@ function isFederationObjectType(node: ObjectTypeDefinitionNode | GraphQLObjectTy } = isObjectType(node) ? astFromObjectType(node, schema) : node; const rootTypeNames = getRootTypeNames(schema); - const isNotRoot = !rootTypeNames.includes(name); + const isNotRoot = !rootTypeNames.has(name); const isNotIntrospection = !name.startsWith('__'); const hasKeyDirective = directives.some(d => d.name.value === 'key'); diff --git a/yarn.lock b/yarn.lock index 707a2c549c3..99bf597b984 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2938,11 +2938,6 @@ dependencies: "@types/node" "*" -"@types/array.prototype.flatmap@1.2.2": - version "1.2.2" - resolved "https://registry.yarnpkg.com/@types/array.prototype.flatmap/-/array.prototype.flatmap-1.2.2.tgz#9041c2dc907d583ffb80b8882a782b42436d57c1" - integrity sha512-dto5M/8GxPzjaScvQeft2IG0EkoZZfPg2+1noM2BWiU1VR2zsGHf76LonTOnLQKDuJlKDLzKaru4b+5Sci0Yhg== - "@types/babel__core@^7.0.0", "@types/babel__core@^7.1.14": version "7.1.15" resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.1.15.tgz#2ccfb1ad55a02c83f8e0ad327cbc332f55eb1024" @@ -3221,7 +3216,12 @@ resolved "https://registry.yarnpkg.com/@types/ms/-/ms-0.7.31.tgz#31b7ca6407128a3d2bbc27fe2d21b345397f6197" integrity sha512-iiUgKzV9AuaEkZqkOLDIvlQiL6ltuZd9tGcW3gwpnX8JbuiuhFlEGmmFXEXkN50Cvq7Os88IY2v0dkDqXYWVgA== -"@types/node@*", "@types/node@14.17.11", "@types/node@^14.14.33": +"@types/node@*", "@types/node@^16.4.10": + version "16.4.13" + resolved "https://registry.yarnpkg.com/@types/node/-/node-16.4.13.tgz#7dfd9c14661edc65cccd43a29eb454174642370d" + integrity sha512-bLL69sKtd25w7p1nvg9pigE4gtKVpGTPojBFLMkGHXuUgap2sLqQt2qUnqmVCDfzGUL0DRNZP+1prIZJbMeAXg== + +"@types/node@14.17.11", "@types/node@^14.14.33": version "14.17.11" resolved "https://registry.yarnpkg.com/@types/node/-/node-14.17.11.tgz#82d266d657aec5ff01ca59f2ffaff1bb43f7bf0f" integrity sha512-n2OQ+0Bz6WEsUjrvcHD1xZ8K+Kgo4cn9/w94s1bJS690QMUWfJPW/m7CCb7gPkA1fcYwL2UpjXP/rq/Eo41m6w== @@ -3241,11 +3241,6 @@ resolved "https://registry.yarnpkg.com/@types/node/-/node-15.14.7.tgz#29fea9a5b14e2b75c19028e1c7a32edd1e89fe92" integrity sha512-FA45p37/mLhpebgbPWWCKfOisTjxGK9lwcHlJ6XVLfu3NgfcazOJHdYUZCWPMK8QX4LhNZdmfo6iMz9FqpUbaw== -"@types/node@^16.4.10": - version "16.4.13" - resolved "https://registry.yarnpkg.com/@types/node/-/node-16.4.13.tgz#7dfd9c14661edc65cccd43a29eb454174642370d" - integrity sha512-bLL69sKtd25w7p1nvg9pigE4gtKVpGTPojBFLMkGHXuUgap2sLqQt2qUnqmVCDfzGUL0DRNZP+1prIZJbMeAXg== - "@types/normalize-package-data@^2.4.0": version "2.4.1" 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: define-properties "^1.1.3" es-abstract "^1.18.0-next.1" -array.prototype.flatmap@^1.2.4: - version "1.2.4" - resolved "https://registry.yarnpkg.com/array.prototype.flatmap/-/array.prototype.flatmap-1.2.4.tgz#94cfd47cc1556ec0747d97f7c7738c58122004c9" - integrity sha512-r9Z0zYoxqHz60vvQbWEdXIEtCwHF0yxaWfno9qzXeNHvfyl3BZqygmGzb84dsubyaXLH4husF+NFgMSdpZhk2Q== - dependencies: - call-bind "^1.0.0" - define-properties "^1.1.3" - es-abstract "^1.18.0-next.1" - function-bind "^1.1.1" - arrify@^1.0.1: version "1.0.1" 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: dependencies: tslib "^1.9.0" -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: +safe-buffer@5.1.2, safe-buffer@~5.1.0, safe-buffer@~5.1.1: version "5.1.2" resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d" integrity sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g== -safe-buffer@^5.2.0, safe-buffer@~5.2.0: +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: version "5.2.1" resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6" integrity sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==