graphql-codegen-typescript
Advanced tools
Comparing version 0.19.0-alpha.b268f76e to 0.19.0-alpha.b76e699a
@@ -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 |
{ | ||
"name": "graphql-codegen-typescript", | ||
"version": "0.19.0-alpha.b268f76e", | ||
"version": "0.19.0-alpha.b76e699a", | ||
"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.b268f76e", | ||
"graphql-codegen-plugin-helpers": "0.19.0-alpha.b268f76e", | ||
"graphql-codegen-visitor-plugin-common": "0.19.0-alpha.b268f76e" | ||
"graphql-codegen-core": "0.19.0-alpha.b76e699a", | ||
"graphql-codegen-plugin-helpers": "0.19.0-alpha.b76e699a", | ||
"graphql-codegen-visitor-plugin-common": "0.19.0-alpha.b76e699a" | ||
}, | ||
@@ -19,0 +19,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
23167
14
316
+ Addedgraphql-codegen-core@0.19.0-alpha.b76e699a(transitive)
+ Addedgraphql-codegen-plugin-helpers@0.19.0-alpha.b76e699a(transitive)
+ Addedgraphql-codegen-visitor-plugin-common@0.19.0-alpha.b76e699a(transitive)
- Removedgraphql-codegen-core@0.19.0-alpha.b268f76e(transitive)
- Removedgraphql-codegen-plugin-helpers@0.19.0-alpha.b268f76e(transitive)
- Removedgraphql-codegen-visitor-plugin-common@0.19.0-alpha.b268f76e(transitive)
Updatedgraphql-codegen-visitor-plugin-common@0.19.0-alpha.b76e699a