@graphql-codegen/visitor-plugin-common
Advanced tools
Comparing version 1.0.1-alpha-1f0a571f.15 to 1.0.1-alpha-48c9ae6d.16
@@ -0,6 +1,6 @@ | ||
import { DirectiveDefinitionNode, EnumTypeDefinitionNode, EnumValueDefinitionNode, FieldDefinitionNode, GraphQLSchema, InputObjectTypeDefinitionNode, InputValueDefinitionNode, InterfaceTypeDefinitionNode, ListTypeNode, NamedTypeNode, NameNode, NonNullTypeNode, ObjectTypeDefinitionNode, ScalarTypeDefinitionNode, UnionTypeDefinitionNode } from 'graphql'; | ||
import { BaseVisitor, ParsedConfig, RawConfig } from './base-visitor'; | ||
import { EnumValuesMap, ScalarsMap } from './types'; | ||
import { DeclarationBlockConfig } from './utils'; | ||
import { OperationVariablesToObject } from './variables-to-object'; | ||
import { DeclarationBlockConfig } from './utils'; | ||
import { InputObjectTypeDefinitionNode, InputValueDefinitionNode, NameNode, FieldDefinitionNode, ObjectTypeDefinitionNode, EnumTypeDefinitionNode, DirectiveDefinitionNode, ListTypeNode, GraphQLSchema, NonNullTypeNode, UnionTypeDefinitionNode, InterfaceTypeDefinitionNode, ScalarTypeDefinitionNode, EnumValueDefinitionNode, NamedTypeNode } from 'graphql'; | ||
export interface ParsedTypesConfig extends ParsedConfig { | ||
@@ -14,4 +14,5 @@ enumValues: EnumValuesMap; | ||
* @description Overrides the default value of enum values declared in your GraphQL schema. | ||
* You can also map the entire enum to an external type by providing a string that of `module#type`. | ||
* | ||
* @example | ||
* @example With Custom Values | ||
* ```yml | ||
@@ -23,2 +24,9 @@ * config: | ||
* ``` | ||
* | ||
* @example With External Enum | ||
* ```yml | ||
* config: | ||
* enumValues: | ||
* MyEnum: ./my-file#MyCustomEnum | ||
* ``` | ||
*/ | ||
@@ -43,4 +51,6 @@ enumValues?: EnumValuesMap; | ||
ScalarTypeDefinition(node: ScalarTypeDefinitionNode): string; | ||
protected _buildEnumImport(identifier: string, source: string): string; | ||
getEnumsImports(): string; | ||
EnumTypeDefinition(node: EnumTypeDefinitionNode): string; | ||
protected buildEnumValuesBlock(values: ReadonlyArray<EnumValueDefinitionNode>): string; | ||
protected buildEnumValuesBlock(typeName: string, values: ReadonlyArray<EnumValueDefinitionNode>): string; | ||
DirectiveDefinition(node: DirectiveDefinitionNode): string; | ||
@@ -47,0 +57,0 @@ protected _getScalar(name: string): string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const base_visitor_1 = require("./base-visitor"); | ||
const mappers_1 = require("./mappers"); | ||
const scalars_1 = require("./scalars"); | ||
const utils_1 = require("./utils"); | ||
const variables_to_object_1 = require("./variables-to-object"); | ||
const utils_1 = require("./utils"); | ||
const scalars_1 = require("./scalars"); | ||
class BaseTypesVisitor extends base_visitor_1.BaseVisitor { | ||
@@ -96,3 +97,27 @@ constructor(_schema, rawConfig, additionalConfig, defaultScalars = scalars_1.DEFAULT_SCALARS) { | ||
} | ||
_buildEnumImport(identifier, source) { | ||
return `import { ${identifier} } from '${source}';`; | ||
} | ||
getEnumsImports() { | ||
return Object.keys(this.config.enumValues) | ||
.map(enumName => { | ||
const mappedValue = this.config.enumValues[enumName]; | ||
if (mappedValue && typeof mappedValue === 'string') { | ||
const mapper = mappers_1.parseMapper(mappedValue); | ||
if (mapper.isExternal) { | ||
const identifier = mapper.type === enumName ? enumName : `${mapper.type} as ${enumName}`; | ||
return this._buildEnumImport(identifier, mapper.source); | ||
} | ||
} | ||
return null; | ||
}) | ||
.filter(a => a) | ||
.join('\n'); | ||
} | ||
EnumTypeDefinition(node) { | ||
const enumName = node.name; | ||
// In case of mapped external enum string | ||
if (this.config.enumValues[enumName] && typeof this.config.enumValues[enumName] === 'string') { | ||
return null; | ||
} | ||
return new utils_1.DeclarationBlock(this._declarationBlockConfig) | ||
@@ -102,6 +127,15 @@ .export() | ||
.withName(this.convertName(node)) | ||
.withBlock(this.buildEnumValuesBlock(node.values)).string; | ||
.withBlock(this.buildEnumValuesBlock(enumName, node.values)).string; | ||
} | ||
buildEnumValuesBlock(values) { | ||
return values.map(enumOption => utils_1.indent(`${this.convertName(enumOption)}${this._declarationBlockConfig.enumNameValueSeparator} ${utils_1.wrapWithSingleQuotes(this.config.enumValues[enumOption.name] || enumOption.name)}`)).join(',\n'); | ||
buildEnumValuesBlock(typeName, values) { | ||
return values | ||
.map(enumOption => { | ||
const optionName = this.convertName(enumOption); | ||
let enumValue = enumOption.name; | ||
if (this.config.enumValues[typeName] && typeof this.config.enumValues[typeName] === 'object' && this.config.enumValues[typeName][enumValue]) { | ||
enumValue = this.config.enumValues[typeName][enumValue]; | ||
} | ||
return utils_1.indent(`${optionName}${this._declarationBlockConfig.enumNameValueSeparator} ${utils_1.wrapWithSingleQuotes(enumValue)}`); | ||
}) | ||
.join(',\n'); | ||
} | ||
@@ -108,0 +142,0 @@ DirectiveDefinition(node) { |
@@ -9,3 +9,3 @@ "use strict"; | ||
source, | ||
type | ||
type, | ||
}; | ||
@@ -15,3 +15,3 @@ } | ||
isExternal: false, | ||
type: mapper | ||
type: mapper, | ||
}; | ||
@@ -18,0 +18,0 @@ } |
@@ -6,3 +6,5 @@ import { ASTNode } from 'graphql'; | ||
export declare type EnumValuesMap = { | ||
[key: string]: string; | ||
[enumName: string]: string | { | ||
[key: string]: string; | ||
}; | ||
}; | ||
@@ -9,0 +11,0 @@ export declare type ConvertNameFn<T = {}> = ConvertFn<T>; |
@@ -0,6 +1,6 @@ | ||
import { DirectiveDefinitionNode, EnumTypeDefinitionNode, EnumValueDefinitionNode, FieldDefinitionNode, GraphQLSchema, InputObjectTypeDefinitionNode, InputValueDefinitionNode, InterfaceTypeDefinitionNode, ListTypeNode, NamedTypeNode, NameNode, NonNullTypeNode, ObjectTypeDefinitionNode, ScalarTypeDefinitionNode, UnionTypeDefinitionNode } from 'graphql'; | ||
import { BaseVisitor, ParsedConfig, RawConfig } from './base-visitor'; | ||
import { EnumValuesMap, ScalarsMap } from './types'; | ||
import { DeclarationBlockConfig } from './utils'; | ||
import { OperationVariablesToObject } from './variables-to-object'; | ||
import { DeclarationBlockConfig } from './utils'; | ||
import { InputObjectTypeDefinitionNode, InputValueDefinitionNode, NameNode, FieldDefinitionNode, ObjectTypeDefinitionNode, EnumTypeDefinitionNode, DirectiveDefinitionNode, ListTypeNode, GraphQLSchema, NonNullTypeNode, UnionTypeDefinitionNode, InterfaceTypeDefinitionNode, ScalarTypeDefinitionNode, EnumValueDefinitionNode, NamedTypeNode } from 'graphql'; | ||
export interface ParsedTypesConfig extends ParsedConfig { | ||
@@ -14,4 +14,5 @@ enumValues: EnumValuesMap; | ||
* @description Overrides the default value of enum values declared in your GraphQL schema. | ||
* You can also map the entire enum to an external type by providing a string that of `module#type`. | ||
* | ||
* @example | ||
* @example With Custom Values | ||
* ```yml | ||
@@ -23,2 +24,9 @@ * config: | ||
* ``` | ||
* | ||
* @example With External Enum | ||
* ```yml | ||
* config: | ||
* enumValues: | ||
* MyEnum: ./my-file#MyCustomEnum | ||
* ``` | ||
*/ | ||
@@ -43,4 +51,6 @@ enumValues?: EnumValuesMap; | ||
ScalarTypeDefinition(node: ScalarTypeDefinitionNode): string; | ||
protected _buildEnumImport(identifier: string, source: string): string; | ||
getEnumsImports(): string; | ||
EnumTypeDefinition(node: EnumTypeDefinitionNode): string; | ||
protected buildEnumValuesBlock(values: ReadonlyArray<EnumValueDefinitionNode>): string; | ||
protected buildEnumValuesBlock(typeName: string, values: ReadonlyArray<EnumValueDefinitionNode>): string; | ||
DirectiveDefinition(node: DirectiveDefinitionNode): string; | ||
@@ -47,0 +57,0 @@ protected _getScalar(name: string): string; |
import { BaseVisitor } from './base-visitor'; | ||
import { parseMapper } from './mappers'; | ||
import { DEFAULT_SCALARS } from './scalars'; | ||
import { buildScalars, DeclarationBlock, indent, wrapWithSingleQuotes } from './utils'; | ||
import { OperationVariablesToObject } from './variables-to-object'; | ||
import { DeclarationBlock, indent, wrapWithSingleQuotes, buildScalars } from './utils'; | ||
import { DEFAULT_SCALARS } from './scalars'; | ||
export class BaseTypesVisitor extends BaseVisitor { | ||
@@ -94,3 +95,27 @@ constructor(_schema, rawConfig, additionalConfig, defaultScalars = DEFAULT_SCALARS) { | ||
} | ||
_buildEnumImport(identifier, source) { | ||
return `import { ${identifier} } from '${source}';`; | ||
} | ||
getEnumsImports() { | ||
return Object.keys(this.config.enumValues) | ||
.map(enumName => { | ||
const mappedValue = this.config.enumValues[enumName]; | ||
if (mappedValue && typeof mappedValue === 'string') { | ||
const mapper = parseMapper(mappedValue); | ||
if (mapper.isExternal) { | ||
const identifier = mapper.type === enumName ? enumName : `${mapper.type} as ${enumName}`; | ||
return this._buildEnumImport(identifier, mapper.source); | ||
} | ||
} | ||
return null; | ||
}) | ||
.filter(a => a) | ||
.join('\n'); | ||
} | ||
EnumTypeDefinition(node) { | ||
const enumName = node.name; | ||
// In case of mapped external enum string | ||
if (this.config.enumValues[enumName] && typeof this.config.enumValues[enumName] === 'string') { | ||
return null; | ||
} | ||
return new DeclarationBlock(this._declarationBlockConfig) | ||
@@ -100,6 +125,15 @@ .export() | ||
.withName(this.convertName(node)) | ||
.withBlock(this.buildEnumValuesBlock(node.values)).string; | ||
.withBlock(this.buildEnumValuesBlock(enumName, node.values)).string; | ||
} | ||
buildEnumValuesBlock(values) { | ||
return values.map(enumOption => indent(`${this.convertName(enumOption)}${this._declarationBlockConfig.enumNameValueSeparator} ${wrapWithSingleQuotes(this.config.enumValues[enumOption.name] || enumOption.name)}`)).join(',\n'); | ||
buildEnumValuesBlock(typeName, values) { | ||
return values | ||
.map(enumOption => { | ||
const optionName = this.convertName(enumOption); | ||
let enumValue = enumOption.name; | ||
if (this.config.enumValues[typeName] && typeof this.config.enumValues[typeName] === 'object' && this.config.enumValues[typeName][enumValue]) { | ||
enumValue = this.config.enumValues[typeName][enumValue]; | ||
} | ||
return indent(`${optionName}${this._declarationBlockConfig.enumNameValueSeparator} ${wrapWithSingleQuotes(enumValue)}`); | ||
}) | ||
.join(',\n'); | ||
} | ||
@@ -106,0 +140,0 @@ DirectiveDefinition(node) { |
@@ -7,3 +7,3 @@ export function parseMapper(mapper) { | ||
source, | ||
type | ||
type, | ||
}; | ||
@@ -13,3 +13,3 @@ } | ||
isExternal: false, | ||
type: mapper | ||
type: mapper, | ||
}; | ||
@@ -16,0 +16,0 @@ } |
@@ -6,3 +6,5 @@ import { ASTNode } from 'graphql'; | ||
export declare type EnumValuesMap = { | ||
[key: string]: string; | ||
[enumName: string]: string | { | ||
[key: string]: string; | ||
}; | ||
}; | ||
@@ -9,0 +11,0 @@ export declare type ConvertNameFn<T = {}> = ConvertFn<T>; |
{ | ||
"name": "@graphql-codegen/visitor-plugin-common", | ||
"version": "1.0.1-alpha-1f0a571f.15+1f0a571f", | ||
"version": "1.0.1-alpha-48c9ae6d.16+48c9ae6d", | ||
"license": "MIT", | ||
@@ -9,3 +9,3 @@ "scripts": { | ||
"dependencies": { | ||
"@graphql-codegen/plugin-helpers": "1.0.1-alpha-1f0a571f.15+1f0a571f", | ||
"@graphql-codegen/plugin-helpers": "1.0.1-alpha-48c9ae6d.16+48c9ae6d", | ||
"auto-bind": "2.0.0", | ||
@@ -20,3 +20,3 @@ "dependency-graph": "0.8.0", | ||
"devDependencies": { | ||
"@graphql-codegen/testing": "1.0.1-alpha-1f0a571f.15+1f0a571f", | ||
"@graphql-codegen/testing": "1.0.1-alpha-48c9ae6d.16+48c9ae6d", | ||
"@types/graphql": "14.0.7", | ||
@@ -36,3 +36,3 @@ "graphql": "14.1.1", | ||
}, | ||
"gitHead": "1f0a571fcedfa07497cc92f809c30a373cf53088" | ||
"gitHead": "48c9ae6d4af1ac322c69456b1130bd1af87a5754" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
220004
3242