graphql-codegen-typescript
Advanced tools
Comparing version 0.18.0-alpha.cab8e330 to 0.19.0-alpha.004d58ea
import { PluginFunction } from 'graphql-codegen-core'; | ||
import { RawConfig } from 'graphql-codegen-visitor-plugin-common'; | ||
import { RawTypesConfig } from 'graphql-codegen-visitor-plugin-common'; | ||
export * from './typescript-variables-to-object'; | ||
export interface TypeScriptPluginConfig extends RawConfig { | ||
export interface TypeScriptPluginConfig extends RawTypesConfig { | ||
avoidOptionals?: boolean; | ||
constEnums?: boolean; | ||
enumsAsTypes?: boolean; | ||
immutableTypes?: boolean; | ||
maybeValue?: string; | ||
} | ||
export declare const plugin: PluginFunction<TypeScriptPluginConfig>; |
@@ -8,2 +8,3 @@ "use strict"; | ||
var visitor_1 = require("./visitor"); | ||
var introspection_visitor_1 = require("./introspection-visitor"); | ||
__export(require("./typescript-variables-to-object")); | ||
@@ -16,4 +17,41 @@ exports.plugin = function (schema, documents, config) { | ||
var visitorResult = graphql_1.visit(astNode, { leave: visitor }); | ||
return [header].concat(visitorResult.definitions).join('\n'); | ||
var introspectionDefinitions = includeIntrospectionDefinitions(schema, documents, config); | ||
return [header].concat(visitorResult.definitions, introspectionDefinitions).join('\n'); | ||
}; | ||
function includeIntrospectionDefinitions(schema, documents, config) { | ||
var typeInfo = new graphql_1.TypeInfo(schema); | ||
var usedTypes = []; | ||
var documentsVisitor = graphql_1.visitWithTypeInfo(typeInfo, { | ||
Field: function () { | ||
var type = graphql_1.getNamedType(typeInfo.getType()); | ||
if (graphql_1.isIntrospectionType(type) && !usedTypes.includes(type)) { | ||
usedTypes.push(type); | ||
} | ||
} | ||
}); | ||
documents.forEach(function (doc) { return graphql_1.visit(doc.content, documentsVisitor); }); | ||
var typesToInclude = []; | ||
usedTypes.forEach(function (type) { | ||
collectTypes(type); | ||
}); | ||
var visitor = new introspection_visitor_1.TsIntrospectionVisitor(config, typesToInclude); | ||
var result = graphql_1.visit(graphql_1.parse(graphql_1.printIntrospectionSchema(schema)), { leave: visitor }); | ||
// recursively go through each `usedTypes` and their children and collect all used types | ||
// we don't care about Interfaces, Unions and others, but Objects and Enums | ||
function collectTypes(type) { | ||
if (typesToInclude.includes(type)) { | ||
return; | ||
} | ||
typesToInclude.push(type); | ||
if (graphql_1.isObjectType(type)) { | ||
var fields_1 = type.getFields(); | ||
Object.keys(fields_1).forEach(function (key) { | ||
var field = fields_1[key]; | ||
var type = graphql_1.getNamedType(field.type); | ||
collectTypes(type); | ||
}); | ||
} | ||
} | ||
return result.definitions; | ||
} | ||
//# sourceMappingURL=index.js.map |
@@ -5,3 +5,4 @@ import { OperationVariablesToObject, ScalarsMap, ConvertNameFn } from 'graphql-codegen-visitor-plugin-common'; | ||
private _avoidOptionals; | ||
constructor(_scalars: ScalarsMap, _convertName: ConvertNameFn, _avoidOptionals: boolean); | ||
private _immutableTypes; | ||
constructor(_scalars: ScalarsMap, _convertName: ConvertNameFn, _avoidOptionals: boolean, _immutableTypes: boolean); | ||
private clearOptional; | ||
@@ -8,0 +9,0 @@ wrapAstTypeWithModifiers(baseType: string, typeNode: TypeNode): string; |
@@ -20,5 +20,6 @@ "use strict"; | ||
__extends(TypeScriptOperationVariablesToObject, _super); | ||
function TypeScriptOperationVariablesToObject(_scalars, _convertName, _avoidOptionals) { | ||
function TypeScriptOperationVariablesToObject(_scalars, _convertName, _avoidOptionals, _immutableTypes) { | ||
var _this = _super.call(this, _scalars, _convertName) || this; | ||
_this._avoidOptionals = _avoidOptionals; | ||
_this._immutableTypes = _immutableTypes; | ||
return _this; | ||
@@ -39,3 +40,3 @@ } | ||
var innerType = this.wrapAstTypeWithModifiers(baseType, typeNode.type); | ||
return "Maybe<Array<" + innerType + ">>"; | ||
return "Maybe<" + (this._immutableTypes ? 'ReadonlyArray' : 'Array') + "<" + innerType + ">>"; | ||
} | ||
@@ -42,0 +43,0 @@ else { |
@@ -1,11 +0,12 @@ | ||
import { BaseVisitor, ParsedConfig } from 'graphql-codegen-visitor-plugin-common'; | ||
import { BaseTypesVisitor, ParsedTypesConfig } from 'graphql-codegen-visitor-plugin-common'; | ||
import { TypeScriptPluginConfig } from './index'; | ||
import { FieldDefinitionNode, NamedTypeNode, ListTypeNode, NonNullTypeNode, EnumTypeDefinitionNode } from 'graphql'; | ||
export interface TypeScriptPluginParsedConfig extends ParsedConfig { | ||
export interface TypeScriptPluginParsedConfig extends ParsedTypesConfig { | ||
avoidOptionals: boolean; | ||
constEnums: boolean; | ||
enumsAsTypes: boolean; | ||
immutableTypes: boolean; | ||
maybeValue: string; | ||
} | ||
export declare class TsVisitor extends BaseVisitor<TypeScriptPluginConfig, TypeScriptPluginParsedConfig> { | ||
export declare class TsVisitor extends BaseTypesVisitor<TypeScriptPluginConfig, TypeScriptPluginParsedConfig> { | ||
constructor(pluginConfig?: TypeScriptPluginConfig); | ||
@@ -15,2 +16,3 @@ private clearOptional; | ||
ListType(node: ListTypeNode): string; | ||
protected wrapWithListType(str: string): string; | ||
NonNullType(node: NonNullTypeNode): string; | ||
@@ -17,0 +19,0 @@ FieldDefinition(node: FieldDefinitionNode, key?: number | string, parent?: any): string; |
@@ -27,6 +27,7 @@ "use strict"; | ||
constEnums: pluginConfig.constEnums || false, | ||
enumsAsTypes: pluginConfig.enumsAsTypes || false | ||
enumsAsTypes: pluginConfig.enumsAsTypes || false, | ||
immutableTypes: pluginConfig.immutableTypes || false | ||
}, null) || this; | ||
autoBind(_this); | ||
_this.setArgumentsTransformer(new typescript_variables_to_object_1.TypeScriptOperationVariablesToObject(_this.scalars, _this.convertName, _this.config.avoidOptionals)); | ||
_this.setArgumentsTransformer(new typescript_variables_to_object_1.TypeScriptOperationVariablesToObject(_this.scalars, _this.convertName, _this.config.avoidOptionals, _this.config.immutableTypes)); | ||
_this.setDeclarationBlockConfig({ | ||
@@ -49,2 +50,5 @@ enumNameValueSeparator: ' =' | ||
}; | ||
TsVisitor.prototype.wrapWithListType = function (str) { | ||
return (this.config.immutableTypes ? 'ReadonlyArray' : 'Array') + "<" + str + ">"; | ||
}; | ||
TsVisitor.prototype.NonNullType = function (node) { | ||
@@ -58,3 +62,3 @@ var baseValue = _super.prototype.NonNullType.call(this, node); | ||
var addOptionalSign = !this.config.avoidOptionals && originalFieldNode.type.kind !== 'NonNullType'; | ||
return graphql_codegen_visitor_plugin_common_1.indent("" + node.name + (addOptionalSign ? '?' : '') + ": " + typeString + ","); | ||
return graphql_codegen_visitor_plugin_common_1.indent("" + (this.config.immutableTypes ? 'readonly ' : '') + node.name + (addOptionalSign ? '?' : '') + ": " + typeString + ","); | ||
}; | ||
@@ -67,3 +71,3 @@ TsVisitor.prototype.EnumTypeDefinition = function (node) { | ||
.asKind('type') | ||
.withName(this.convertName(node.name)) | ||
.withName(this.convertName(node)) | ||
.withContent(node.values.map(function (v) { return "'" + (_this.config.enumValues[v.name] || v.name) + "'"; }).join(' | ')).string; | ||
@@ -75,3 +79,3 @@ } | ||
.asKind(this.config.constEnums ? 'const enum' : 'enum') | ||
.withName(this.convertName(node.name)) | ||
.withName(this.convertName(node)) | ||
.withBlock(this.buildEnumValuesBlock(node.values)).string; | ||
@@ -81,4 +85,4 @@ } | ||
return TsVisitor; | ||
}(graphql_codegen_visitor_plugin_common_1.BaseVisitor)); | ||
}(graphql_codegen_visitor_plugin_common_1.BaseTypesVisitor)); | ||
exports.TsVisitor = TsVisitor; | ||
//# sourceMappingURL=visitor.js.map |
{ | ||
"name": "graphql-codegen-typescript", | ||
"version": "0.18.0-alpha.cab8e330", | ||
"version": "0.19.0-alpha.004d58ea", | ||
"description": "GraphQL Code Generator plugin for generating TypeScript types", | ||
@@ -14,5 +14,5 @@ "repository": "git@github.com:dotansimha/graphql-code-generator.git", | ||
"dependencies": { | ||
"graphql-codegen-core": "0.18.0-alpha.cab8e330", | ||
"graphql-codegen-plugin-helpers": "0.18.0-alpha.cab8e330", | ||
"graphql-codegen-visitor-plugin-common": "0.18.0-alpha.cab8e330" | ||
"graphql-codegen-core": "0.19.0-alpha.004d58ea", | ||
"graphql-codegen-plugin-helpers": "0.19.0-alpha.004d58ea", | ||
"graphql-codegen-visitor-plugin-common": "0.19.0-alpha.004d58ea" | ||
}, | ||
@@ -19,0 +19,0 @@ "devDependencies": { |
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
23167
14
316
+ Addedaggregate-error@2.1.0(transitive)
+ Addeddependency-graph@0.8.0(transitive)
+ Addedgraphql-codegen-core@0.19.0-alpha.004d58ea(transitive)
+ Addedgraphql-codegen-plugin-helpers@0.19.0-alpha.004d58ea(transitive)
+ Addedgraphql-codegen-visitor-plugin-common@0.19.0-alpha.004d58ea(transitive)
+ Addedgraphql-toolkit@0.2.0(transitive)
- Removedaggregate-error@2.0.0(transitive)
- Removedgraphql-codegen-core@0.18.0-alpha.cab8e330(transitive)
- Removedgraphql-codegen-plugin-helpers@0.18.0-alpha.cab8e330(transitive)
- Removedgraphql-codegen-visitor-plugin-common@0.18.0-alpha.cab8e330(transitive)
- Removedgraphql-toolkit@0.1.1(transitive)
Updatedgraphql-codegen-visitor-plugin-common@0.19.0-alpha.004d58ea