Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

graphql-codegen-typescript

Package Overview
Dependencies
Maintainers
1
Versions
123
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-codegen-typescript - npm Package Compare versions

Comparing version 0.19.0-alpha.3f859ab8 to 0.19.0-alpha.52db8d29

cjs/index.js

2

dist/index.d.ts

@@ -1,2 +0,2 @@

import { PluginFunction } from 'graphql-codegen-core';
import { PluginFunction } from 'graphql-codegen-plugin-helpers';
import { RawTypesConfig } from 'graphql-codegen-visitor-plugin-common';

@@ -3,0 +3,0 @@ export * from './typescript-variables-to-object';

@@ -1,27 +0,22 @@

"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
var graphql_1 = require("graphql");
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) {
var visitor = new visitor_1.TsVisitor(config);
var printedSchema = graphql_1.printSchema(schema);
var astNode = graphql_1.parse(printedSchema);
var header = "type Maybe<T> = " + visitor.config.maybeValue + ";";
var visitorResult = graphql_1.visit(astNode, { leave: visitor });
var introspectionDefinitions = includeIntrospectionDefinitions(schema, documents, config);
return [header].concat(visitorResult.definitions, introspectionDefinitions).join('\n');
import { parse, printSchema, visit, TypeInfo, visitWithTypeInfo, getNamedType, isIntrospectionType, printIntrospectionSchema, isObjectType } from 'graphql';
import { TsVisitor } from './visitor';
import { TsIntrospectionVisitor } from './introspection-visitor';
export * from './typescript-variables-to-object';
export * from './visitor';
export const plugin = (schema, documents, config) => {
const visitor = new TsVisitor(config);
const printedSchema = printSchema(schema);
const astNode = parse(printedSchema);
const header = `type Maybe<T> = ${visitor.config.maybeValue};`;
const visitorResult = visit(astNode, { leave: visitor });
const introspectionDefinitions = includeIntrospectionDefinitions(schema, documents, config);
return [header, ...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)) {
const typeInfo = new TypeInfo(schema);
const usedTypes = [];
const documentsVisitor = visitWithTypeInfo(typeInfo, {
Field() {
const type = getNamedType(typeInfo.getType());
if (isIntrospectionType(type) && !usedTypes.includes(type)) {
usedTypes.push(type);

@@ -31,9 +26,9 @@ }

});
documents.forEach(function (doc) { return graphql_1.visit(doc.content, documentsVisitor); });
var typesToInclude = [];
usedTypes.forEach(function (type) {
documents.forEach(doc => visit(doc.content, documentsVisitor));
const typesToInclude = [];
usedTypes.forEach(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 });
const visitor = new TsIntrospectionVisitor(config, typesToInclude);
const result = visit(parse(printIntrospectionSchema(schema)), { leave: visitor });
// recursively go through each `usedTypes` and their children and collect all used types

@@ -46,7 +41,7 @@ // we don't care about Interfaces, Unions and others, but Objects and Enums

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);
if (isObjectType(type)) {
const fields = type.getFields();
Object.keys(fields).forEach(key => {
const field = fields[key];
const type = getNamedType(field.type);
collectTypes(type);

@@ -53,0 +48,0 @@ });

@@ -1,48 +0,28 @@

"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var visitor_1 = require("./visitor");
var autoBind = require("auto-bind");
var TsIntrospectionVisitor = /** @class */ (function (_super) {
__extends(TsIntrospectionVisitor, _super);
function TsIntrospectionVisitor(pluginConfig, typesToInclude) {
if (pluginConfig === void 0) { pluginConfig = {}; }
var _this = _super.call(this, pluginConfig) || this;
_this.typesToInclude = [];
_this.typesToInclude = typesToInclude;
autoBind(_this);
return _this;
import { TsVisitor } from './visitor';
import autoBind from 'auto-bind';
export class TsIntrospectionVisitor extends TsVisitor {
constructor(pluginConfig = {}, typesToInclude) {
super(pluginConfig);
this.typesToInclude = [];
this.typesToInclude = typesToInclude;
autoBind(this);
}
TsIntrospectionVisitor.prototype.DirectiveDefinition = function () {
DirectiveDefinition() {
return null;
};
TsIntrospectionVisitor.prototype.ObjectTypeDefinition = function (node, key, parent) {
var name = node.name;
if (this.typesToInclude.some(function (type) { return type.name === name; })) {
return _super.prototype.ObjectTypeDefinition.call(this, node, key, parent);
}
ObjectTypeDefinition(node, key, parent) {
const name = node.name;
if (this.typesToInclude.some(type => type.name === name)) {
return super.ObjectTypeDefinition(node, key, parent);
}
return null;
};
TsIntrospectionVisitor.prototype.EnumTypeDefinition = function (node) {
var name = node.name;
if (this.typesToInclude.some(function (type) { return type.name === name; })) {
return _super.prototype.EnumTypeDefinition.call(this, node);
}
EnumTypeDefinition(node) {
const name = node.name;
if (this.typesToInclude.some(type => type.name === name)) {
return super.EnumTypeDefinition(node);
}
return null;
};
return TsIntrospectionVisitor;
}(visitor_1.TsVisitor));
exports.TsIntrospectionVisitor = TsIntrospectionVisitor;
}
}
//# sourceMappingURL=introspection-visitor.js.map

@@ -1,27 +0,10 @@

"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var graphql_codegen_visitor_plugin_common_1 = require("graphql-codegen-visitor-plugin-common");
var graphql_1 = require("graphql");
var TypeScriptOperationVariablesToObject = /** @class */ (function (_super) {
__extends(TypeScriptOperationVariablesToObject, _super);
function TypeScriptOperationVariablesToObject(_scalars, _convertName, _avoidOptionals, _immutableTypes) {
var _this = _super.call(this, _scalars, _convertName) || this;
_this._avoidOptionals = _avoidOptionals;
_this._immutableTypes = _immutableTypes;
return _this;
import { OperationVariablesToObject } from 'graphql-codegen-visitor-plugin-common';
import { Kind } from 'graphql';
export class TypeScriptOperationVariablesToObject extends OperationVariablesToObject {
constructor(_scalars, _convertName, _avoidOptionals, _immutableTypes) {
super(_scalars, _convertName);
this._avoidOptionals = _avoidOptionals;
this._immutableTypes = _immutableTypes;
}
TypeScriptOperationVariablesToObject.prototype.clearOptional = function (str) {
clearOptional(str) {
if (str.startsWith('Maybe')) {

@@ -31,17 +14,17 @@ return str.replace(/^Maybe<(.*?)>$/i, '$1');

return str;
};
TypeScriptOperationVariablesToObject.prototype.wrapAstTypeWithModifiers = function (baseType, typeNode) {
if (typeNode.kind === graphql_1.Kind.NON_NULL_TYPE) {
var type = this.wrapAstTypeWithModifiers(baseType, typeNode.type);
}
wrapAstTypeWithModifiers(baseType, typeNode) {
if (typeNode.kind === Kind.NON_NULL_TYPE) {
const type = this.wrapAstTypeWithModifiers(baseType, typeNode.type);
return this.clearOptional(type);
}
else if (typeNode.kind === graphql_1.Kind.LIST_TYPE) {
var innerType = this.wrapAstTypeWithModifiers(baseType, typeNode.type);
return "Maybe<" + (this._immutableTypes ? 'ReadonlyArray' : 'Array') + "<" + innerType + ">>";
else if (typeNode.kind === Kind.LIST_TYPE) {
const innerType = this.wrapAstTypeWithModifiers(baseType, typeNode.type);
return `Maybe<${this._immutableTypes ? 'ReadonlyArray' : 'Array'}<${innerType}>>`;
}
else {
return "Maybe<" + baseType + ">";
return `Maybe<${baseType}>`;
}
};
TypeScriptOperationVariablesToObject.prototype.formatFieldString = function (fieldName, isNonNullType, hasDefaultValue) {
}
formatFieldString(fieldName, isNonNullType, hasDefaultValue) {
if (hasDefaultValue || isNonNullType || this._avoidOptionals) {

@@ -51,6 +34,6 @@ return fieldName;

else {
return fieldName + "?";
return `${fieldName}?`;
}
};
TypeScriptOperationVariablesToObject.prototype.formatTypeString = function (fieldType, isNonNullType, hasDefaultValue) {
}
formatTypeString(fieldType, isNonNullType, hasDefaultValue) {
if (hasDefaultValue) {

@@ -60,6 +43,4 @@ return this.clearOptional(fieldType);

return fieldType;
};
return TypeScriptOperationVariablesToObject;
}(graphql_codegen_visitor_plugin_common_1.OperationVariablesToObject));
exports.TypeScriptOperationVariablesToObject = TypeScriptOperationVariablesToObject;
}
}
//# sourceMappingURL=typescript-variables-to-object.js.map

@@ -1,43 +0,15 @@

"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
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 });
var graphql_codegen_visitor_plugin_common_1 = require("graphql-codegen-visitor-plugin-common");
var autoBind = require("auto-bind");
var typescript_variables_to_object_1 = require("./typescript-variables-to-object");
var TsVisitor = /** @class */ (function (_super) {
__extends(TsVisitor, _super);
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);
_this.setArgumentsTransformer(new typescript_variables_to_object_1.TypeScriptOperationVariablesToObject(_this.scalars, _this.convertName, _this.config.avoidOptionals, _this.config.immutableTypes));
_this.setDeclarationBlockConfig({
import { DeclarationBlock, indent, BaseTypesVisitor } from 'graphql-codegen-visitor-plugin-common';
import autoBind from 'auto-bind';
import { Kind } from 'graphql';
import { TypeScriptOperationVariablesToObject } from './typescript-variables-to-object';
export class TsVisitor extends BaseTypesVisitor {
constructor(pluginConfig, additionalConfig = {}) {
super(pluginConfig, Object.assign({ avoidOptionals: pluginConfig.avoidOptionals || false, maybeValue: pluginConfig.maybeValue || 'T | null', constEnums: pluginConfig.constEnums || false, enumsAsTypes: pluginConfig.enumsAsTypes || false, immutableTypes: pluginConfig.immutableTypes || false }, (additionalConfig || {})), null);
autoBind(this);
this.setArgumentsTransformer(new TypeScriptOperationVariablesToObject(this.scalars, this.convertName, this.config.avoidOptionals, this.config.immutableTypes));
this.setDeclarationBlockConfig({
enumNameValueSeparator: ' ='
});
return _this;
}
TsVisitor.prototype.clearOptional = function (str) {
clearOptional(str) {
if (str.startsWith('Maybe')) {

@@ -47,33 +19,32 @@ return str.replace(/Maybe<(.*?)>/, '$1');

return str;
};
TsVisitor.prototype.NamedType = function (node) {
return "Maybe<" + _super.prototype.NamedType.call(this, node) + ">";
};
TsVisitor.prototype.ListType = function (node) {
return "Maybe<" + _super.prototype.ListType.call(this, node) + ">";
};
TsVisitor.prototype.wrapWithListType = function (str) {
return (this.config.immutableTypes ? 'ReadonlyArray' : 'Array') + "<" + str + ">";
};
TsVisitor.prototype.NonNullType = function (node) {
var baseValue = _super.prototype.NonNullType.call(this, node);
}
NamedType(node) {
return `Maybe<${super.NamedType(node)}>`;
}
ListType(node) {
return `Maybe<${super.ListType(node)}>`;
}
wrapWithListType(str) {
return `${this.config.immutableTypes ? 'ReadonlyArray' : 'Array'}<${str}>`;
}
NonNullType(node) {
const baseValue = super.NonNullType(node);
return this.clearOptional(baseValue);
};
TsVisitor.prototype.FieldDefinition = function (node, key, parent) {
var typeString = node.type;
var originalFieldNode = parent[key];
var addOptionalSign = !this.config.avoidOptionals && originalFieldNode.type.kind !== 'NonNullType';
return graphql_codegen_visitor_plugin_common_1.indent("" + (this.config.immutableTypes ? 'readonly ' : '') + node.name + (addOptionalSign ? '?' : '') + ": " + typeString + ",");
};
TsVisitor.prototype.EnumTypeDefinition = function (node) {
var _this = this;
}
FieldDefinition(node, key, parent) {
const typeString = node.type;
const originalFieldNode = parent[key];
const addOptionalSign = !this.config.avoidOptionals && originalFieldNode.type.kind !== Kind.NON_NULL_TYPE;
return indent(`${this.config.immutableTypes ? 'readonly ' : ''}${node.name}${addOptionalSign ? '?' : ''}: ${typeString},`);
}
EnumTypeDefinition(node) {
if (this.config.enumsAsTypes) {
return new graphql_codegen_visitor_plugin_common_1.DeclarationBlock(this._declarationBlockConfig)
return new DeclarationBlock(this._declarationBlockConfig)
.export()
.asKind('type')
.withName(this.convertName(node))
.withContent(node.values.map(function (v) { return "'" + (_this.config.enumValues[v.name] || v.name) + "'"; }).join(' | ')).string;
.withContent(node.values.map(v => `'${this.config.enumValues[v.name] || v.name}'`).join(' | ')).string;
}
else {
return new graphql_codegen_visitor_plugin_common_1.DeclarationBlock(this._declarationBlockConfig)
return new DeclarationBlock(this._declarationBlockConfig)
.export()

@@ -84,6 +55,4 @@ .asKind(this.config.constEnums ? 'const enum' : 'enum')

}
};
return TsVisitor;
}(graphql_codegen_visitor_plugin_common_1.BaseTypesVisitor));
exports.TsVisitor = TsVisitor;
}
}
//# sourceMappingURL=visitor.js.map
{
"name": "graphql-codegen-typescript",
"version": "0.19.0-alpha.3f859ab8",
"version": "0.19.0-alpha.52db8d29",
"description": "GraphQL Code Generator plugin for generating TypeScript types",

@@ -12,8 +12,10 @@ "repository": "git@github.com:dotansimha/graphql-code-generator.git",

"dependencies": {
"graphql-codegen-core": "0.19.0-alpha.3f859ab8",
"graphql-codegen-plugin-helpers": "0.19.0-alpha.3f859ab8",
"graphql-codegen-visitor-plugin-common": "0.19.0-alpha.3f859ab8"
"esm": "3.2.11",
"graphql-codegen-plugin-helpers": "0.19.0-alpha.52db8d29",
"graphql-codegen-visitor-plugin-common": "0.19.0-alpha.52db8d29",
"tslib": "1.9.3"
},
"devDependencies": {
"graphql": "14.1.1",
"graphql-codegen-testing": "0.19.0-alpha.52db8d29",
"jest": "24.1.0",

@@ -23,3 +25,4 @@ "ts-jest": "24.0.0",

},
"main": "./dist/index.js",
"main": "cjs/index.js",
"module": "dist/index.js",
"typings": "dist/index.d.ts",

@@ -32,3 +35,6 @@ "typescript": {

"ts-jest": {
"enableTsDiagnostics": false
"enableTsDiagnostics": false,
"tsConfig": {
"esModuleInterop": true
}
}

@@ -35,0 +41,0 @@ },

{
"compilerOptions": {
"importHelpers": true,
"experimentalDecorators": true,
"module": "commonjs",
"target": "es5",
"module": "esnext",
"target": "es2018",
"lib": ["es6", "esnext", "es2015"],

@@ -7,0 +8,0 @@ "suppressImplicitAnyIndexErrors": true,

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc