graphql-codegen-typescript
Advanced tools
Comparing version 0.19.0-alpha.26bc98f9 to 0.19.0-alpha.2da4f267
import { PluginFunction } from 'graphql-codegen-core'; | ||
import { RawTypesConfig } from 'graphql-codegen-visitor-plugin-common'; | ||
export * from './typescript-variables-to-object'; | ||
export * from './visitor'; | ||
export interface TypeScriptPluginConfig extends RawTypesConfig { | ||
@@ -5,0 +6,0 @@ avoidOptionals?: boolean; |
@@ -8,3 +8,5 @@ "use strict"; | ||
var visitor_1 = require("./visitor"); | ||
var introspection_visitor_1 = require("./introspection-visitor"); | ||
__export(require("./typescript-variables-to-object")); | ||
__export(require("./visitor")); | ||
exports.plugin = function (schema, documents, config) { | ||
@@ -16,4 +18,41 @@ var visitor = new visitor_1.TsVisitor(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 |
@@ -11,4 +11,4 @@ import { BaseTypesVisitor, ParsedTypesConfig } from 'graphql-codegen-visitor-plugin-common'; | ||
} | ||
export declare class TsVisitor extends BaseTypesVisitor<TypeScriptPluginConfig, TypeScriptPluginParsedConfig> { | ||
constructor(pluginConfig?: TypeScriptPluginConfig); | ||
export declare class TsVisitor<TRawConfig extends TypeScriptPluginConfig = TypeScriptPluginConfig, TParsedConfig extends TypeScriptPluginParsedConfig = TypeScriptPluginParsedConfig> extends BaseTypesVisitor<TRawConfig, TParsedConfig> { | ||
constructor(pluginConfig: TRawConfig, additionalConfig?: Partial<TParsedConfig>); | ||
private clearOptional; | ||
@@ -15,0 +15,0 @@ NamedType(node: NamedTypeNode): string; |
@@ -15,2 +15,13 @@ "use strict"; | ||
})(); | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -22,11 +33,5 @@ var graphql_codegen_visitor_plugin_common_1 = require("graphql-codegen-visitor-plugin-common"); | ||
__extends(TsVisitor, _super); | ||
function TsVisitor(pluginConfig) { | ||
if (pluginConfig === void 0) { pluginConfig = {}; } | ||
var _this = _super.call(this, pluginConfig, { | ||
avoidOptionals: pluginConfig.avoidOptionals || false, | ||
maybeValue: pluginConfig.maybeValue || 'T | null', | ||
constEnums: pluginConfig.constEnums || false, | ||
enumsAsTypes: pluginConfig.enumsAsTypes || false, | ||
immutableTypes: pluginConfig.immutableTypes || false | ||
}, null) || this; | ||
function TsVisitor(pluginConfig, additionalConfig) { | ||
if (additionalConfig === void 0) { additionalConfig = {}; } | ||
var _this = _super.call(this, pluginConfig, __assign({ avoidOptionals: pluginConfig.avoidOptionals || false, maybeValue: pluginConfig.maybeValue || 'T | null', constEnums: pluginConfig.constEnums || false, enumsAsTypes: pluginConfig.enumsAsTypes || false, immutableTypes: pluginConfig.immutableTypes || false }, (additionalConfig || {})), null) || this; | ||
autoBind(_this); | ||
@@ -70,3 +75,3 @@ _this.setArgumentsTransformer(new typescript_variables_to_object_1.TypeScriptOperationVariablesToObject(_this.scalars, _this.convertName, _this.config.avoidOptionals, _this.config.immutableTypes)); | ||
.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; | ||
@@ -78,3 +83,3 @@ } | ||
.asKind(this.config.constEnums ? 'const enum' : 'enum') | ||
.withName(this.convertName(node.name)) | ||
.withName(this.convertName(node)) | ||
.withBlock(this.buildEnumValuesBlock(node.values)).string; | ||
@@ -81,0 +86,0 @@ } |
{ | ||
"name": "graphql-codegen-typescript", | ||
"version": "0.19.0-alpha.26bc98f9", | ||
"version": "0.19.0-alpha.2da4f267", | ||
"description": "GraphQL Code Generator plugin for generating TypeScript types", | ||
@@ -8,3 +8,2 @@ "repository": "git@github.com:dotansimha/graphql-code-generator.git", | ||
"scripts": { | ||
"prepublishOnly": "yarn build", | ||
"build": "tsc", | ||
@@ -15,5 +14,5 @@ "pretest": "yarn build", | ||
"dependencies": { | ||
"graphql-codegen-core": "0.19.0-alpha.26bc98f9", | ||
"graphql-codegen-plugin-helpers": "0.19.0-alpha.26bc98f9", | ||
"graphql-codegen-visitor-plugin-common": "0.19.0-alpha.26bc98f9" | ||
"graphql-codegen-core": "0.19.0-alpha.2da4f267", | ||
"graphql-codegen-plugin-helpers": "0.19.0-alpha.2da4f267", | ||
"graphql-codegen-visitor-plugin-common": "0.19.0-alpha.2da4f267" | ||
}, | ||
@@ -20,0 +19,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
23786
14
324
+ Addedgraphql-codegen-core@0.19.0-alpha.2da4f267(transitive)
+ Addedgraphql-codegen-plugin-helpers@0.19.0-alpha.2da4f267(transitive)
+ Addedgraphql-codegen-visitor-plugin-common@0.19.0-alpha.2da4f267(transitive)
- Removedgraphql-codegen-core@0.19.0-alpha.26bc98f9(transitive)
- Removedgraphql-codegen-plugin-helpers@0.19.0-alpha.26bc98f9(transitive)
- Removedgraphql-codegen-visitor-plugin-common@0.19.0-alpha.26bc98f9(transitive)
Updatedgraphql-codegen-visitor-plugin-common@0.19.0-alpha.2da4f267