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.6a3f1430 to 0.19.0-alpha.6f299999

cjs/index.js

3

dist/index.d.ts

@@ -1,4 +0,5 @@

import { PluginFunction } from 'graphql-codegen-core';
import { PluginFunction } from 'graphql-codegen-plugin-helpers';
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;

@@ -1,17 +0,51 @@

"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
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) {
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);
}
}
});
documents.forEach(doc => visit(doc.content, documentsVisitor));
const typesToInclude = [];
usedTypes.forEach(type => {
collectTypes(type);
});
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
// 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 (isObjectType(type)) {
const fields = type.getFields();
Object.keys(fields).forEach(key => {
const field = fields[key];
const type = getNamedType(field.type);
collectTypes(type);
});
}
}
return result.definitions;
}
Object.defineProperty(exports, "__esModule", { value: true });
var graphql_1 = require("graphql");
var visitor_1 = require("./visitor");
__export(require("./typescript-variables-to-object"));
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 });
return [header].concat(visitorResult.definitions).join('\n');
};
//# sourceMappingURL=index.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

@@ -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;

@@ -1,38 +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 __());
};
})();
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) {
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;
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')) {

@@ -42,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()

@@ -79,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.6a3f1430",
"version": "0.19.0-alpha.6f299999",
"description": "GraphQL Code Generator plugin for generating TypeScript types",

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

"scripts": {
"prepublishOnly": "yarn build",
"build": "tsc",
"pretest": "yarn build",
"test": "jest"
},
"dependencies": {
"graphql-codegen-core": "0.19.0-alpha.6a3f1430",
"graphql-codegen-plugin-helpers": "0.19.0-alpha.6a3f1430",
"graphql-codegen-visitor-plugin-common": "0.19.0-alpha.6a3f1430"
"esm": "3.2.11",
"graphql-codegen-plugin-helpers": "0.19.0-alpha.6f299999",
"graphql-codegen-visitor-plugin-common": "0.19.0-alpha.6f299999",
"tslib": "1.9.3"
},
"devDependencies": {
"graphql": "14.1.1",
"graphql-codegen-testing": "0.19.0-alpha.6f299999",
"jest": "24.1.0",

@@ -25,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",

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

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

@@ -37,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

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