@graphql-codegen/visitor-plugin-common
Advanced tools
Comparing version 1.0.1-alpha-d343ec0b.18 to 1.0.1
@@ -1,2 +0,2 @@ | ||
import { DirectiveDefinitionNode, EnumTypeDefinitionNode, EnumValueDefinitionNode, FieldDefinitionNode, GraphQLSchema, InputObjectTypeDefinitionNode, InputValueDefinitionNode, InterfaceTypeDefinitionNode, ListTypeNode, NamedTypeNode, NameNode, NonNullTypeNode, ObjectTypeDefinitionNode, ScalarTypeDefinitionNode, UnionTypeDefinitionNode } from 'graphql'; | ||
import { DirectiveDefinitionNode, EnumTypeDefinitionNode, EnumValueDefinitionNode, FieldDefinitionNode, GraphQLSchema, InputObjectTypeDefinitionNode, InputValueDefinitionNode, InterfaceTypeDefinitionNode, ListTypeNode, NamedTypeNode, NameNode, NonNullTypeNode, ObjectTypeDefinitionNode, ScalarTypeDefinitionNode, UnionTypeDefinitionNode, StringValueNode } from 'graphql'; | ||
import { BaseVisitor, ParsedConfig, RawConfig } from './base-visitor'; | ||
@@ -52,2 +52,3 @@ import { EnumValuesMap, ScalarsMap } from './types'; | ||
EnumTypeDefinition(node: EnumTypeDefinitionNode): string; | ||
StringValue(node: StringValueNode): string; | ||
protected buildEnumValuesBlock(typeName: string, values: ReadonlyArray<EnumValueDefinitionNode>): string; | ||
@@ -54,0 +55,0 @@ DirectiveDefinition(node: DirectiveDefinitionNode): string; |
@@ -17,3 +17,5 @@ "use strict"; | ||
const scalarValue = this.config.scalars[scalarName]; | ||
return utils_1.indent(`${scalarName}: ${scalarValue},`); | ||
const scalarType = this._schema.getType(scalarName); | ||
const comment = scalarType && scalarType.astNode && scalarType.description ? utils_1.transformComment(scalarType.description, 1) : ''; | ||
return comment + utils_1.indent(`${scalarName}: ${scalarValue},`); | ||
}); | ||
@@ -24,2 +26,3 @@ return new utils_1.DeclarationBlock(this._declarationBlockConfig) | ||
.withName('Scalars') | ||
.withComment('All built-in and custom scalars, mapped to their actual values') | ||
.withBlock(allScalars.join('\n')).string; | ||
@@ -42,6 +45,8 @@ } | ||
.withName(this.convertName(node)) | ||
.withComment(node.description) | ||
.withBlock(node.fields.join('\n')).string; | ||
} | ||
InputValueDefinition(node) { | ||
return utils_1.indent(`${node.name}: ${node.type},`); | ||
const comment = utils_1.transformComment(node.description, 1); | ||
return comment + utils_1.indent(`${node.name}: ${node.type},`); | ||
} | ||
@@ -53,3 +58,4 @@ Name(node) { | ||
const typeString = node.type; | ||
return utils_1.indent(`${node.name}: ${typeString},`); | ||
const comment = utils_1.transformComment(node.description, 1); | ||
return comment + utils_1.indent(`${node.name}: ${typeString},`); | ||
} | ||
@@ -63,2 +69,3 @@ UnionTypeDefinition(node, key, parent) { | ||
.withName(this.convertName(node)) | ||
.withComment(node.description) | ||
.withContent(possibleTypes).string; | ||
@@ -74,2 +81,3 @@ } | ||
.withContent(interfaces) | ||
.withComment(node.description) | ||
.withBlock(node.fields.join('\n')).string; | ||
@@ -88,2 +96,3 @@ const original = parent[key]; | ||
.withName(this.convertName(name)) | ||
.withComment(node.description) | ||
.withBlock(this._argumentsTransformer.transform(field.arguments)).string; | ||
@@ -98,2 +107,3 @@ }); | ||
.withName(this.convertName(node)) | ||
.withComment(node.description) | ||
.withBlock(node.fields.join('\n')).string; | ||
@@ -134,4 +144,9 @@ } | ||
.withName(this.convertName(node)) | ||
.withComment(node.description) | ||
.withBlock(this.buildEnumValuesBlock(enumName, node.values)).string; | ||
} | ||
// We are using it in order to transform "description" field | ||
StringValue(node) { | ||
return node.value; | ||
} | ||
buildEnumValuesBlock(typeName, values) { | ||
@@ -141,2 +156,3 @@ return values | ||
const optionName = this.convertName(enumOption); | ||
const comment = utils_1.transformComment(enumOption.description, 1); | ||
let enumValue = enumOption.name; | ||
@@ -146,3 +162,3 @@ if (this.config.enumValues[typeName] && typeof this.config.enumValues[typeName] === 'object' && this.config.enumValues[typeName][enumValue]) { | ||
} | ||
return utils_1.indent(`${optionName}${this._declarationBlockConfig.enumNameValueSeparator} ${utils_1.wrapWithSingleQuotes(enumValue)}`); | ||
return comment + utils_1.indent(`${optionName}${this._declarationBlockConfig.enumNameValueSeparator} ${utils_1.wrapWithSingleQuotes(enumValue)}`); | ||
}) | ||
@@ -149,0 +165,0 @@ .join(',\n'); |
@@ -15,2 +15,3 @@ import { NameNode, TypeNode, NamedTypeNode, GraphQLObjectType, GraphQLNonNull, GraphQLList, GraphQLOutputType, GraphQLNamedType, GraphQLSchema } from 'graphql'; | ||
} | ||
export declare function transformComment(comment: string, indentLevel?: number): string; | ||
export declare class DeclarationBlock { | ||
@@ -25,5 +26,7 @@ private _config; | ||
_nameGenerics: any; | ||
_comment: any; | ||
constructor(_config: DeclarationBlockConfig); | ||
export(exp?: boolean): DeclarationBlock; | ||
asKind(kind: any): DeclarationBlock; | ||
asKind(kind: string): DeclarationBlock; | ||
withComment(comment: string | null): DeclarationBlock; | ||
withMethodCall(methodName: string): DeclarationBlock; | ||
@@ -30,0 +33,0 @@ withBlock(block: string): DeclarationBlock; |
@@ -51,2 +51,19 @@ "use strict"; | ||
exports.indent = indent; | ||
function transformComment(comment, indentLevel = 0) { | ||
if (!comment || comment === '') { | ||
return ''; | ||
} | ||
const lines = comment.split('\n'); | ||
return lines | ||
.map((line, index) => { | ||
const isLast = lines.length === index + 1; | ||
const isFirst = index === 0; | ||
if (isFirst && isLast) { | ||
return indent(`/** ${comment} */\n`, indentLevel); | ||
} | ||
return indent(`${isFirst ? '/** ' : ' * '}${line}${isLast ? '\n */\n' : ''}`, indentLevel); | ||
}) | ||
.join('\n'); | ||
} | ||
exports.transformComment = transformComment; | ||
class DeclarationBlock { | ||
@@ -62,2 +79,3 @@ constructor(_config) { | ||
this._nameGenerics = null; | ||
this._comment = null; | ||
this._config = Object.assign({ blockWrapper: '', blockTransformer: block => block, enumNameValueSeparator: ':' }, this._config); | ||
@@ -73,2 +91,8 @@ } | ||
} | ||
withComment(comment) { | ||
if (comment) { | ||
this._comment = transformComment(comment, 0); | ||
} | ||
return this; | ||
} | ||
withMethodCall(methodName) { | ||
@@ -127,3 +151,3 @@ this._methodName = methodName; | ||
} | ||
return result + (this._kind === 'interface' || this._kind === 'enum' ? '' : ';') + '\n'; | ||
return (this._comment ? this._comment : '') + result + (this._kind === 'interface' || this._kind === 'enum' ? '' : ';') + '\n'; | ||
} | ||
@@ -130,0 +154,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import { DirectiveDefinitionNode, EnumTypeDefinitionNode, EnumValueDefinitionNode, FieldDefinitionNode, GraphQLSchema, InputObjectTypeDefinitionNode, InputValueDefinitionNode, InterfaceTypeDefinitionNode, ListTypeNode, NamedTypeNode, NameNode, NonNullTypeNode, ObjectTypeDefinitionNode, ScalarTypeDefinitionNode, UnionTypeDefinitionNode } from 'graphql'; | ||
import { DirectiveDefinitionNode, EnumTypeDefinitionNode, EnumValueDefinitionNode, FieldDefinitionNode, GraphQLSchema, InputObjectTypeDefinitionNode, InputValueDefinitionNode, InterfaceTypeDefinitionNode, ListTypeNode, NamedTypeNode, NameNode, NonNullTypeNode, ObjectTypeDefinitionNode, ScalarTypeDefinitionNode, UnionTypeDefinitionNode, StringValueNode } from 'graphql'; | ||
import { BaseVisitor, ParsedConfig, RawConfig } from './base-visitor'; | ||
@@ -52,2 +52,3 @@ import { EnumValuesMap, ScalarsMap } from './types'; | ||
EnumTypeDefinition(node: EnumTypeDefinitionNode): string; | ||
StringValue(node: StringValueNode): string; | ||
protected buildEnumValuesBlock(typeName: string, values: ReadonlyArray<EnumValueDefinitionNode>): string; | ||
@@ -54,0 +55,0 @@ DirectiveDefinition(node: DirectiveDefinitionNode): string; |
import { BaseVisitor } from './base-visitor'; | ||
import { parseMapper } from './mappers'; | ||
import { DEFAULT_SCALARS } from './scalars'; | ||
import { buildScalars, DeclarationBlock, indent, wrapWithSingleQuotes } from './utils'; | ||
import { transformComment, buildScalars, DeclarationBlock, indent, wrapWithSingleQuotes } from './utils'; | ||
import { OperationVariablesToObject } from './variables-to-object'; | ||
@@ -15,3 +15,5 @@ export class BaseTypesVisitor extends BaseVisitor { | ||
const scalarValue = this.config.scalars[scalarName]; | ||
return indent(`${scalarName}: ${scalarValue},`); | ||
const scalarType = this._schema.getType(scalarName); | ||
const comment = scalarType && scalarType.astNode && scalarType.description ? transformComment(scalarType.description, 1) : ''; | ||
return comment + indent(`${scalarName}: ${scalarValue},`); | ||
}); | ||
@@ -22,2 +24,3 @@ return new DeclarationBlock(this._declarationBlockConfig) | ||
.withName('Scalars') | ||
.withComment('All built-in and custom scalars, mapped to their actual values') | ||
.withBlock(allScalars.join('\n')).string; | ||
@@ -40,6 +43,8 @@ } | ||
.withName(this.convertName(node)) | ||
.withComment(node.description) | ||
.withBlock(node.fields.join('\n')).string; | ||
} | ||
InputValueDefinition(node) { | ||
return indent(`${node.name}: ${node.type},`); | ||
const comment = transformComment(node.description, 1); | ||
return comment + indent(`${node.name}: ${node.type},`); | ||
} | ||
@@ -51,3 +56,4 @@ Name(node) { | ||
const typeString = node.type; | ||
return indent(`${node.name}: ${typeString},`); | ||
const comment = transformComment(node.description, 1); | ||
return comment + indent(`${node.name}: ${typeString},`); | ||
} | ||
@@ -61,2 +67,3 @@ UnionTypeDefinition(node, key, parent) { | ||
.withName(this.convertName(node)) | ||
.withComment(node.description) | ||
.withContent(possibleTypes).string; | ||
@@ -72,2 +79,3 @@ } | ||
.withContent(interfaces) | ||
.withComment(node.description) | ||
.withBlock(node.fields.join('\n')).string; | ||
@@ -86,2 +94,3 @@ const original = parent[key]; | ||
.withName(this.convertName(name)) | ||
.withComment(node.description) | ||
.withBlock(this._argumentsTransformer.transform(field.arguments)).string; | ||
@@ -96,2 +105,3 @@ }); | ||
.withName(this.convertName(node)) | ||
.withComment(node.description) | ||
.withBlock(node.fields.join('\n')).string; | ||
@@ -132,4 +142,9 @@ } | ||
.withName(this.convertName(node)) | ||
.withComment(node.description) | ||
.withBlock(this.buildEnumValuesBlock(enumName, node.values)).string; | ||
} | ||
// We are using it in order to transform "description" field | ||
StringValue(node) { | ||
return node.value; | ||
} | ||
buildEnumValuesBlock(typeName, values) { | ||
@@ -139,2 +154,3 @@ return values | ||
const optionName = this.convertName(enumOption); | ||
const comment = transformComment(enumOption.description, 1); | ||
let enumValue = enumOption.name; | ||
@@ -144,3 +160,3 @@ if (this.config.enumValues[typeName] && typeof this.config.enumValues[typeName] === 'object' && this.config.enumValues[typeName][enumValue]) { | ||
} | ||
return indent(`${optionName}${this._declarationBlockConfig.enumNameValueSeparator} ${wrapWithSingleQuotes(enumValue)}`); | ||
return comment + indent(`${optionName}${this._declarationBlockConfig.enumNameValueSeparator} ${wrapWithSingleQuotes(enumValue)}`); | ||
}) | ||
@@ -147,0 +163,0 @@ .join(',\n'); |
@@ -15,2 +15,3 @@ import { NameNode, TypeNode, NamedTypeNode, GraphQLObjectType, GraphQLNonNull, GraphQLList, GraphQLOutputType, GraphQLNamedType, GraphQLSchema } from 'graphql'; | ||
} | ||
export declare function transformComment(comment: string, indentLevel?: number): string; | ||
export declare class DeclarationBlock { | ||
@@ -25,5 +26,7 @@ private _config; | ||
_nameGenerics: any; | ||
_comment: any; | ||
constructor(_config: DeclarationBlockConfig); | ||
export(exp?: boolean): DeclarationBlock; | ||
asKind(kind: any): DeclarationBlock; | ||
asKind(kind: string): DeclarationBlock; | ||
withComment(comment: string | null): DeclarationBlock; | ||
withMethodCall(methodName: string): DeclarationBlock; | ||
@@ -30,0 +33,0 @@ withBlock(block: string): DeclarationBlock; |
@@ -43,2 +43,18 @@ import { pascalCase } from 'change-case'; | ||
} | ||
export function transformComment(comment, indentLevel = 0) { | ||
if (!comment || comment === '') { | ||
return ''; | ||
} | ||
const lines = comment.split('\n'); | ||
return lines | ||
.map((line, index) => { | ||
const isLast = lines.length === index + 1; | ||
const isFirst = index === 0; | ||
if (isFirst && isLast) { | ||
return indent(`/** ${comment} */\n`, indentLevel); | ||
} | ||
return indent(`${isFirst ? '/** ' : ' * '}${line}${isLast ? '\n */\n' : ''}`, indentLevel); | ||
}) | ||
.join('\n'); | ||
} | ||
export class DeclarationBlock { | ||
@@ -54,2 +70,3 @@ constructor(_config) { | ||
this._nameGenerics = null; | ||
this._comment = null; | ||
this._config = Object.assign({ blockWrapper: '', blockTransformer: block => block, enumNameValueSeparator: ':' }, this._config); | ||
@@ -65,2 +82,8 @@ } | ||
} | ||
withComment(comment) { | ||
if (comment) { | ||
this._comment = transformComment(comment, 0); | ||
} | ||
return this; | ||
} | ||
withMethodCall(methodName) { | ||
@@ -119,3 +142,3 @@ this._methodName = methodName; | ||
} | ||
return result + (this._kind === 'interface' || this._kind === 'enum' ? '' : ';') + '\n'; | ||
return (this._comment ? this._comment : '') + result + (this._kind === 'interface' || this._kind === 'enum' ? '' : ';') + '\n'; | ||
} | ||
@@ -122,0 +145,0 @@ } |
{ | ||
"name": "@graphql-codegen/visitor-plugin-common", | ||
"version": "1.0.1-alpha-d343ec0b.18+d343ec0b", | ||
"version": "1.0.1", | ||
"license": "MIT", | ||
@@ -9,3 +9,3 @@ "scripts": { | ||
"dependencies": { | ||
"@graphql-codegen/plugin-helpers": "1.0.1-alpha-d343ec0b.18+d343ec0b", | ||
"@graphql-codegen/plugin-helpers": "1.0.1", | ||
"auto-bind": "2.0.0", | ||
@@ -20,3 +20,3 @@ "dependency-graph": "0.8.0", | ||
"devDependencies": { | ||
"@graphql-codegen/testing": "1.0.1-alpha-d343ec0b.18+d343ec0b", | ||
"@graphql-codegen/testing": "1.0.1", | ||
"@types/graphql": "14.0.7", | ||
@@ -36,3 +36,3 @@ "graphql": "14.1.1", | ||
}, | ||
"gitHead": "d343ec0baebd2fe1b17c6afb2971acafaaf31e79" | ||
"gitHead": "8928b444997fa177dc106220637709370cc527f3" | ||
} |
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
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
227678
3335
1
0
+ Added@graphql-codegen/plugin-helpers@1.0.1(transitive)
+ Addedcamel-case@3.0.0(transitive)
+ Addedchange-case@3.1.0(transitive)
+ Addedcommon-tags@1.8.0(transitive)
+ Addedconstant-case@2.0.0(transitive)
+ Addeddot-case@2.1.1(transitive)
+ Addedheader-case@1.0.1(transitive)
+ Addedimport-from@2.1.0(transitive)
+ Addedis-lower-case@1.1.3(transitive)
+ Addedis-upper-case@1.1.2(transitive)
+ Addedlower-case@1.1.4(transitive)
+ Addedlower-case-first@1.0.2(transitive)
+ Addedno-case@2.3.2(transitive)
+ Addedparam-case@2.1.1(transitive)
+ Addedpascal-case@2.0.1(transitive)
+ Addedpath-case@2.1.1(transitive)
+ Addedresolve-from@3.0.0(transitive)
+ Addedsentence-case@2.1.1(transitive)
+ Addedsnake-case@2.1.0(transitive)
+ Addedswap-case@1.1.2(transitive)
+ Addedtitle-case@2.1.1(transitive)
+ Addedupper-case@1.1.3(transitive)
+ Addedupper-case-first@1.1.2(transitive)