Skip to content

Commit

Permalink
fix(plugin-helpers): remove unnecessary import (#6555)
Browse files Browse the repository at this point in the history
* fix(plugin-helpers): remove unnecessary import

* Add getRootTypeNames just in case
  • Loading branch information
ardatan authored Aug 24, 2021
1 parent 263570e commit 6470e6c
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 82 deletions.
6 changes: 6 additions & 0 deletions .changeset/early-roses-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-codegen/visitor-plugin-common': patch
'@graphql-codegen/plugin-helpers': patch
---

fix(plugin-helpers): remove unnecessary import
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 1 addition & 2 deletions packages/plugins/other/visitor-plugin-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
indent,
getBaseTypeNode,
getConfigValue,
getRootTypeNames,
stripMapperTypeInterpolation,
OMIT_TYPE,
REQUIRE_FIELDS_TYPE,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -349,8 +349,8 @@ export class BaseResolversVisitor<
protected _usedMappers: { [key: string]: boolean } = {};
protected _resolversTypes: ResolverTypes = {};
protected _resolversParentTypes: ResolverParentTypes = {};
protected _rootTypeNames: string[] = [];
protected _globalDeclarations: Set<string> = new Set();
protected _rootTypeNames = new Set<string>();
protected _globalDeclarations = new Set<string>();
protected _federation: ApolloFederation;
protected _hasScalars = false;
protected _hasFederation = false;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
}
Expand All @@ -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'));
}

Expand All @@ -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)}`);
Expand All @@ -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);

Expand All @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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'));
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}

Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}));
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -485,22 +486,15 @@ export class SelectionSetToObject<Config extends ParsedDocumentsConfig = ParsedD
return this._processor.buildSelectionSetFromStrings(fields);
}

protected isRootType(type: GraphQLObjectType): boolean {
const rootType = [this._schema.getQueryType(), this._schema.getMutationType(), this._schema.getSubscriptionType()]
.filter(Boolean)
.map(t => t.name);

return rootType.includes(type.name);
}

protected buildTypeNameField(
type: GraphQLObjectType,
nonOptionalTypename: boolean = this._config.nonOptionalTypename,
addTypename: boolean = this._config.addTypename,
queriedForTypename: boolean = this._queriedForTypename,
skipTypeNameForRoot: boolean = this._config.skipTypeNameForRoot
): { name: string; type: string } {
if (this.isRootType(type) && skipTypeNameForRoot && !queriedForTypename) {
const rootTypes = getRootTypes(this._schema);
if (rootTypes.has(type) && skipTypeNameForRoot && !queriedForTypename) {
return null;
}

Expand Down
10 changes: 1 addition & 9 deletions packages/plugins/other/visitor-plugin-common/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
GraphQLSchema,
GraphQLScalarType,
StringValueNode,
isEqualType,
SelectionSetNode,
FieldNode,
SelectionNode,
Expand Down Expand Up @@ -340,14 +339,7 @@ function isStringValueNode(node: any): node is StringValueNode {
return node && typeof node === 'object' && node.kind === Kind.STRING;
}

export function isRootType(type: GraphQLNamedType, schema: GraphQLSchema): type is GraphQLObjectType {
return (
isEqualType(type, schema.getQueryType()) ||
isEqualType(type, schema.getMutationType()) ||
isEqualType(type, schema.getSubscriptionType())
);
}

// will be removed on next release because tools already has it
export function getRootTypeNames(schema: GraphQLSchema): string[] {
return [schema.getQueryType(), schema.getMutationType(), schema.getSubscriptionType()]
.filter(t => t)
Expand Down
5 changes: 2 additions & 3 deletions packages/utils/plugins-helpers/src/federation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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');

Expand Down
31 changes: 8 additions & 23 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2938,11 +2938,6 @@
dependencies:
"@types/node" "*"

"@types/[email protected]":
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"
Expand Down Expand Up @@ -3221,7 +3216,12 @@
resolved "https://registry.yarnpkg.com/@types/ms/-/ms-0.7.31.tgz#31b7ca6407128a3d2bbc27fe2d21b345397f6197"
integrity sha512-iiUgKzV9AuaEkZqkOLDIvlQiL6ltuZd9tGcW3gwpnX8JbuiuhFlEGmmFXEXkN50Cvq7Os88IY2v0dkDqXYWVgA==

"@types/node@*", "@types/[email protected]", "@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/[email protected]", "@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==
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"

[email protected], 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:
[email protected], 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==
Expand Down

0 comments on commit 6470e6c

Please sign in to comment.