graphql-codegen-visitor-plugin-common
Advanced tools
Comparing version 0.19.0-alpha.35980e3e to 0.19.0-alpha.5634c385
@@ -1,9 +0,10 @@ | ||
import { ScalarsMap } from './types'; | ||
import { ScalarsMap, NamingConvention, ConvertFn, ConvertOptions } from './types'; | ||
import { DeclarationBlockConfig } from './utils'; | ||
import { GraphQLSchema, FragmentDefinitionNode, OperationDefinitionNode } from 'graphql'; | ||
import { GraphQLSchema, FragmentDefinitionNode, OperationDefinitionNode, ASTNode } from 'graphql'; | ||
import { SelectionSetToObject } from './selection-set-to-object'; | ||
import { OperationVariablesToObject } from './variables-to-object'; | ||
import { BaseVisitorConvertOptions } from './base-visitor'; | ||
export interface ParsedDocumentsConfig { | ||
scalars: ScalarsMap; | ||
convert: (str: string) => string; | ||
convert: ConvertFn<ConvertOptions>; | ||
typesPrefix: string; | ||
@@ -14,3 +15,3 @@ addTypename: boolean; | ||
scalars?: ScalarsMap; | ||
namingConvention?: string; | ||
namingConvention?: NamingConvention; | ||
typesPrefix?: string; | ||
@@ -30,3 +31,3 @@ skipTypename?: boolean; | ||
setVariablesTransformer(variablesTransfomer: OperationVariablesToObject): void; | ||
convertName(name: any, addPrefix?: boolean): string; | ||
convertName(node: ASTNode | string, options?: ConvertOptions & BaseVisitorConvertOptions): string; | ||
readonly config: TPluginConfig; | ||
@@ -37,4 +38,4 @@ readonly schema: GraphQLSchema; | ||
private handleAnonymouseOperation; | ||
FragmentDefinition: (node: FragmentDefinitionNode) => string; | ||
OperationDefinition: (node: OperationDefinitionNode) => string; | ||
FragmentDefinition(node: FragmentDefinitionNode): string; | ||
OperationDefinition(node: OperationDefinitionNode): string; | ||
} |
@@ -15,40 +15,23 @@ "use strict"; | ||
var autoBind = require("auto-bind"); | ||
var graphql_codegen_plugin_helpers_1 = require("graphql-codegen-plugin-helpers"); | ||
var scalars_1 = require("./scalars"); | ||
var utils_1 = require("./utils"); | ||
var variables_to_object_1 = require("./variables-to-object"); | ||
var naming_1 = require("./naming"); | ||
function getRootType(operation, schema) { | ||
switch (operation) { | ||
case 'query': | ||
return schema.getQueryType(); | ||
case 'mutation': | ||
return schema.getMutationType(); | ||
case 'subscription': | ||
return schema.getSubscriptionType(); | ||
} | ||
} | ||
var BaseDocumentsVisitor = /** @class */ (function () { | ||
function BaseDocumentsVisitor(rawConfig, additionalConfig, _schema, defaultScalars) { | ||
if (defaultScalars === void 0) { defaultScalars = scalars_1.DEFAULT_SCALARS; } | ||
var _this = this; | ||
this._schema = _schema; | ||
this._declarationBlockConfig = {}; | ||
this._unnamedCounter = 1; | ||
this.FragmentDefinition = function (node) { | ||
var fragmentRootType = _this._schema.getType(node.typeCondition.name.value); | ||
var selectionSet = _this._selectionSetToObject.createNext(fragmentRootType, node.selectionSet); | ||
return new utils_1.DeclarationBlock(_this._declarationBlockConfig) | ||
.export() | ||
.asKind('type') | ||
.withName(_this.convertName(node.name.value + 'Fragment', true)) | ||
.withContent(selectionSet.string).string; | ||
}; | ||
this.OperationDefinition = function (node) { | ||
var name = _this.handleAnonymouseOperation(node.name && node.name.value ? node.name.value : null); | ||
var operationRootType = _this._schema.getType(utils_1.toPascalCase(node.operation)); | ||
var selectionSet = _this._selectionSetToObject.createNext(operationRootType, node.selectionSet); | ||
var visitedOperationVariables = _this._variablesTransfomer.transform(node.variableDefinitions); | ||
var operationResult = new utils_1.DeclarationBlock(_this._declarationBlockConfig) | ||
.export() | ||
.asKind('type') | ||
.withName(_this.convertName(name + utils_1.toPascalCase(node.operation))) | ||
.withContent(selectionSet.string).string; | ||
var operationVariables = new utils_1.DeclarationBlock(_this._declarationBlockConfig) | ||
.export() | ||
.asKind('type') | ||
.withName(_this.convertName(name + utils_1.toPascalCase(node.operation) + 'Variables')) | ||
.withBlock(visitedOperationVariables).string; | ||
return [operationVariables, operationResult].filter(function (r) { return r; }).join('\n\n'); | ||
}; | ||
this._parsedConfig = __assign({ addTypename: !rawConfig.skipTypename, scalars: __assign({}, (defaultScalars || scalars_1.DEFAULT_SCALARS), (rawConfig.scalars || {})), convert: rawConfig.namingConvention ? graphql_codegen_plugin_helpers_1.resolveExternalModuleAndFn(rawConfig.namingConvention) : utils_1.toPascalCase, typesPrefix: rawConfig.typesPrefix || '' }, (additionalConfig || {})); | ||
this._parsedConfig = __assign({ addTypename: !rawConfig.skipTypename, scalars: __assign({}, (defaultScalars || scalars_1.DEFAULT_SCALARS), (rawConfig.scalars || {})), convert: naming_1.convertFactory(rawConfig), typesPrefix: rawConfig.typesPrefix || '' }, (additionalConfig || {})); | ||
autoBind(this); | ||
@@ -66,5 +49,5 @@ this._variablesTransfomer = new variables_to_object_1.OperationVariablesToObject(this.scalars, this.convertName); | ||
}; | ||
BaseDocumentsVisitor.prototype.convertName = function (name, addPrefix) { | ||
if (addPrefix === void 0) { addPrefix = true; } | ||
return (addPrefix ? this._parsedConfig.typesPrefix : '') + this._parsedConfig.convert(name); | ||
BaseDocumentsVisitor.prototype.convertName = function (node, options) { | ||
var useTypesPrefix = options && typeof options.useTypesPrefix === 'boolean' ? options.useTypesPrefix : true; | ||
return (useTypesPrefix ? this._parsedConfig.typesPrefix : '') + this._parsedConfig.convert(node, options); | ||
}; | ||
@@ -99,8 +82,48 @@ Object.defineProperty(BaseDocumentsVisitor.prototype, "config", { | ||
}); | ||
BaseDocumentsVisitor.prototype.handleAnonymouseOperation = function (name) { | ||
BaseDocumentsVisitor.prototype.handleAnonymouseOperation = function (node) { | ||
var name = node.name && node.name.value; | ||
if (name) { | ||
return this.convertName(name); | ||
return this.convertName(node, { | ||
useTypesPrefix: false | ||
}); | ||
} | ||
return this.convertName("Unnamed_" + this._unnamedCounter++ + "_"); | ||
return this.convertName(this._unnamedCounter++ + '', { | ||
prefix: 'Unnamed_', | ||
suffix: '_', | ||
useTypesPrefix: false | ||
}); | ||
}; | ||
BaseDocumentsVisitor.prototype.FragmentDefinition = function (node) { | ||
var fragmentRootType = this._schema.getType(node.typeCondition.name.value); | ||
var selectionSet = this._selectionSetToObject.createNext(fragmentRootType, node.selectionSet); | ||
return new utils_1.DeclarationBlock(this._declarationBlockConfig) | ||
.export() | ||
.asKind('type') | ||
.withName(this.convertName(node, { | ||
useTypesPrefix: true, | ||
suffix: 'Fragment' | ||
})) | ||
.withContent(selectionSet.string).string; | ||
}; | ||
BaseDocumentsVisitor.prototype.OperationDefinition = function (node) { | ||
var name = this.handleAnonymouseOperation(node); | ||
var operationRootType = getRootType(node.operation, this._schema); | ||
var selectionSet = this._selectionSetToObject.createNext(operationRootType, node.selectionSet); | ||
var visitedOperationVariables = this._variablesTransfomer.transform(node.variableDefinitions); | ||
var operationResult = new utils_1.DeclarationBlock(this._declarationBlockConfig) | ||
.export() | ||
.asKind('type') | ||
.withName(this.convertName(name, { | ||
suffix: utils_1.toPascalCase(node.operation) | ||
})) | ||
.withContent(selectionSet.string).string; | ||
var operationVariables = new utils_1.DeclarationBlock(this._declarationBlockConfig) | ||
.export() | ||
.asKind('type') | ||
.withName(this.convertName(name, { | ||
suffix: utils_1.toPascalCase(node.operation) + 'Variables' | ||
})) | ||
.withBlock(visitedOperationVariables).string; | ||
return [operationVariables, operationResult].filter(function (r) { return r; }).join('\n\n'); | ||
}; | ||
return BaseDocumentsVisitor; | ||
@@ -107,0 +130,0 @@ }()); |
@@ -1,2 +0,2 @@ | ||
import { ScalarsMap } from './types'; | ||
import { ScalarsMap, NamingConvention, ConvertFn, ConvertOptions } from './types'; | ||
import { DeclarationBlockConfig } from './utils'; | ||
@@ -7,2 +7,3 @@ import { NameNode, ListTypeNode, NamedTypeNode, FieldDefinitionNode, ObjectTypeDefinitionNode, GraphQLSchema } from 'graphql'; | ||
import { OperationVariablesToObject } from './variables-to-object'; | ||
import { BaseVisitorConvertOptions } from './base-visitor'; | ||
interface ParsedMapper { | ||
@@ -15,3 +16,3 @@ isExternal: boolean; | ||
scalars: ScalarsMap; | ||
convert: (str: string) => string; | ||
convert: ConvertFn; | ||
typesPrefix: string; | ||
@@ -29,3 +30,3 @@ contextType: string; | ||
scalars?: ScalarsMap; | ||
namingConvention?: string; | ||
namingConvention?: NamingConvention; | ||
typesPrefix?: string; | ||
@@ -40,2 +41,5 @@ } | ||
}; | ||
protected _collectedDirectiveResolvers: { | ||
[key: string]: string; | ||
}; | ||
protected _variablesTransfomer: OperationVariablesToObject; | ||
@@ -51,7 +55,8 @@ constructor(rawConfig: TRawConfig, additionalConfig: TPluginConfig, _schema: GraphQLSchema, defaultScalars?: ScalarsMap); | ||
protected buildMapperImport(source: string, types: string[]): string; | ||
convertName(name: any, addPrefix?: boolean): string; | ||
convertName(name: any, options?: ConvertOptions & BaseVisitorConvertOptions): string; | ||
setDeclarationBlockConfig(config: DeclarationBlockConfig): void; | ||
setVariablesTransformer(variablesTransfomer: OperationVariablesToObject): void; | ||
readonly rootResolver: string; | ||
getRootResolver(): string; | ||
protected formatRootResolver(schemaTypeName: string, resolverType: string): string; | ||
getAllDirectiveResolvers(): string; | ||
Name(node: NameNode): string; | ||
@@ -61,3 +66,3 @@ ListType(node: ListTypeNode): string; | ||
NonNullType(node: NonNullTypeNode): string; | ||
FieldDefinition(node: FieldDefinitionNode, key: string | number, parent: any): (parentName: any) => string; | ||
FieldDefinition(node: FieldDefinitionNode, key: string | number, parent: any): (parentName: string) => string; | ||
ObjectTypeDefinition(node: ObjectTypeDefinitionNode): string; | ||
@@ -64,0 +69,0 @@ UnionTypeDefinition(node: UnionTypeDefinitionNode, key: string | number, parent: any): string; |
@@ -14,3 +14,2 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var graphql_codegen_plugin_helpers_1 = require("graphql-codegen-plugin-helpers"); | ||
var autoBind = require("auto-bind"); | ||
@@ -21,2 +20,3 @@ var scalars_1 = require("./scalars"); | ||
var variables_to_object_1 = require("./variables-to-object"); | ||
var naming_1 = require("./naming"); | ||
var BaseResolversVisitor = /** @class */ (function () { | ||
@@ -28,3 +28,4 @@ function BaseResolversVisitor(rawConfig, additionalConfig, _schema, defaultScalars) { | ||
this._collectedResolvers = {}; | ||
this._parsedConfig = __assign({ scalars: __assign({}, (defaultScalars || scalars_1.DEFAULT_SCALARS), (rawConfig.scalars || {})), convert: rawConfig.namingConvention ? graphql_codegen_plugin_helpers_1.resolveExternalModuleAndFn(rawConfig.namingConvention) : utils_1.toPascalCase, typesPrefix: rawConfig.typesPrefix || '', contextType: rawConfig.contextType || 'any', mappers: this.transformMappers(rawConfig.mappers || {}) }, (additionalConfig || {})); | ||
this._collectedDirectiveResolvers = {}; | ||
this._parsedConfig = __assign({ scalars: __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); | ||
@@ -102,5 +103,5 @@ this._variablesTransfomer = new variables_to_object_1.OperationVariablesToObject(this.scalars, this.convertName); | ||
}; | ||
BaseResolversVisitor.prototype.convertName = function (name, addPrefix) { | ||
if (addPrefix === void 0) { addPrefix = true; } | ||
return (addPrefix ? this.config.typesPrefix : '') + this.config.convert(name); | ||
BaseResolversVisitor.prototype.convertName = function (name, options) { | ||
var useTypesPrefix = options && typeof options.useTypesPrefix === 'boolean' ? options.useTypesPrefix : true; | ||
return (useTypesPrefix ? this.config.typesPrefix : '') + this.config.convert(name, options); | ||
}; | ||
@@ -113,22 +114,31 @@ BaseResolversVisitor.prototype.setDeclarationBlockConfig = function (config) { | ||
}; | ||
Object.defineProperty(BaseResolversVisitor.prototype, "rootResolver", { | ||
get: function () { | ||
var _this = this; | ||
return new utils_1.DeclarationBlock(this._declarationBlockConfig) | ||
.export() | ||
.asKind('interface') | ||
.withName(this.convertName('ResolversRoot')) | ||
.withBlock(Object.keys(this._collectedResolvers) | ||
.map(function (schemaTypeName) { | ||
var resolverType = _this._collectedResolvers[schemaTypeName]; | ||
return utils_1.indent(_this.formatRootResolver(schemaTypeName, resolverType)); | ||
}) | ||
.join('\n')).string; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
BaseResolversVisitor.prototype.getRootResolver = function () { | ||
var _this = this; | ||
return new utils_1.DeclarationBlock(this._declarationBlockConfig) | ||
.export() | ||
.asKind('type') | ||
.withName(this.convertName('IResolvers'), "<Context = " + this.config.contextType + ">") | ||
.withBlock(Object.keys(this._collectedResolvers) | ||
.map(function (schemaTypeName) { | ||
var resolverType = _this._collectedResolvers[schemaTypeName]; | ||
return utils_1.indent(_this.formatRootResolver(schemaTypeName, resolverType)); | ||
}) | ||
.join('\n')).string; | ||
}; | ||
BaseResolversVisitor.prototype.formatRootResolver = function (schemaTypeName, resolverType) { | ||
return schemaTypeName + "?: " + resolverType + ","; | ||
}; | ||
BaseResolversVisitor.prototype.getAllDirectiveResolvers = function () { | ||
var _this = this; | ||
return new utils_1.DeclarationBlock(this._declarationBlockConfig) | ||
.export() | ||
.asKind('type') | ||
.withName(this.convertName('IDirectiveResolvers'), "<Context = " + this.config.contextType + ">") | ||
.withBlock(Object.keys(this._collectedDirectiveResolvers) | ||
.map(function (schemaTypeName) { | ||
var resolverType = _this._collectedDirectiveResolvers[schemaTypeName]; | ||
return utils_1.indent(_this.formatRootResolver(schemaTypeName, resolverType)); | ||
}) | ||
.join('\n')).string; | ||
}; | ||
BaseResolversVisitor.prototype.Name = function (node) { | ||
@@ -142,4 +152,3 @@ return node.value; | ||
BaseResolversVisitor.prototype.NamedType = function (node) { | ||
var asString = node.name; | ||
var type = this.config.scalars[asString] || this.convertName(asString); | ||
var type = this.config.scalars[node.name] || this.convertName(node); | ||
return "" + type; | ||
@@ -162,7 +171,17 @@ }; | ||
var isSubscriptionType = subscriptionType && subscriptionType.name === parentName; | ||
return utils_1.indent(node.name + "?: " + (isSubscriptionType ? 'SubscriptionResolver' : 'Resolver') + "<" + mappedType + ", ParentType, Context" + (hasArguments ? ", " + (_this.convertName(parentName, true) + _this.convertName(node.name, false) + 'Args') : '') + ">,"); | ||
return utils_1.indent(node.name + "?: " + (isSubscriptionType ? 'SubscriptionResolver' : 'Resolver') + "<" + mappedType + ", ParentType, Context" + (hasArguments | ||
? ", " + (_this.convertName(parentName, { | ||
useTypesPrefix: true | ||
}) + | ||
_this.convertName(node.name, { | ||
useTypesPrefix: false | ||
}) + | ||
'Args') | ||
: '') + ">,"); | ||
}; | ||
}; | ||
BaseResolversVisitor.prototype.ObjectTypeDefinition = function (node) { | ||
var name = this.convertName(node.name + 'Resolvers'); | ||
var name = this.convertName(node, { | ||
suffix: 'Resolvers' | ||
}); | ||
var type = null; | ||
@@ -173,3 +192,3 @@ if (this.config.mappers[node.name]) { | ||
else { | ||
type = this.config.scalars[node.name] || this.convertName(node.name); | ||
type = this.config.scalars[node.name] || this.convertName(node); | ||
} | ||
@@ -181,3 +200,3 @@ var block = new utils_1.DeclarationBlock(this._declarationBlockConfig) | ||
.withBlock(node.fields.map(function (f) { return f(node.name); }).join('\n')); | ||
this._collectedResolvers[node.name] = name; | ||
this._collectedResolvers[node.name] = name + '<Context>'; | ||
return block.string; | ||
@@ -187,6 +206,8 @@ }; | ||
var _this = this; | ||
var name = this.convertName(node.name + 'Resolvers'); | ||
var name = this.convertName(node, { | ||
suffix: 'Resolvers' | ||
}); | ||
var originalNode = parent[key]; | ||
var possibleTypes = originalNode.types | ||
.map(function (node) { return _this.convertName(node.name.value); }) | ||
.map(function (node) { return _this.convertName(node); }) | ||
.map(function (f) { return "'" + f + "'"; }) | ||
@@ -202,11 +223,16 @@ .join(' | '); | ||
BaseResolversVisitor.prototype.ScalarTypeDefinition = function (node) { | ||
var baseName = this.convertName(node.name); | ||
var baseName = this.convertName(node); | ||
this._collectedResolvers[node.name] = 'GraphQLScalarType'; | ||
return new utils_1.DeclarationBlock(this._declarationBlockConfig) | ||
.export() | ||
.asKind('interface') | ||
.withName(this.convertName(node.name + 'ScalarConfig'), " extends GraphQLScalarTypeConfig<" + baseName + ", any>") | ||
.withName(this.convertName(node, { | ||
suffix: 'ScalarConfig' | ||
}), " extends GraphQLScalarTypeConfig<" + baseName + ", any>") | ||
.withBlock(utils_1.indent("name: '" + node.name + "'")).string; | ||
}; | ||
BaseResolversVisitor.prototype.DirectiveDefinition = function (node) { | ||
var directiveName = this.convertName(node.name + 'DirectiveResolver'); | ||
var directiveName = this.convertName(node, { | ||
suffix: 'DirectiveResolver' | ||
}); | ||
var hasArguments = node.arguments && node.arguments.length > 0; | ||
@@ -216,10 +242,13 @@ var directiveArgs = hasArguments | ||
: ''; | ||
this._collectedDirectiveResolvers[node.name] = directiveName + '<any, any, Context>'; | ||
return new utils_1.DeclarationBlock(this._declarationBlockConfig) | ||
.export() | ||
.asKind('type') | ||
.withName(directiveName, '<Result>') | ||
.withContent("DirectiveResolverFn<Result, { " + directiveArgs + " }, " + this.config.contextType + ">").string; | ||
.withName(directiveName, "<Result, Parent, Context = " + this.config.contextType + ", Args = { " + directiveArgs + " }>") | ||
.withContent("DirectiveResolverFn<Result, Parent, Context, Args>").string; | ||
}; | ||
BaseResolversVisitor.prototype.InterfaceTypeDefinition = function (node) { | ||
var name = this.convertName(node.name + 'Resolvers'); | ||
var name = this.convertName(node, { | ||
suffix: 'Resolvers' | ||
}); | ||
var allTypesMap = this._schema.getTypeMap(); | ||
@@ -226,0 +255,0 @@ var implementingTypes = []; |
@@ -1,10 +0,10 @@ | ||
import { ScalarsMap, EnumValuesMap } from './types'; | ||
import { ScalarsMap, NamingConvention, ConvertFn, ConvertOptions } from './types'; | ||
import { DeclarationBlockConfig } from './utils'; | ||
import { NamedTypeNode, ListTypeNode, NonNullTypeNode, DirectiveDefinitionNode, NameNode, InputObjectTypeDefinitionNode, InputValueDefinitionNode, EnumTypeDefinitionNode, ScalarTypeDefinitionNode } from 'graphql'; | ||
import { FieldDefinitionNode, UnionTypeDefinitionNode, ObjectTypeDefinitionNode, InterfaceTypeDefinitionNode, EnumValueDefinitionNode } from 'graphql/language/ast'; | ||
import { OperationVariablesToObject } from './variables-to-object'; | ||
import { ASTNode } from 'graphql'; | ||
export interface BaseVisitorConvertOptions { | ||
useTypesPrefix?: boolean; | ||
} | ||
export interface ParsedConfig { | ||
scalars: ScalarsMap; | ||
enumValues: EnumValuesMap; | ||
convert: (str: string) => string; | ||
convert: ConvertFn; | ||
typesPrefix: string; | ||
@@ -14,4 +14,3 @@ } | ||
scalars?: ScalarsMap; | ||
enumValues?: EnumValuesMap; | ||
namingConvention?: string; | ||
namingConvention?: NamingConvention; | ||
typesPrefix?: string; | ||
@@ -21,25 +20,7 @@ } | ||
protected _parsedConfig: TPluginConfig; | ||
protected _argumentsTransformer: OperationVariablesToObject; | ||
protected _declarationBlockConfig: DeclarationBlockConfig; | ||
constructor(rawConfig: TRawConfig, additionalConfig: TPluginConfig, defaultScalars?: ScalarsMap); | ||
setDeclarationBlockConfig(config: DeclarationBlockConfig): void; | ||
setArgumentsTransformer(argumentsTransfomer: OperationVariablesToObject): void; | ||
readonly config: TPluginConfig; | ||
readonly scalars: ScalarsMap; | ||
convertName(name: any, addPrefix?: boolean): string; | ||
DirectiveDefinition(node: DirectiveDefinitionNode): string; | ||
NamedType(node: NamedTypeNode): string; | ||
ListType(node: ListTypeNode): string; | ||
protected wrapWithListType(str: string): string; | ||
NonNullType(node: NonNullTypeNode): string; | ||
InputObjectTypeDefinition(node: InputObjectTypeDefinitionNode): string; | ||
InputValueDefinition(node: InputValueDefinitionNode): string; | ||
Name(node: NameNode): string; | ||
FieldDefinition(node: FieldDefinitionNode): string; | ||
UnionTypeDefinition(node: UnionTypeDefinitionNode, key: string | number, parent: any): string; | ||
ObjectTypeDefinition(node: ObjectTypeDefinitionNode, key: number | string, parent: any): string; | ||
InterfaceTypeDefinition(node: InterfaceTypeDefinitionNode): string; | ||
ScalarTypeDefinition(node: ScalarTypeDefinitionNode): string; | ||
EnumTypeDefinition(node: EnumTypeDefinitionNode): string; | ||
protected buildEnumValuesBlock(values: ReadonlyArray<EnumValueDefinitionNode>): string; | ||
convertName(node: ASTNode | string, options?: BaseVisitorConvertOptions & ConvertOptions): string; | ||
} |
@@ -14,7 +14,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var utils_1 = require("./utils"); | ||
var graphql_codegen_plugin_helpers_1 = require("graphql-codegen-plugin-helpers"); | ||
var autoBind = require("auto-bind"); | ||
var variables_to_object_1 = require("./variables-to-object"); | ||
var scalars_1 = require("./scalars"); | ||
var naming_1 = require("./naming"); | ||
var BaseVisitor = /** @class */ (function () { | ||
@@ -24,12 +22,5 @@ function BaseVisitor(rawConfig, additionalConfig, defaultScalars) { | ||
this._declarationBlockConfig = {}; | ||
this._parsedConfig = __assign({ scalars: __assign({}, (defaultScalars || scalars_1.DEFAULT_SCALARS), (rawConfig.scalars || {})), enumValues: rawConfig.enumValues || {}, convert: rawConfig.namingConvention ? graphql_codegen_plugin_helpers_1.resolveExternalModuleAndFn(rawConfig.namingConvention) : utils_1.toPascalCase, typesPrefix: rawConfig.typesPrefix || '' }, (additionalConfig || {})); | ||
this._parsedConfig = __assign({ scalars: __assign({}, (defaultScalars || scalars_1.DEFAULT_SCALARS), (rawConfig.scalars || {})), convert: naming_1.convertFactory(rawConfig), typesPrefix: rawConfig.typesPrefix || '' }, (additionalConfig || {})); | ||
autoBind(this); | ||
this._argumentsTransformer = new variables_to_object_1.OperationVariablesToObject(this.scalars, this.convertName); | ||
} | ||
BaseVisitor.prototype.setDeclarationBlockConfig = function (config) { | ||
this._declarationBlockConfig = config; | ||
}; | ||
BaseVisitor.prototype.setArgumentsTransformer = function (argumentsTransfomer) { | ||
this._argumentsTransformer = argumentsTransfomer; | ||
}; | ||
Object.defineProperty(BaseVisitor.prototype, "config", { | ||
@@ -49,105 +40,6 @@ get: function () { | ||
}); | ||
BaseVisitor.prototype.convertName = function (name, addPrefix) { | ||
if (addPrefix === void 0) { addPrefix = true; } | ||
return (addPrefix ? this.config.typesPrefix : '') + this.config.convert(name); | ||
BaseVisitor.prototype.convertName = function (node, options) { | ||
var useTypesPrefix = typeof (options && options.useTypesPrefix) === 'boolean' ? options.useTypesPrefix : true; | ||
return (useTypesPrefix ? this.config.typesPrefix : '') + this.config.convert(node, options); | ||
}; | ||
BaseVisitor.prototype.DirectiveDefinition = function (node) { | ||
return ''; | ||
}; | ||
BaseVisitor.prototype.NamedType = function (node) { | ||
var asString = node.name; | ||
var type = this.scalars[asString] || this.convertName(asString); | ||
return type; | ||
}; | ||
BaseVisitor.prototype.ListType = function (node) { | ||
var asString = node.type; | ||
return this.wrapWithListType(asString); | ||
}; | ||
BaseVisitor.prototype.wrapWithListType = function (str) { | ||
return "Array<" + str + ">"; | ||
}; | ||
BaseVisitor.prototype.NonNullType = function (node) { | ||
var asString = node.type; | ||
return asString; | ||
}; | ||
BaseVisitor.prototype.InputObjectTypeDefinition = function (node) { | ||
return new utils_1.DeclarationBlock(this._declarationBlockConfig) | ||
.export() | ||
.asKind('type') | ||
.withName(this.convertName(node.name)) | ||
.withBlock(node.fields.join('\n')).string; | ||
}; | ||
BaseVisitor.prototype.InputValueDefinition = function (node) { | ||
return utils_1.indent(node.name + ": " + node.type + ","); | ||
}; | ||
BaseVisitor.prototype.Name = function (node) { | ||
return node.value; | ||
}; | ||
BaseVisitor.prototype.FieldDefinition = function (node) { | ||
var typeString = node.type; | ||
return utils_1.indent(node.name + ": " + typeString + ","); | ||
}; | ||
BaseVisitor.prototype.UnionTypeDefinition = function (node, key, parent) { | ||
var _this = this; | ||
var originalNode = parent[key]; | ||
var possibleTypes = originalNode.types.map(function (t) { return _this.convertName(t.name.value); }).join(' | '); | ||
return new utils_1.DeclarationBlock(this._declarationBlockConfig) | ||
.export() | ||
.asKind('type') | ||
.withName(this.convertName(node.name)) | ||
.withContent(possibleTypes).string; | ||
}; | ||
BaseVisitor.prototype.ObjectTypeDefinition = function (node, key, parent) { | ||
var _this = this; | ||
var originalNode = parent[key]; | ||
var interfaces = originalNode.interfaces && node.interfaces.length > 0 | ||
? originalNode.interfaces.map(function (i) { return _this.convertName(i.name.value); }).join(' & ') + ' & ' | ||
: ''; | ||
var typeDefinition = new utils_1.DeclarationBlock(this._declarationBlockConfig) | ||
.export() | ||
.asKind('type') | ||
.withName(this.convertName(node.name)) | ||
.withContent(interfaces) | ||
.withBlock(node.fields.join('\n')).string; | ||
var original = parent[key]; | ||
var fieldsWithArguments = original.fields.filter(function (field) { return field.arguments && field.arguments.length > 0; }); | ||
var fieldsArguments = fieldsWithArguments.map(function (field) { | ||
var name = original.name.value + _this.convertName(field.name.value, false) + 'Args'; | ||
return new utils_1.DeclarationBlock(_this._declarationBlockConfig) | ||
.export() | ||
.asKind('type') | ||
.withName(_this.convertName(name)) | ||
.withBlock(_this._argumentsTransformer.transform(field.arguments)).string; | ||
}); | ||
return [typeDefinition].concat(fieldsArguments).filter(function (f) { return f; }).join('\n\n'); | ||
}; | ||
BaseVisitor.prototype.InterfaceTypeDefinition = function (node) { | ||
return new utils_1.DeclarationBlock(this._declarationBlockConfig) | ||
.export() | ||
.asKind('type') | ||
.withName(this.convertName(node.name)) | ||
.withBlock(node.fields.join('\n')).string; | ||
}; | ||
BaseVisitor.prototype.ScalarTypeDefinition = function (node) { | ||
return new utils_1.DeclarationBlock(this._declarationBlockConfig) | ||
.export() | ||
.asKind('type') | ||
.withName(this.convertName(node.name)) | ||
.withContent(this.config.scalars[node.name] || 'any').string; | ||
}; | ||
BaseVisitor.prototype.EnumTypeDefinition = function (node) { | ||
return new utils_1.DeclarationBlock(this._declarationBlockConfig) | ||
.export() | ||
.asKind('enum') | ||
.withName(this.convertName(node.name)) | ||
.withBlock(this.buildEnumValuesBlock(node.values)).string; | ||
}; | ||
BaseVisitor.prototype.buildEnumValuesBlock = function (values) { | ||
var _this = this; | ||
return values | ||
.map(function (enumOption) { | ||
return utils_1.indent("" + _this.convertName(enumOption.name) + _this._declarationBlockConfig.enumNameValueSeparator + " " + utils_1.wrapWithSingleQuotes(_this.config.enumValues[enumOption.name] || enumOption.name)); | ||
}) | ||
.join(', \n'); | ||
}; | ||
return BaseVisitor; | ||
@@ -154,0 +46,0 @@ }()); |
@@ -5,5 +5,7 @@ export * from './types'; | ||
export * from './base-visitor'; | ||
export * from './base-types-visitor'; | ||
export * from './base-documents-visitor'; | ||
export * from './base-resolvers-visitor'; | ||
export * from './client-side-base-visitor'; | ||
export * from './variables-to-object'; | ||
export * from './selection-set-to-object'; |
@@ -9,6 +9,8 @@ "use strict"; | ||
__export(require("./base-visitor")); | ||
__export(require("./base-types-visitor")); | ||
__export(require("./base-documents-visitor")); | ||
__export(require("./base-resolvers-visitor")); | ||
__export(require("./client-side-base-visitor")); | ||
__export(require("./variables-to-object")); | ||
__export(require("./selection-set-to-object")); | ||
//# sourceMappingURL=index.js.map |
import { SelectionSetNode, FieldNode, FragmentSpreadNode, InlineFragmentNode, GraphQLNamedType, GraphQLSchema } from 'graphql'; | ||
import { ScalarsMap, ConvertNameFn } from './types'; | ||
import { GraphQLObjectType, GraphQLNonNull, GraphQLList } from 'graphql'; | ||
import { BaseVisitorConvertOptions } from './base-visitor'; | ||
export declare type PrimitiveField = string; | ||
@@ -22,3 +23,3 @@ export declare type PrimitiveAliasedFields = { | ||
protected _schema: GraphQLSchema; | ||
protected _convertName: ConvertNameFn; | ||
protected _convertName: ConvertNameFn<BaseVisitorConvertOptions>; | ||
protected _addTypename: boolean; | ||
@@ -33,3 +34,3 @@ protected _parentSchemaType?: GraphQLNamedType; | ||
protected _queriedForTypename: boolean; | ||
constructor(_scalars: ScalarsMap, _schema: GraphQLSchema, _convertName: ConvertNameFn, _addTypename: boolean, _parentSchemaType?: GraphQLNamedType, _selectionSet?: SelectionSetNode); | ||
constructor(_scalars: ScalarsMap, _schema: GraphQLSchema, _convertName: ConvertNameFn<BaseVisitorConvertOptions>, _addTypename: boolean, _parentSchemaType?: GraphQLNamedType, _selectionSet?: SelectionSetNode); | ||
createNext(parentSchemaType: GraphQLNamedType, selectionSet: SelectionSetNode): SelectionSetToObject; | ||
@@ -36,0 +37,0 @@ protected wrapTypeWithModifiers(baseType: string, type: GraphQLObjectType | GraphQLNonNull<GraphQLObjectType> | GraphQLList<GraphQLObjectType>): string; |
@@ -5,2 +5,14 @@ "use strict"; | ||
var utils_1 = require("./utils"); | ||
function isMetadataFieldName(name) { | ||
return ['__schema', '__type'].includes(name); | ||
} | ||
function isRootType(type, schema) { | ||
return (graphql_1.isEqualType(type, schema.getQueryType()) || | ||
graphql_1.isEqualType(type, schema.getMutationType()) || | ||
graphql_1.isEqualType(type, schema.getSubscriptionType())); | ||
} | ||
var metadataFieldMap = { | ||
__schema: graphql_1.SchemaMetaFieldDef, | ||
__type: graphql_1.TypeMetaFieldDef | ||
}; | ||
var SelectionSetToObject = /** @class */ (function () { | ||
@@ -33,3 +45,9 @@ function SelectionSetToObject(_scalars, _schema, _convertName, _addTypename, _parentSchemaType, _selectionSet) { | ||
if (graphql_1.isObjectType(this._parentSchemaType) || graphql_1.isInterfaceType(this._parentSchemaType)) { | ||
var schemaField = this._parentSchemaType.getFields()[field.name.value]; | ||
var schemaField = void 0; | ||
if (isRootType(this._parentSchemaType, this._schema) && isMetadataFieldName(field.name.value)) { | ||
schemaField = metadataFieldMap[field.name.value]; | ||
} | ||
else { | ||
schemaField = this._parentSchemaType.getFields()[field.name.value]; | ||
} | ||
var rawType = schemaField.type; | ||
@@ -92,3 +110,5 @@ var baseType = utils_1.getBaseType(rawType); | ||
} | ||
var parentName = this._convertName(this._parentSchemaType.name, true); | ||
var parentName = this._convertName(this._parentSchemaType.name, { | ||
useTypesPrefix: true | ||
}); | ||
var typeName = this._addTypename || this._queriedForTypename ? this.buildTypeNameField() : null; | ||
@@ -157,3 +177,8 @@ var baseFields = this.buildPrimitiveFields(parentName, this._primitiveFields); | ||
} | ||
return utils_1.quoteIfNeeded(fragmentsSpread.map(function (fragmentName) { return _this._convertName(fragmentName + 'Fragment', true); }), ' & '); | ||
return utils_1.quoteIfNeeded(fragmentsSpread.map(function (fragmentName) { | ||
return _this._convertName(fragmentName, { | ||
suffix: 'Fragment', | ||
useTypesPrefix: true | ||
}); | ||
}), ' & '); | ||
}; | ||
@@ -160,0 +185,0 @@ return SelectionSetToObject; |
@@ -0,1 +1,2 @@ | ||
import { ASTNode } from 'graphql'; | ||
export declare type ScalarsMap = { | ||
@@ -7,2 +8,13 @@ [name: string]: string; | ||
}; | ||
export declare type ConvertNameFn = (name: any, addPrefix: boolean) => string; | ||
export declare type ConvertNameFn<T = {}> = ConvertFn<T>; | ||
export interface ConvertOptions { | ||
prefix?: string; | ||
suffix?: string; | ||
} | ||
export declare type ConvertFn<T = {}> = (node: ASTNode | string, options?: ConvertOptions & T) => string; | ||
export declare type NamingConventionResolvePath = string; | ||
export declare type NamingConvention = string | NamingConventionMap; | ||
export interface NamingConventionMap { | ||
enumValues?: 'keep' | NamingConventionResolvePath | Function; | ||
typeNames?: 'keep' | NamingConventionResolvePath | Function; | ||
} |
import { NameNode, TypeNode, NamedTypeNode, GraphQLObjectType, GraphQLNonNull, GraphQLList, GraphQLOutputType, GraphQLNamedType } from 'graphql'; | ||
export declare const getConfigValue: <T = any>(value: T, defaultValue: T) => T; | ||
export declare function getBaseType(type: GraphQLOutputType): GraphQLNamedType; | ||
@@ -3,0 +4,0 @@ export declare function quoteIfNeeded(array: string[], joinWith?: string): string; |
@@ -19,2 +19,8 @@ "use strict"; | ||
} | ||
exports.getConfigValue = function (value, defaultValue) { | ||
if (value === null || value === undefined) { | ||
return defaultValue; | ||
} | ||
return value; | ||
}; | ||
function getBaseType(type) { | ||
@@ -21,0 +27,0 @@ if (isWrapperType(type)) { |
import { TypeNode, VariableNode, NameNode, ValueNode } from 'graphql'; | ||
import { ScalarsMap, ConvertNameFn } from './types'; | ||
import { BaseVisitorConvertOptions } from './base-visitor'; | ||
export interface InterfaceOrVariable { | ||
@@ -11,4 +12,4 @@ name?: NameNode; | ||
protected _scalars: ScalarsMap; | ||
protected _convertName: ConvertNameFn; | ||
constructor(_scalars: ScalarsMap, _convertName: ConvertNameFn); | ||
protected _convertName: ConvertNameFn<BaseVisitorConvertOptions>; | ||
constructor(_scalars: ScalarsMap, _convertName: ConvertNameFn<BaseVisitorConvertOptions>); | ||
getName<TDefinitionType extends InterfaceOrVariable>(node: TDefinitionType): string; | ||
@@ -15,0 +16,0 @@ transform<TDefinitionType extends InterfaceOrVariable>(variablesNode: ReadonlyArray<TDefinitionType>): string; |
@@ -34,3 +34,6 @@ "use strict"; | ||
var typeName = typeof baseType === 'string' ? baseType : baseType.name.value; | ||
var typeValue = this._scalars[typeName] ? this._scalars[typeName] : this._convertName(typeName, true); | ||
var typeValue = this._scalars[typeName] || | ||
this._convertName(baseType, { | ||
useTypesPrefix: true | ||
}); | ||
var fieldName = this.getName(variable); | ||
@@ -37,0 +40,0 @@ var fieldType = this.wrapAstTypeWithModifiers(typeValue, variable.type); |
{ | ||
"name": "graphql-codegen-visitor-plugin-common", | ||
"version": "0.19.0-alpha.35980e3e", | ||
"version": "0.19.0-alpha.5634c385", | ||
"license": "MIT", | ||
@@ -10,3 +10,5 @@ "scripts": { | ||
"auto-bind": "2.0.0", | ||
"graphql-codegen-core": "0.19.0-alpha.35980e3e" | ||
"dependency-graph": "0.8.0", | ||
"graphql-codegen-core": "0.19.0-alpha.5634c385", | ||
"graphql-tag": "2.10.1" | ||
}, | ||
@@ -13,0 +15,0 @@ "peerDependencies": { |
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
112998
363
38
1614
5
+ Addeddependency-graph@0.8.0
+ Addedgraphql-tag@2.10.1
+ Addeddependency-graph@0.8.0(transitive)
+ Addedgraphql-codegen-core@0.19.0-alpha.5634c385(transitive)
- Removedgraphql-codegen-core@0.19.0-alpha.35980e3e(transitive)