graphql-codegen-visitor-plugin-common
Advanced tools
Comparing version 0.19.0-alpha.a74ef9c7 to 0.19.0-alpha.ab0b68e0
@@ -1,13 +0,8 @@ | ||
import { ScalarsMap, NamingConvention, ConvertFn, ConvertOptions } from './types'; | ||
import { BaseVisitor } from './base-visitor'; | ||
import { ScalarsMap, NamingConvention, ConvertFn } from './types'; | ||
import { DeclarationBlockConfig } from './utils'; | ||
import { NameNode, ListTypeNode, NamedTypeNode, FieldDefinitionNode, ObjectTypeDefinitionNode, GraphQLSchema } from 'graphql'; | ||
import { NonNullTypeNode, UnionTypeDefinitionNode, ScalarTypeDefinitionNode, InterfaceTypeDefinitionNode } from 'graphql/language/ast'; | ||
import { NameNode, ListTypeNode, NamedTypeNode, FieldDefinitionNode, ObjectTypeDefinitionNode, GraphQLSchema, NonNullTypeNode, UnionTypeDefinitionNode, ScalarTypeDefinitionNode, InterfaceTypeDefinitionNode } from 'graphql'; | ||
import { DirectiveDefinitionNode } from 'graphql'; | ||
import { OperationVariablesToObject } from './variables-to-object'; | ||
import { BaseVisitorConvertOptions } from './base-visitor'; | ||
interface ParsedMapper { | ||
isExternal: boolean; | ||
type: string; | ||
source?: string; | ||
} | ||
import { ParsedMapper } from './mappers'; | ||
export interface ParsedResolversConfig { | ||
@@ -17,3 +12,3 @@ scalars: ScalarsMap; | ||
typesPrefix: string; | ||
contextType: string; | ||
contextType: ParsedMapper; | ||
mappers: { | ||
@@ -32,3 +27,3 @@ [typeName: string]: ParsedMapper; | ||
} | ||
export declare class BaseResolversVisitor<TRawConfig extends RawResolversConfig = RawResolversConfig, TPluginConfig extends ParsedResolversConfig = ParsedResolversConfig> { | ||
export declare class BaseResolversVisitor<TRawConfig extends RawResolversConfig = RawResolversConfig, TPluginConfig extends ParsedResolversConfig = ParsedResolversConfig> extends BaseVisitor<TRawConfig, TPluginConfig> { | ||
private _schema; | ||
@@ -45,11 +40,5 @@ protected _parsedConfig: TPluginConfig; | ||
constructor(rawConfig: TRawConfig, additionalConfig: TPluginConfig, _schema: GraphQLSchema, defaultScalars?: ScalarsMap); | ||
private isExternalMapper; | ||
private parseMapper; | ||
private transformMappers; | ||
readonly config: TPluginConfig; | ||
readonly schema: GraphQLSchema; | ||
readonly scalars: ScalarsMap; | ||
readonly mappersImports: string[]; | ||
protected buildMapperImport(source: string, types: string[]): string; | ||
convertName(name: any, options?: ConvertOptions & BaseVisitorConvertOptions): string; | ||
setDeclarationBlockConfig(config: DeclarationBlockConfig): void; | ||
@@ -62,2 +51,3 @@ setVariablesTransformer(variablesTransfomer: OperationVariablesToObject): void; | ||
ListType(node: ListTypeNode): string; | ||
protected _getScalar(name: string): string; | ||
NamedType(node: NamedTypeNode): string; | ||
@@ -73,2 +63,1 @@ NonNullType(node: NonNullTypeNode): string; | ||
} | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const base_visitor_1 = require("./base-visitor"); | ||
const autoBind = require("auto-bind"); | ||
@@ -8,5 +9,6 @@ const scalars_1 = require("./scalars"); | ||
const variables_to_object_1 = require("./variables-to-object"); | ||
const naming_1 = require("./naming"); | ||
class BaseResolversVisitor { | ||
const mappers_1 = require("./mappers"); | ||
class BaseResolversVisitor extends base_visitor_1.BaseVisitor { | ||
constructor(rawConfig, additionalConfig, _schema, defaultScalars = scalars_1.DEFAULT_SCALARS) { | ||
super(rawConfig, Object.assign({ contextType: mappers_1.parseMapper(rawConfig.contextType || 'any'), mappers: mappers_1.transformMappers(rawConfig.mappers || {}) }, (additionalConfig || {})), utils_1.buildScalars(_schema, defaultScalars)); | ||
this._schema = _schema; | ||
@@ -16,41 +18,8 @@ this._declarationBlockConfig = {}; | ||
this._collectedDirectiveResolvers = {}; | ||
this._parsedConfig = Object.assign({ scalars: Object.assign({}, (defaultScalars || scalars_1.DEFAULT_SCALARS), (rawConfig.scalars || {})), convert: naming_1.convertFactory(rawConfig), typesPrefix: rawConfig.typesPrefix || '', contextType: rawConfig.contextType || 'any', mappers: this.transformMappers(rawConfig.mappers || {}) }, (additionalConfig || {})); | ||
autoBind(this); | ||
this._variablesTransfomer = new variables_to_object_1.OperationVariablesToObject(this.scalars, this.convertName); | ||
} | ||
isExternalMapper(value) { | ||
return value.includes('#'); | ||
} | ||
parseMapper(mapper) { | ||
if (this.isExternalMapper(mapper)) { | ||
const [source, type] = mapper.split('#'); | ||
return { | ||
isExternal: true, | ||
source, | ||
type | ||
}; | ||
} | ||
return { | ||
isExternal: false, | ||
type: mapper | ||
}; | ||
} | ||
transformMappers(rawMappers) { | ||
const result = {}; | ||
Object.keys(rawMappers).forEach(gqlTypeName => { | ||
const mapperDef = rawMappers[gqlTypeName]; | ||
const parsedMapper = this.parseMapper(mapperDef); | ||
result[gqlTypeName] = parsedMapper; | ||
}); | ||
return result; | ||
} | ||
get config() { | ||
return this._parsedConfig; | ||
} | ||
get schema() { | ||
return this._schema; | ||
} | ||
get scalars() { | ||
return this.config.scalars; | ||
} | ||
get mappersImports() { | ||
@@ -67,2 +36,8 @@ const groupedMappers = {}; | ||
}); | ||
if (this.config.contextType.isExternal) { | ||
if (!groupedMappers[this.config.contextType.source]) { | ||
groupedMappers[this.config.contextType.source] = []; | ||
} | ||
groupedMappers[this.config.contextType.source].push(this.config.contextType.type); | ||
} | ||
return Object.keys(groupedMappers).map(source => this.buildMapperImport(source, groupedMappers[source])); | ||
@@ -73,6 +48,2 @@ } | ||
} | ||
convertName(name, options) { | ||
const useTypesPrefix = options && typeof options.useTypesPrefix === 'boolean' ? options.useTypesPrefix : true; | ||
return (useTypesPrefix ? this.config.typesPrefix : '') + this.config.convert(name, options); | ||
} | ||
setDeclarationBlockConfig(config) { | ||
@@ -88,3 +59,3 @@ this._declarationBlockConfig = config; | ||
.asKind('type') | ||
.withName(this.convertName('IResolvers'), `<Context = ${this.config.contextType}>`) | ||
.withName(this.convertName('IResolvers'), `<Context = ${this.config.contextType.type}>`) | ||
.withBlock(Object.keys(this._collectedResolvers) | ||
@@ -104,3 +75,3 @@ .map(schemaTypeName => { | ||
.asKind('type') | ||
.withName(this.convertName('IDirectiveResolvers'), `<Context = ${this.config.contextType}>`) | ||
.withName(this.convertName('IDirectiveResolvers'), `<Context = ${this.config.contextType.type}>`) | ||
.withBlock(Object.keys(this._collectedDirectiveResolvers) | ||
@@ -118,7 +89,13 @@ .map(schemaTypeName => { | ||
const asString = node.type; | ||
return `Array<${asString}>`; | ||
return `ArrayOrIterable<${asString}>`; | ||
} | ||
_getScalar(name) { | ||
return `Scalars['${name}']`; | ||
} | ||
NamedType(node) { | ||
const type = this.config.scalars[node.name] || this.convertName(node); | ||
return `${type}`; | ||
const nameStr = node.name; | ||
if (this.config.scalars[nameStr]) { | ||
return this._getScalar(nameStr); | ||
} | ||
return this.convertName(node); | ||
} | ||
@@ -163,4 +140,4 @@ NonNullType(node) { | ||
.export() | ||
.asKind('interface') | ||
.withName(name, `<Context = ${this.config.contextType}, ParentType = ${type}>`) | ||
.asKind('type') | ||
.withName(name, `<Context = ${this.config.contextType.type}, ParentType = ${type}>`) | ||
.withBlock(node.fields.map((f) => f(node.name)).join('\n')); | ||
@@ -182,10 +159,13 @@ this._collectedResolvers[node.name] = name + '<Context>'; | ||
.export() | ||
.asKind('interface') | ||
.withName(name, `<Context = ${this.config.contextType}, ParentType = ${node.name}>`) | ||
.asKind('type') | ||
.withName(name, `<Context = ${this.config.contextType.type}, ParentType = ${node.name}>`) | ||
.withBlock(utils_1.indent(`__resolveType: TypeResolveFn<${possibleTypes}>`)).string; | ||
} | ||
ScalarTypeDefinition(node) { | ||
const baseName = this.convertName(node); | ||
const nameAsString = node.name; | ||
const baseName = this.scalars[nameAsString] ? this._getScalar(nameAsString) : this.convertName(node); | ||
this._collectedResolvers[node.name] = 'GraphQLScalarType'; | ||
return new utils_1.DeclarationBlock(this._declarationBlockConfig) | ||
return new utils_1.DeclarationBlock(Object.assign({}, this._declarationBlockConfig, { blockTransformer(block) { | ||
return block; | ||
} })) | ||
.export() | ||
@@ -207,6 +187,8 @@ .asKind('interface') | ||
this._collectedDirectiveResolvers[node.name] = directiveName + '<any, any, Context>'; | ||
return new utils_1.DeclarationBlock(this._declarationBlockConfig) | ||
return new utils_1.DeclarationBlock(Object.assign({}, this._declarationBlockConfig, { blockTransformer(block) { | ||
return block; | ||
} })) | ||
.export() | ||
.asKind('type') | ||
.withName(directiveName, `<Result, Parent, Context = ${this.config.contextType}, Args = { ${directiveArgs} }>`) | ||
.withName(directiveName, `<Result, Parent, Context = ${this.config.contextType.type}, Args = { ${directiveArgs} }>`) | ||
.withContent(`DirectiveResolverFn<Result, Parent, Context, Args>`).string; | ||
@@ -231,6 +213,8 @@ } | ||
.export() | ||
.asKind('interface') | ||
.withName(name, `<Context = ${this.config.contextType}, ParentType = ${node.name}>`) | ||
.withBlock(utils_1.indent(`__resolveType: TypeResolveFn<${implementingTypes.map(name => `'${name}'`).join(' | ')}>`)) | ||
.string; | ||
.asKind('type') | ||
.withName(name, `<Context = ${this.config.contextType.type}, ParentType = ${node.name}>`) | ||
.withBlock([ | ||
utils_1.indent(`__resolveType: TypeResolveFn<${implementingTypes.map(name => `'${name}'`).join(' | ')}>,`), | ||
...(node.fields || []).map((f) => f(node.name)) | ||
].join('\n')).string; | ||
} | ||
@@ -237,0 +221,0 @@ SchemaDefinition() { |
@@ -5,4 +5,3 @@ import { BaseVisitor, ParsedConfig, RawConfig } from './base-visitor'; | ||
import { DeclarationBlockConfig } from './utils'; | ||
import { NonNullTypeNode, UnionTypeDefinitionNode, InterfaceTypeDefinitionNode, ScalarTypeDefinitionNode, EnumValueDefinitionNode, NamedTypeNode } from 'graphql/language/ast'; | ||
import { InputObjectTypeDefinitionNode, InputValueDefinitionNode, NameNode, FieldDefinitionNode, ObjectTypeDefinitionNode, EnumTypeDefinitionNode, DirectiveDefinitionNode, ListTypeNode } from 'graphql'; | ||
import { InputObjectTypeDefinitionNode, InputValueDefinitionNode, NameNode, FieldDefinitionNode, ObjectTypeDefinitionNode, EnumTypeDefinitionNode, DirectiveDefinitionNode, ListTypeNode, GraphQLSchema, NonNullTypeNode, UnionTypeDefinitionNode, InterfaceTypeDefinitionNode, ScalarTypeDefinitionNode, EnumValueDefinitionNode, NamedTypeNode } from 'graphql'; | ||
export interface ParsedTypesConfig extends ParsedConfig { | ||
@@ -15,4 +14,6 @@ enumValues: EnumValuesMap; | ||
export declare class BaseTypesVisitor<TRawConfig extends RawTypesConfig = RawTypesConfig, TPluginConfig extends ParsedTypesConfig = ParsedTypesConfig> extends BaseVisitor<TRawConfig, TPluginConfig> { | ||
protected _schema: GraphQLSchema; | ||
protected _argumentsTransformer: OperationVariablesToObject; | ||
constructor(rawConfig: TRawConfig, additionalConfig: TPluginConfig, defaultScalars?: ScalarsMap); | ||
constructor(_schema: GraphQLSchema, rawConfig: TRawConfig, additionalConfig: TPluginConfig, defaultScalars?: ScalarsMap); | ||
readonly scalarsDefinition: string; | ||
setDeclarationBlockConfig(config: DeclarationBlockConfig): void; | ||
@@ -32,2 +33,3 @@ setArgumentsTransformer(argumentsTransfomer: OperationVariablesToObject): void; | ||
DirectiveDefinition(node: DirectiveDefinitionNode): string; | ||
protected _getScalar(name: string): string; | ||
protected _getTypeForNode(node: NamedTypeNode): string; | ||
@@ -34,0 +36,0 @@ NamedType(node: NamedTypeNode): string; |
@@ -8,6 +8,18 @@ "use strict"; | ||
class BaseTypesVisitor extends base_visitor_1.BaseVisitor { | ||
constructor(rawConfig, additionalConfig, defaultScalars = scalars_1.DEFAULT_SCALARS) { | ||
super(rawConfig, Object.assign({ enumValues: rawConfig.enumValues || {} }, additionalConfig), defaultScalars); | ||
constructor(_schema, rawConfig, additionalConfig, defaultScalars = scalars_1.DEFAULT_SCALARS) { | ||
super(rawConfig, Object.assign({ enumValues: rawConfig.enumValues || {} }, additionalConfig), utils_1.buildScalars(_schema, defaultScalars)); | ||
this._schema = _schema; | ||
this._argumentsTransformer = new variables_to_object_1.OperationVariablesToObject(this.scalars, this.convertName); | ||
} | ||
get scalarsDefinition() { | ||
const allScalars = Object.keys(this.config.scalars).map(scalarName => { | ||
const scalarValue = this.config.scalars[scalarName]; | ||
return utils_1.indent(`${scalarName}: ${scalarValue},`); | ||
}); | ||
return new utils_1.DeclarationBlock(this._declarationBlockConfig) | ||
.export() | ||
.asKind('type') | ||
.withName('Scalars') | ||
.withBlock(allScalars.join('\n')).string; | ||
} | ||
setDeclarationBlockConfig(config) { | ||
@@ -42,3 +54,5 @@ this._declarationBlockConfig = config; | ||
const originalNode = parent[key]; | ||
const possibleTypes = originalNode.types.map(t => this.convertName(t)).join(' | '); | ||
const possibleTypes = originalNode.types | ||
.map(t => (this.scalars[t.name.value] ? this._getScalar(t.name.value) : this.convertName(t))) | ||
.join(' | '); | ||
return new utils_1.DeclarationBlock(this._declarationBlockConfig) | ||
@@ -85,7 +99,4 @@ .export() | ||
ScalarTypeDefinition(node) { | ||
return new utils_1.DeclarationBlock(this._declarationBlockConfig) | ||
.export() | ||
.asKind('type') | ||
.withName(this.convertName(node)) | ||
.withContent(this.config.scalars[node.name] || 'any').string; | ||
// We empty this because we handle scalars in a different way, see constructor. | ||
return ''; | ||
} | ||
@@ -107,4 +118,11 @@ EnumTypeDefinition(node) { | ||
} | ||
_getScalar(name) { | ||
return `Scalars['${name}']`; | ||
} | ||
_getTypeForNode(node) { | ||
return this.scalars[node.name] || this.convertName(node); | ||
const typeAsString = node.name; | ||
if (this.scalars[typeAsString]) { | ||
return this._getScalar(typeAsString); | ||
} | ||
return this.convertName(node); | ||
} | ||
@@ -111,0 +129,0 @@ NamedType(node) { |
@@ -7,5 +7,2 @@ export declare const DEFAULT_SCALARS: { | ||
Float: string; | ||
string: string; | ||
number: string; | ||
boolean: string; | ||
}; |
@@ -8,7 +8,4 @@ "use strict"; | ||
Int: 'number', | ||
Float: 'number', | ||
string: 'string', | ||
number: 'number', | ||
boolean: 'boolean' | ||
Float: 'number' | ||
}; | ||
//# sourceMappingURL=scalars.js.map |
@@ -54,3 +54,3 @@ "use strict"; | ||
const typeName = baseType.name; | ||
if (this._scalars[typeName] || graphql_1.isEnumType(baseType)) { | ||
if (this._scalars[typeName] || graphql_1.isEnumType(baseType) || graphql_1.isScalarType(baseType)) { | ||
if (field.alias && field.alias.value) { | ||
@@ -57,0 +57,0 @@ this._primitiveAliasedFields.push({ |
@@ -1,2 +0,3 @@ | ||
import { NameNode, TypeNode, NamedTypeNode, GraphQLObjectType, GraphQLNonNull, GraphQLList, GraphQLOutputType, GraphQLNamedType } from 'graphql'; | ||
import { NameNode, TypeNode, NamedTypeNode, GraphQLObjectType, GraphQLNonNull, GraphQLList, GraphQLOutputType, GraphQLNamedType, GraphQLSchema } from 'graphql'; | ||
import { ScalarsMap } from './types'; | ||
export declare const getConfigValue: <T = any>(value: T, defaultValue: T) => T; | ||
@@ -11,2 +12,3 @@ export declare function getBaseType(type: GraphQLOutputType): GraphQLNamedType; | ||
blockWrapper?: string; | ||
blockTransformer?: (block: string) => string; | ||
enumNameValueSeparator?: string; | ||
@@ -41,1 +43,2 @@ } | ||
}>>) => string; | ||
export declare function buildScalars(schema: GraphQLSchema, scalarsMapping: ScalarsMap): ScalarsMap; |
@@ -61,3 +61,3 @@ "use strict"; | ||
this._nameGenerics = null; | ||
this._config = Object.assign({ blockWrapper: '', enumNameValueSeparator: ':' }, this._config); | ||
this._config = Object.assign({ blockWrapper: '', blockTransformer: block => block, enumNameValueSeparator: ':' }, this._config); | ||
} | ||
@@ -109,11 +109,10 @@ export(exp = true) { | ||
} | ||
const before = '{' + this._config.blockWrapper; | ||
const after = this._config.blockWrapper + '}'; | ||
const block = [before, this._block, after].join('\n'); | ||
if (this._methodName) { | ||
result += `${this._methodName}({${this._config.blockWrapper} | ||
${this._block} | ||
${this._config.blockWrapper}})`; | ||
result += `${this._methodName}(${this._config.blockTransformer(block)})`; | ||
} | ||
else { | ||
result += `{${this._config.blockWrapper} | ||
${this._block} | ||
${this._config.blockWrapper}}`; | ||
result += this._config.blockTransformer(block); | ||
} | ||
@@ -157,2 +156,16 @@ } | ||
}; | ||
function buildScalars(schema, scalarsMapping) { | ||
const typeMap = schema.getTypeMap(); | ||
let result = Object.assign({}, scalarsMapping); | ||
Object.keys(typeMap) | ||
.map(typeName => typeMap[typeName]) | ||
.filter(type => graphql_1.isScalarType(type)) | ||
.map((scalarType) => { | ||
const name = scalarType.name; | ||
const value = scalarsMapping[name] || 'any'; | ||
result[name] = value; | ||
}); | ||
return result; | ||
} | ||
exports.buildScalars = buildScalars; | ||
//# sourceMappingURL=utils.js.map |
@@ -16,2 +16,3 @@ import { TypeNode, VariableNode, NameNode, ValueNode } from 'graphql'; | ||
transform<TDefinitionType extends InterfaceOrVariable>(variablesNode: ReadonlyArray<TDefinitionType>): string; | ||
protected getScalar(name: string): string; | ||
protected transformVariable<TDefinitionType extends InterfaceOrVariable>(variable: TDefinitionType): string; | ||
@@ -18,0 +19,0 @@ wrapAstTypeWithModifiers(baseType: string, typeNode: TypeNode): string; |
@@ -30,9 +30,19 @@ "use strict"; | ||
} | ||
getScalar(name) { | ||
return `Scalars['${name}']`; | ||
} | ||
transformVariable(variable) { | ||
const baseType = typeof variable.type === 'string' ? variable.type : utils_1.getBaseTypeNode(variable.type); | ||
const typeName = typeof baseType === 'string' ? baseType : baseType.name.value; | ||
const typeValue = this._scalars[typeName] || | ||
this._convertName(baseType, { | ||
useTypesPrefix: true | ||
}); | ||
let typeValue = null; | ||
if (typeof variable.type === 'string') { | ||
typeValue = variable.type; | ||
} | ||
else { | ||
const baseType = utils_1.getBaseTypeNode(variable.type); | ||
const typeName = baseType.name.value; | ||
typeValue = this._scalars[typeName] | ||
? this.getScalar(typeName) | ||
: this._convertName(baseType, { | ||
useTypesPrefix: true | ||
}); | ||
} | ||
const fieldName = this.getName(variable); | ||
@@ -39,0 +49,0 @@ const fieldType = this.wrapAstTypeWithModifiers(typeValue, variable.type); |
@@ -1,13 +0,8 @@ | ||
import { ScalarsMap, NamingConvention, ConvertFn, ConvertOptions } from './types'; | ||
import { BaseVisitor } from './base-visitor'; | ||
import { ScalarsMap, NamingConvention, ConvertFn } from './types'; | ||
import { DeclarationBlockConfig } from './utils'; | ||
import { NameNode, ListTypeNode, NamedTypeNode, FieldDefinitionNode, ObjectTypeDefinitionNode, GraphQLSchema } from 'graphql'; | ||
import { NonNullTypeNode, UnionTypeDefinitionNode, ScalarTypeDefinitionNode, InterfaceTypeDefinitionNode } from 'graphql/language/ast'; | ||
import { NameNode, ListTypeNode, NamedTypeNode, FieldDefinitionNode, ObjectTypeDefinitionNode, GraphQLSchema, NonNullTypeNode, UnionTypeDefinitionNode, ScalarTypeDefinitionNode, InterfaceTypeDefinitionNode } from 'graphql'; | ||
import { DirectiveDefinitionNode } from 'graphql'; | ||
import { OperationVariablesToObject } from './variables-to-object'; | ||
import { BaseVisitorConvertOptions } from './base-visitor'; | ||
interface ParsedMapper { | ||
isExternal: boolean; | ||
type: string; | ||
source?: string; | ||
} | ||
import { ParsedMapper } from './mappers'; | ||
export interface ParsedResolversConfig { | ||
@@ -17,3 +12,3 @@ scalars: ScalarsMap; | ||
typesPrefix: string; | ||
contextType: string; | ||
contextType: ParsedMapper; | ||
mappers: { | ||
@@ -32,3 +27,3 @@ [typeName: string]: ParsedMapper; | ||
} | ||
export declare class BaseResolversVisitor<TRawConfig extends RawResolversConfig = RawResolversConfig, TPluginConfig extends ParsedResolversConfig = ParsedResolversConfig> { | ||
export declare class BaseResolversVisitor<TRawConfig extends RawResolversConfig = RawResolversConfig, TPluginConfig extends ParsedResolversConfig = ParsedResolversConfig> extends BaseVisitor<TRawConfig, TPluginConfig> { | ||
private _schema; | ||
@@ -45,11 +40,5 @@ protected _parsedConfig: TPluginConfig; | ||
constructor(rawConfig: TRawConfig, additionalConfig: TPluginConfig, _schema: GraphQLSchema, defaultScalars?: ScalarsMap); | ||
private isExternalMapper; | ||
private parseMapper; | ||
private transformMappers; | ||
readonly config: TPluginConfig; | ||
readonly schema: GraphQLSchema; | ||
readonly scalars: ScalarsMap; | ||
readonly mappersImports: string[]; | ||
protected buildMapperImport(source: string, types: string[]): string; | ||
convertName(name: any, options?: ConvertOptions & BaseVisitorConvertOptions): string; | ||
setDeclarationBlockConfig(config: DeclarationBlockConfig): void; | ||
@@ -62,2 +51,3 @@ setVariablesTransformer(variablesTransfomer: OperationVariablesToObject): void; | ||
ListType(node: ListTypeNode): string; | ||
protected _getScalar(name: string): string; | ||
NamedType(node: NamedTypeNode): string; | ||
@@ -73,2 +63,1 @@ NonNullType(node: NonNullTypeNode): string; | ||
} | ||
export {}; |
@@ -0,9 +1,11 @@ | ||
import { BaseVisitor } from './base-visitor'; | ||
import * as autoBind from 'auto-bind'; | ||
import { DEFAULT_SCALARS } from './scalars'; | ||
import { DeclarationBlock, indent, getBaseTypeNode } from './utils'; | ||
import { DeclarationBlock, indent, getBaseTypeNode, buildScalars } from './utils'; | ||
import { GraphQLObjectType } from 'graphql'; | ||
import { OperationVariablesToObject } from './variables-to-object'; | ||
import { convertFactory } from './naming'; | ||
export class BaseResolversVisitor { | ||
import { parseMapper, transformMappers } from './mappers'; | ||
export class BaseResolversVisitor extends BaseVisitor { | ||
constructor(rawConfig, additionalConfig, _schema, defaultScalars = DEFAULT_SCALARS) { | ||
super(rawConfig, Object.assign({ contextType: parseMapper(rawConfig.contextType || 'any'), mappers: transformMappers(rawConfig.mappers || {}) }, (additionalConfig || {})), buildScalars(_schema, defaultScalars)); | ||
this._schema = _schema; | ||
@@ -13,41 +15,8 @@ this._declarationBlockConfig = {}; | ||
this._collectedDirectiveResolvers = {}; | ||
this._parsedConfig = Object.assign({ scalars: Object.assign({}, (defaultScalars || DEFAULT_SCALARS), (rawConfig.scalars || {})), convert: convertFactory(rawConfig), typesPrefix: rawConfig.typesPrefix || '', contextType: rawConfig.contextType || 'any', mappers: this.transformMappers(rawConfig.mappers || {}) }, (additionalConfig || {})); | ||
autoBind(this); | ||
this._variablesTransfomer = new OperationVariablesToObject(this.scalars, this.convertName); | ||
} | ||
isExternalMapper(value) { | ||
return value.includes('#'); | ||
} | ||
parseMapper(mapper) { | ||
if (this.isExternalMapper(mapper)) { | ||
const [source, type] = mapper.split('#'); | ||
return { | ||
isExternal: true, | ||
source, | ||
type | ||
}; | ||
} | ||
return { | ||
isExternal: false, | ||
type: mapper | ||
}; | ||
} | ||
transformMappers(rawMappers) { | ||
const result = {}; | ||
Object.keys(rawMappers).forEach(gqlTypeName => { | ||
const mapperDef = rawMappers[gqlTypeName]; | ||
const parsedMapper = this.parseMapper(mapperDef); | ||
result[gqlTypeName] = parsedMapper; | ||
}); | ||
return result; | ||
} | ||
get config() { | ||
return this._parsedConfig; | ||
} | ||
get schema() { | ||
return this._schema; | ||
} | ||
get scalars() { | ||
return this.config.scalars; | ||
} | ||
get mappersImports() { | ||
@@ -64,2 +33,8 @@ const groupedMappers = {}; | ||
}); | ||
if (this.config.contextType.isExternal) { | ||
if (!groupedMappers[this.config.contextType.source]) { | ||
groupedMappers[this.config.contextType.source] = []; | ||
} | ||
groupedMappers[this.config.contextType.source].push(this.config.contextType.type); | ||
} | ||
return Object.keys(groupedMappers).map(source => this.buildMapperImport(source, groupedMappers[source])); | ||
@@ -70,6 +45,2 @@ } | ||
} | ||
convertName(name, options) { | ||
const useTypesPrefix = options && typeof options.useTypesPrefix === 'boolean' ? options.useTypesPrefix : true; | ||
return (useTypesPrefix ? this.config.typesPrefix : '') + this.config.convert(name, options); | ||
} | ||
setDeclarationBlockConfig(config) { | ||
@@ -85,3 +56,3 @@ this._declarationBlockConfig = config; | ||
.asKind('type') | ||
.withName(this.convertName('IResolvers'), `<Context = ${this.config.contextType}>`) | ||
.withName(this.convertName('IResolvers'), `<Context = ${this.config.contextType.type}>`) | ||
.withBlock(Object.keys(this._collectedResolvers) | ||
@@ -101,3 +72,3 @@ .map(schemaTypeName => { | ||
.asKind('type') | ||
.withName(this.convertName('IDirectiveResolvers'), `<Context = ${this.config.contextType}>`) | ||
.withName(this.convertName('IDirectiveResolvers'), `<Context = ${this.config.contextType.type}>`) | ||
.withBlock(Object.keys(this._collectedDirectiveResolvers) | ||
@@ -115,7 +86,13 @@ .map(schemaTypeName => { | ||
const asString = node.type; | ||
return `Array<${asString}>`; | ||
return `ArrayOrIterable<${asString}>`; | ||
} | ||
_getScalar(name) { | ||
return `Scalars['${name}']`; | ||
} | ||
NamedType(node) { | ||
const type = this.config.scalars[node.name] || this.convertName(node); | ||
return `${type}`; | ||
const nameStr = node.name; | ||
if (this.config.scalars[nameStr]) { | ||
return this._getScalar(nameStr); | ||
} | ||
return this.convertName(node); | ||
} | ||
@@ -160,4 +137,4 @@ NonNullType(node) { | ||
.export() | ||
.asKind('interface') | ||
.withName(name, `<Context = ${this.config.contextType}, ParentType = ${type}>`) | ||
.asKind('type') | ||
.withName(name, `<Context = ${this.config.contextType.type}, ParentType = ${type}>`) | ||
.withBlock(node.fields.map((f) => f(node.name)).join('\n')); | ||
@@ -179,10 +156,13 @@ this._collectedResolvers[node.name] = name + '<Context>'; | ||
.export() | ||
.asKind('interface') | ||
.withName(name, `<Context = ${this.config.contextType}, ParentType = ${node.name}>`) | ||
.asKind('type') | ||
.withName(name, `<Context = ${this.config.contextType.type}, ParentType = ${node.name}>`) | ||
.withBlock(indent(`__resolveType: TypeResolveFn<${possibleTypes}>`)).string; | ||
} | ||
ScalarTypeDefinition(node) { | ||
const baseName = this.convertName(node); | ||
const nameAsString = node.name; | ||
const baseName = this.scalars[nameAsString] ? this._getScalar(nameAsString) : this.convertName(node); | ||
this._collectedResolvers[node.name] = 'GraphQLScalarType'; | ||
return new DeclarationBlock(this._declarationBlockConfig) | ||
return new DeclarationBlock(Object.assign({}, this._declarationBlockConfig, { blockTransformer(block) { | ||
return block; | ||
} })) | ||
.export() | ||
@@ -204,6 +184,8 @@ .asKind('interface') | ||
this._collectedDirectiveResolvers[node.name] = directiveName + '<any, any, Context>'; | ||
return new DeclarationBlock(this._declarationBlockConfig) | ||
return new DeclarationBlock(Object.assign({}, this._declarationBlockConfig, { blockTransformer(block) { | ||
return block; | ||
} })) | ||
.export() | ||
.asKind('type') | ||
.withName(directiveName, `<Result, Parent, Context = ${this.config.contextType}, Args = { ${directiveArgs} }>`) | ||
.withName(directiveName, `<Result, Parent, Context = ${this.config.contextType.type}, Args = { ${directiveArgs} }>`) | ||
.withContent(`DirectiveResolverFn<Result, Parent, Context, Args>`).string; | ||
@@ -228,6 +210,8 @@ } | ||
.export() | ||
.asKind('interface') | ||
.withName(name, `<Context = ${this.config.contextType}, ParentType = ${node.name}>`) | ||
.withBlock(indent(`__resolveType: TypeResolveFn<${implementingTypes.map(name => `'${name}'`).join(' | ')}>`)) | ||
.string; | ||
.asKind('type') | ||
.withName(name, `<Context = ${this.config.contextType.type}, ParentType = ${node.name}>`) | ||
.withBlock([ | ||
indent(`__resolveType: TypeResolveFn<${implementingTypes.map(name => `'${name}'`).join(' | ')}>,`), | ||
...(node.fields || []).map((f) => f(node.name)) | ||
].join('\n')).string; | ||
} | ||
@@ -234,0 +218,0 @@ SchemaDefinition() { |
@@ -5,4 +5,3 @@ import { BaseVisitor, ParsedConfig, RawConfig } from './base-visitor'; | ||
import { DeclarationBlockConfig } from './utils'; | ||
import { NonNullTypeNode, UnionTypeDefinitionNode, InterfaceTypeDefinitionNode, ScalarTypeDefinitionNode, EnumValueDefinitionNode, NamedTypeNode } from 'graphql/language/ast'; | ||
import { InputObjectTypeDefinitionNode, InputValueDefinitionNode, NameNode, FieldDefinitionNode, ObjectTypeDefinitionNode, EnumTypeDefinitionNode, DirectiveDefinitionNode, ListTypeNode } from 'graphql'; | ||
import { InputObjectTypeDefinitionNode, InputValueDefinitionNode, NameNode, FieldDefinitionNode, ObjectTypeDefinitionNode, EnumTypeDefinitionNode, DirectiveDefinitionNode, ListTypeNode, GraphQLSchema, NonNullTypeNode, UnionTypeDefinitionNode, InterfaceTypeDefinitionNode, ScalarTypeDefinitionNode, EnumValueDefinitionNode, NamedTypeNode } from 'graphql'; | ||
export interface ParsedTypesConfig extends ParsedConfig { | ||
@@ -15,4 +14,6 @@ enumValues: EnumValuesMap; | ||
export declare class BaseTypesVisitor<TRawConfig extends RawTypesConfig = RawTypesConfig, TPluginConfig extends ParsedTypesConfig = ParsedTypesConfig> extends BaseVisitor<TRawConfig, TPluginConfig> { | ||
protected _schema: GraphQLSchema; | ||
protected _argumentsTransformer: OperationVariablesToObject; | ||
constructor(rawConfig: TRawConfig, additionalConfig: TPluginConfig, defaultScalars?: ScalarsMap); | ||
constructor(_schema: GraphQLSchema, rawConfig: TRawConfig, additionalConfig: TPluginConfig, defaultScalars?: ScalarsMap); | ||
readonly scalarsDefinition: string; | ||
setDeclarationBlockConfig(config: DeclarationBlockConfig): void; | ||
@@ -32,2 +33,3 @@ setArgumentsTransformer(argumentsTransfomer: OperationVariablesToObject): void; | ||
DirectiveDefinition(node: DirectiveDefinitionNode): string; | ||
protected _getScalar(name: string): string; | ||
protected _getTypeForNode(node: NamedTypeNode): string; | ||
@@ -34,0 +36,0 @@ NamedType(node: NamedTypeNode): string; |
import { BaseVisitor } from './base-visitor'; | ||
import { OperationVariablesToObject } from './variables-to-object'; | ||
import { DeclarationBlock, indent, wrapWithSingleQuotes } from './utils'; | ||
import { DeclarationBlock, indent, wrapWithSingleQuotes, buildScalars } from './utils'; | ||
import { DEFAULT_SCALARS } from './scalars'; | ||
export class BaseTypesVisitor extends BaseVisitor { | ||
constructor(rawConfig, additionalConfig, defaultScalars = DEFAULT_SCALARS) { | ||
super(rawConfig, Object.assign({ enumValues: rawConfig.enumValues || {} }, additionalConfig), defaultScalars); | ||
constructor(_schema, rawConfig, additionalConfig, defaultScalars = DEFAULT_SCALARS) { | ||
super(rawConfig, Object.assign({ enumValues: rawConfig.enumValues || {} }, additionalConfig), buildScalars(_schema, defaultScalars)); | ||
this._schema = _schema; | ||
this._argumentsTransformer = new OperationVariablesToObject(this.scalars, this.convertName); | ||
} | ||
get scalarsDefinition() { | ||
const allScalars = Object.keys(this.config.scalars).map(scalarName => { | ||
const scalarValue = this.config.scalars[scalarName]; | ||
return indent(`${scalarName}: ${scalarValue},`); | ||
}); | ||
return new DeclarationBlock(this._declarationBlockConfig) | ||
.export() | ||
.asKind('type') | ||
.withName('Scalars') | ||
.withBlock(allScalars.join('\n')).string; | ||
} | ||
setDeclarationBlockConfig(config) { | ||
@@ -39,3 +51,5 @@ this._declarationBlockConfig = config; | ||
const originalNode = parent[key]; | ||
const possibleTypes = originalNode.types.map(t => this.convertName(t)).join(' | '); | ||
const possibleTypes = originalNode.types | ||
.map(t => (this.scalars[t.name.value] ? this._getScalar(t.name.value) : this.convertName(t))) | ||
.join(' | '); | ||
return new DeclarationBlock(this._declarationBlockConfig) | ||
@@ -82,7 +96,4 @@ .export() | ||
ScalarTypeDefinition(node) { | ||
return new DeclarationBlock(this._declarationBlockConfig) | ||
.export() | ||
.asKind('type') | ||
.withName(this.convertName(node)) | ||
.withContent(this.config.scalars[node.name] || 'any').string; | ||
// We empty this because we handle scalars in a different way, see constructor. | ||
return ''; | ||
} | ||
@@ -104,4 +115,11 @@ EnumTypeDefinition(node) { | ||
} | ||
_getScalar(name) { | ||
return `Scalars['${name}']`; | ||
} | ||
_getTypeForNode(node) { | ||
return this.scalars[node.name] || this.convertName(node); | ||
const typeAsString = node.name; | ||
if (this.scalars[typeAsString]) { | ||
return this._getScalar(typeAsString); | ||
} | ||
return this.convertName(node); | ||
} | ||
@@ -108,0 +126,0 @@ NamedType(node) { |
@@ -7,5 +7,2 @@ export declare const DEFAULT_SCALARS: { | ||
Float: string; | ||
string: string; | ||
number: string; | ||
boolean: string; | ||
}; |
@@ -6,7 +6,4 @@ export const DEFAULT_SCALARS = { | ||
Int: 'number', | ||
Float: 'number', | ||
string: 'string', | ||
number: 'number', | ||
boolean: 'boolean' | ||
Float: 'number' | ||
}; | ||
//# sourceMappingURL=scalars.js.map |
@@ -1,2 +0,2 @@ | ||
import { Kind, isObjectType, isUnionType, isInterfaceType, isEnumType, isEqualType, SchemaMetaFieldDef, TypeMetaFieldDef } from 'graphql'; | ||
import { Kind, isObjectType, isUnionType, isInterfaceType, isEnumType, isEqualType, SchemaMetaFieldDef, TypeMetaFieldDef, isScalarType } from 'graphql'; | ||
import { getBaseType, quoteIfNeeded } from './utils'; | ||
@@ -52,3 +52,3 @@ function isMetadataFieldName(name) { | ||
const typeName = baseType.name; | ||
if (this._scalars[typeName] || isEnumType(baseType)) { | ||
if (this._scalars[typeName] || isEnumType(baseType) || isScalarType(baseType)) { | ||
if (field.alias && field.alias.value) { | ||
@@ -55,0 +55,0 @@ this._primitiveAliasedFields.push({ |
@@ -1,2 +0,3 @@ | ||
import { NameNode, TypeNode, NamedTypeNode, GraphQLObjectType, GraphQLNonNull, GraphQLList, GraphQLOutputType, GraphQLNamedType } from 'graphql'; | ||
import { NameNode, TypeNode, NamedTypeNode, GraphQLObjectType, GraphQLNonNull, GraphQLList, GraphQLOutputType, GraphQLNamedType, GraphQLSchema } from 'graphql'; | ||
import { ScalarsMap } from './types'; | ||
export declare const getConfigValue: <T = any>(value: T, defaultValue: T) => T; | ||
@@ -11,2 +12,3 @@ export declare function getBaseType(type: GraphQLOutputType): GraphQLNamedType; | ||
blockWrapper?: string; | ||
blockTransformer?: (block: string) => string; | ||
enumNameValueSeparator?: string; | ||
@@ -41,1 +43,2 @@ } | ||
}>>) => string; | ||
export declare function buildScalars(schema: GraphQLSchema, scalarsMapping: ScalarsMap): ScalarsMap; |
import { pascalCase } from 'change-case'; | ||
import { Kind, isNonNullType, isListType } from 'graphql'; | ||
import { Kind, isNonNullType, isListType, isScalarType } from 'graphql'; | ||
function isWrapperType(t) { | ||
@@ -53,3 +53,3 @@ return isListType(t) || isNonNullType(t); | ||
this._nameGenerics = null; | ||
this._config = Object.assign({ blockWrapper: '', enumNameValueSeparator: ':' }, this._config); | ||
this._config = Object.assign({ blockWrapper: '', blockTransformer: block => block, enumNameValueSeparator: ':' }, this._config); | ||
} | ||
@@ -101,11 +101,10 @@ export(exp = true) { | ||
} | ||
const before = '{' + this._config.blockWrapper; | ||
const after = this._config.blockWrapper + '}'; | ||
const block = [before, this._block, after].join('\n'); | ||
if (this._methodName) { | ||
result += `${this._methodName}({${this._config.blockWrapper} | ||
${this._block} | ||
${this._config.blockWrapper}})`; | ||
result += `${this._methodName}(${this._config.blockTransformer(block)})`; | ||
} | ||
else { | ||
result += `{${this._config.blockWrapper} | ||
${this._block} | ||
${this._config.blockWrapper}}`; | ||
result += this._config.blockTransformer(block); | ||
} | ||
@@ -146,2 +145,15 @@ } | ||
}; | ||
export function buildScalars(schema, scalarsMapping) { | ||
const typeMap = schema.getTypeMap(); | ||
let result = Object.assign({}, scalarsMapping); | ||
Object.keys(typeMap) | ||
.map(typeName => typeMap[typeName]) | ||
.filter(type => isScalarType(type)) | ||
.map((scalarType) => { | ||
const name = scalarType.name; | ||
const value = scalarsMapping[name] || 'any'; | ||
result[name] = value; | ||
}); | ||
return result; | ||
} | ||
//# sourceMappingURL=utils.js.map |
@@ -16,2 +16,3 @@ import { TypeNode, VariableNode, NameNode, ValueNode } from 'graphql'; | ||
transform<TDefinitionType extends InterfaceOrVariable>(variablesNode: ReadonlyArray<TDefinitionType>): string; | ||
protected getScalar(name: string): string; | ||
protected transformVariable<TDefinitionType extends InterfaceOrVariable>(variable: TDefinitionType): string; | ||
@@ -18,0 +19,0 @@ wrapAstTypeWithModifiers(baseType: string, typeNode: TypeNode): string; |
@@ -28,9 +28,19 @@ import { Kind } from 'graphql'; | ||
} | ||
getScalar(name) { | ||
return `Scalars['${name}']`; | ||
} | ||
transformVariable(variable) { | ||
const baseType = typeof variable.type === 'string' ? variable.type : getBaseTypeNode(variable.type); | ||
const typeName = typeof baseType === 'string' ? baseType : baseType.name.value; | ||
const typeValue = this._scalars[typeName] || | ||
this._convertName(baseType, { | ||
useTypesPrefix: true | ||
}); | ||
let typeValue = null; | ||
if (typeof variable.type === 'string') { | ||
typeValue = variable.type; | ||
} | ||
else { | ||
const baseType = getBaseTypeNode(variable.type); | ||
const typeName = baseType.name.value; | ||
typeValue = this._scalars[typeName] | ||
? this.getScalar(typeName) | ||
: this._convertName(baseType, { | ||
useTypesPrefix: true | ||
}); | ||
} | ||
const fieldName = this.getName(variable); | ||
@@ -37,0 +47,0 @@ const fieldType = this.wrapAstTypeWithModifiers(typeValue, variable.type); |
{ | ||
"name": "graphql-codegen-visitor-plugin-common", | ||
"version": "0.19.0-alpha.a74ef9c7", | ||
"version": "0.19.0-alpha.ab0b68e0", | ||
"license": "MIT", | ||
@@ -11,3 +11,3 @@ "scripts": { | ||
"dependency-graph": "0.8.0", | ||
"graphql-codegen-plugin-helpers": "0.19.0-alpha.a74ef9c7", | ||
"graphql-codegen-plugin-helpers": "0.19.0-alpha.ab0b68e0", | ||
"graphql-tag": "2.10.1", | ||
@@ -22,3 +22,3 @@ "tslib": "1.9.3" | ||
"graphql": "14.1.1", | ||
"graphql-codegen-testing": "0.19.0-alpha.a74ef9c7", | ||
"graphql-codegen-testing": "0.19.0-alpha.ab0b68e0", | ||
"typescript": "3.3.3333" | ||
@@ -25,0 +25,0 @@ }, |
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
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
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
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
206228
80
2923
0
+ Addedgraphql-codegen-plugin-helpers@0.19.0-alpha.ab0b68e0(transitive)
- Removedgraphql-codegen-plugin-helpers@0.19.0-alpha.a74ef9c7(transitive)
- Removedts-log@2.1.4(transitive)