graphql-codegen-typescript
Advanced tools
Comparing version
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; | ||
@@ -6,0 +6,0 @@ constEnums?: boolean; |
@@ -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 |
@@ -1,5 +0,5 @@ | ||
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; | ||
@@ -11,3 +11,3 @@ constEnums: boolean; | ||
} | ||
export declare class TsVisitor extends BaseVisitor<TypeScriptPluginConfig, TypeScriptPluginParsedConfig> { | ||
export declare class TsVisitor extends BaseTypesVisitor<TypeScriptPluginConfig, TypeScriptPluginParsedConfig> { | ||
constructor(pluginConfig?: TypeScriptPluginConfig); | ||
@@ -14,0 +14,0 @@ private clearOptional; |
@@ -68,3 +68,3 @@ "use strict"; | ||
.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; | ||
@@ -76,3 +76,3 @@ } | ||
.asKind(this.config.constEnums ? 'const enum' : 'enum') | ||
.withName(this.convertName(node.name)) | ||
.withName(this.convertName(node)) | ||
.withBlock(this.buildEnumValuesBlock(node.values)).string; | ||
@@ -82,4 +82,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.19.0-alpha.35980e3e", | ||
"version": "0.19.0-alpha.5634c385", | ||
"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.19.0-alpha.35980e3e", | ||
"graphql-codegen-plugin-helpers": "0.19.0-alpha.35980e3e", | ||
"graphql-codegen-visitor-plugin-common": "0.19.0-alpha.35980e3e" | ||
"graphql-codegen-core": "0.19.0-alpha.5634c385", | ||
"graphql-codegen-plugin-helpers": "0.19.0-alpha.5634c385", | ||
"graphql-codegen-visitor-plugin-common": "0.19.0-alpha.5634c385" | ||
}, | ||
@@ -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
23167
41.94%14
27.27%316
42.99%+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
Updated