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

nexus

Package Overview
Dependencies
Maintainers
3
Versions
395
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nexus - npm Package Compare versions

Comparing version 0.9.11 to 0.9.12

15

CHANGELOG.md
# Changelog
### 0.9.12
- Fix #33, consistent `t.list` chaining output for inputs & scalars
- Fix #34, include used imports on SDL converter
- Fix #27, args output for SDL converter
- Other SDL converter cleanup: reference types rather than strings, default values
### 0.9.11
- Fix typing regression in 0.9.10
### 0.9.10
- Fix #26, incorrect typing on boolean return type
### 0.9.9

@@ -4,0 +19,0 @@

2

dist/definitions/definitionBlocks.d.ts

@@ -150,2 +150,3 @@ import {

): void;
protected decorateField(config: NexusOutputFieldDef): NexusOutputFieldDef;
}

@@ -191,2 +192,3 @@ export interface ScalarInputFieldConfig<T> extends CommonFieldConfig {

): void;
protected decorateField(config: NexusOutputFieldDef): NexusOutputFieldDef;
}

@@ -193,0 +195,0 @@ export interface AbstractOutputDefinitionBuilder<TypeName extends string>

41

dist/definitions/definitionBlocks.js

@@ -62,12 +62,3 @@ "use strict";

var field = tslib_1.__assign({ name: name }, fieldConfig);
if (this.isList) {
if (field.list) {
console.warn("It looks like you chained .list and set list for " + name +
"You should only do one or the other");
}
else {
field.list = true;
}
}
this.typeBuilder.addField(field);
this.typeBuilder.addField(this.decorateField(field));
};

@@ -85,4 +76,16 @@ OutputDefinitionBlock.prototype.addScalarField = function (fieldName, typeName, opts) {

}
this.typeBuilder.addField(config);
this.typeBuilder.addField(this.decorateField(config));
};
OutputDefinitionBlock.prototype.decorateField = function (config) {
if (this.isList) {
if (config.list) {
console.warn("It looks like you chained .list and set list for " + config.name +
"You should only do one or the other");
}
else {
config.list = true;
}
}
return config;
};
return OutputDefinitionBlock;

@@ -124,8 +127,20 @@ }());

InputDefinitionBlock.prototype.field = function (fieldName, fieldConfig) {
this.typeBuilder.addField(tslib_1.__assign({ name: fieldName }, fieldConfig));
this.typeBuilder.addField(this.decorateField(tslib_1.__assign({ name: fieldName }, fieldConfig)));
};
InputDefinitionBlock.prototype.addScalarField = function (fieldName, typeName, opts) {
if (opts === void 0) { opts = {}; }
this.typeBuilder.addField(tslib_1.__assign({ name: fieldName, type: typeName }, opts));
this.typeBuilder.addField(this.decorateField(tslib_1.__assign({ name: fieldName, type: typeName }, opts)));
};
InputDefinitionBlock.prototype.decorateField = function (config) {
if (this.isList) {
if (config.list) {
console.warn("It looks like you chained .list and set list for " + config.name +
"You should only do one or the other");
}
else {
config.list = true;
}
}
return config;
};
return InputDefinitionBlock;

@@ -132,0 +147,0 @@ }());

@@ -14,2 +14,3 @@ import {

GraphQLInputField,
GraphQLArgument,
} from "graphql";

@@ -26,2 +27,3 @@ import { GroupedTypes } from "./utils";

export declare class SDLConverter {
protected commonjs: null | boolean;
protected json: JSON;

@@ -31,4 +33,6 @@ protected export: string;

protected groupedTypes: GroupedTypes;
protected usedImports: Set<string>;
constructor(sdl: string, commonjs?: null | boolean, json?: JSON);
print(): string;
printUsedImports(): string;
printObjectTypes(): string;

@@ -51,2 +55,4 @@ printObjectType(type: GraphQLObjectType): string;

): string;
printMeta(val: any, key: string): any;
printArg(arg: GraphQLArgument): string;
printInterfaceTypes(): string;

@@ -53,0 +59,0 @@ printInterfaceType(type: GraphQLInterfaceType): string;

@@ -23,3 +23,5 @@ "use strict";

if (json === void 0) { json = JSON; }
this.commonjs = commonjs;
this.json = json;
this.usedImports = new Set();
this.export =

@@ -31,3 +33,3 @@ commonjs === null ? "const " : commonjs ? "exports." : "export const ";

SDLConverter.prototype.print = function () {
return [
var body = [
this.printObjectTypes(),

@@ -39,9 +41,18 @@ this.printInterfaceTypes(),

this.printScalarTypes(),
]
];
return [this.printUsedImports()]
.concat(body)
.filter(function (f) { return f; })
.join("\n\n");
};
SDLConverter.prototype.printUsedImports = function () {
if (this.commonjs) {
return "const { " + Array.from(this.usedImports).join(", ") + " } = require('nexus');";
}
return "import { " + Array.from(this.usedImports).join(", ") + " } from 'nexus';";
};
SDLConverter.prototype.printObjectTypes = function () {
var _this = this;
if (this.groupedTypes.object.length > 0) {
this.usedImports.add("objectType");
return this.groupedTypes.object

@@ -54,8 +65,5 @@ .map(function (t) { return _this.printObjectType(t); })

SDLConverter.prototype.printObjectType = function (type) {
var _this = this;
var implementing = type.getInterfaces().map(function (i) { return i.name; });
var implementsInterfaces = implementing.length > 0
? " t.implements(" + implementing
.map(function (i) { return _this.json.stringify(i); })
.join(", ") + ")"
? " t.implements(" + implementing.join(", ") + ")"
: "";

@@ -92,21 +100,3 @@ return this.printBlock([

SDLConverter.prototype.printField = function (source, field) {
var fieldType = field.type;
var isNonNull = false;
var list = [];
while (graphql_1.isWrappingType(fieldType)) {
while (graphql_1.isListType(fieldType)) {
fieldType = fieldType.ofType;
if (graphql_1.isNonNullType(fieldType)) {
fieldType = fieldType.ofType;
list.unshift(true);
}
else {
list.unshift(false);
}
}
if (graphql_1.isNonNullType(fieldType)) {
isNonNull = true;
fieldType = fieldType.ofType;
}
}
var _a = unwrapType(field.type), list = _a.list, fieldType = _a.type, isNonNull = _a.isNonNull;
var prefix = list.length === 1 ? "t.list." : "t.";

@@ -116,6 +106,12 @@ return " " + prefix + this.printFieldMethod(source, field, fieldType, list, isNonNull);

SDLConverter.prototype.printFieldMethod = function (source, field, type, list, isNonNull) {
var _this = this;
var objectMeta = {};
if (field.description) {
objectMeta.description = field.description;
var str = "";
if (isCommonScalar(type)) {
str += type.name.toLowerCase() + "(\"" + field.name + "\"";
}
else {
objectMeta.type = type;
str += "field(\"" + field.name + "\"";
}
if ("deprecationReason" in field && field.deprecationReason) {

@@ -133,18 +129,94 @@ objectMeta.deprecation = field.deprecationReason;

}
var str = "";
if (isCommonScalar(type)) {
str += type.name.toLowerCase() + "(\"" + field.name + "\"";
if (field.description) {
objectMeta.description = field.description;
}
if (source === "output") {
var outputField = field;
if (outputField.args.length) {
objectMeta.args = outputField.args;
}
}
else {
objectMeta.type = type;
str += "field(\"" + field.name + "\"";
var inputField = field;
if (inputField.defaultValue !== undefined) {
objectMeta.default = inputField.defaultValue;
}
}
if (Object.keys(objectMeta).length > 0) {
str += ", " + this.json.stringify(objectMeta);
var metaKeys = Object.keys(objectMeta);
if (metaKeys.length > 0) {
if (metaKeys.length === 1 && !objectMeta.args) {
var key = metaKeys[0];
str += ", { " + key + ": " + this.printMeta(objectMeta[key], key) + " }";
}
else {
str += ", {\n";
utils_1.eachObj(objectMeta, function (val, key) {
str += " " + key + ": " + _this.printMeta(val, key) + ",\n";
});
str += " }";
}
}
return str + ")";
};
SDLConverter.prototype.printMeta = function (val, key) {
var _this = this;
if (key === "type") {
return val;
}
if (key === "list" || key === "required") {
return Array.isArray(val)
? "[" + val.join(", ") + "]"
: this.json.stringify(val);
}
if (key === "args") {
var str_1 = "{\n";
val.forEach(function (arg) {
str_1 += " " + arg.name + ": " + _this.printArg(arg) + "\n";
});
str_1 += " }";
return str_1;
}
return this.json.stringify(val);
};
SDLConverter.prototype.printArg = function (arg) {
var description = arg.description;
var _a = unwrapType(arg.type), list = _a.list, isNonNull = _a.isNonNull, type = _a.type;
var isArg = !isCommonScalar(type);
var str = "";
if (isArg) {
this.usedImports.add("arg");
str += "arg(";
}
else {
this.usedImports.add(type.toString().toLowerCase() + "Arg");
str += type.toString().toLowerCase() + "Arg(";
}
var metaToAdd = [];
if (isArg) {
metaToAdd.push("type: " + type.name);
}
if (description) {
metaToAdd.push("description: " + JSON.stringify(description));
}
if (list.length) {
metaToAdd.push(list.length === 1 ? "list: true" : "list: [" + list.join(", ") + "]");
}
if (arg.defaultValue !== undefined) {
metaToAdd.push("default: " + this.json.stringify(arg.defaultValue));
}
if (isNonNull) {
metaToAdd.push("required: true");
}
str +=
metaToAdd.length > 1
? "{\n " + metaToAdd.join(",\n ") + "\n }"
: metaToAdd.length
? "{ " + metaToAdd[0] + " }"
: "";
return str + "),";
};
SDLConverter.prototype.printInterfaceTypes = function () {
var _this = this;
if (this.groupedTypes.interface.length) {
this.usedImports.add("interfaceType");
return this.groupedTypes.interface

@@ -171,2 +243,3 @@ .map(function (t) { return _this.printInterfaceType(t); })

if (this.groupedTypes.enum.length) {
this.usedImports.add("enumType");
return this.groupedTypes.enum

@@ -197,2 +270,3 @@ .map(function (t) { return _this.printEnumType(t); })

if (this.groupedTypes.input.length) {
this.usedImports.add("inputObjectType");
return this.groupedTypes.input

@@ -218,2 +292,3 @@ .map(function (t) { return _this.printInputObjectType(t); })

if (this.groupedTypes.union.length) {
this.usedImports.add("unionType");
return this.groupedTypes.union

@@ -226,2 +301,3 @@ .map(function (t) { return _this.printUnionType(t); })

SDLConverter.prototype.printUnionType = function (type) {
var _this = this;
return this.printBlock([

@@ -234,3 +310,3 @@ "" + this.export + type.name + " = unionType({",

.getTypes()
.map(function (t) { return JSON.stringify(t.name); })
.map(function (t) { return _this.json.stringify(t.name); })
.join(", ") + ")",

@@ -244,2 +320,3 @@ " }",

if (this.groupedTypes.scalar.length) {
this.usedImports.add("scalarType");
return this.groupedTypes.scalar

@@ -272,3 +349,3 @@ .filter(function (s) { return !graphql_1.isSpecifiedScalarType(s); })

if (type.description) {
return " description: " + JSON.stringify(type.description) + ",";
return " description: " + this.json.stringify(type.description) + ",";
}

@@ -283,2 +360,24 @@ return null;

exports.SDLConverter = SDLConverter;
function unwrapType(type) {
var finalType = type;
var isNonNull = false;
var list = [];
while (graphql_1.isWrappingType(finalType)) {
while (graphql_1.isListType(finalType)) {
finalType = finalType.ofType;
if (graphql_1.isNonNullType(finalType)) {
finalType = finalType.ofType;
list.unshift(true);
}
else {
list.unshift(false);
}
}
if (graphql_1.isNonNullType(finalType)) {
isNonNull = true;
finalType = finalType.ofType;
}
}
return { type: finalType, isNonNull: isNonNull, list: list };
}
function isCommonScalar(field) {

@@ -285,0 +384,0 @@ if (graphql_1.isScalarType(field)) {

@@ -15,6 +15,2 @@ import { GraphQLSchema, GraphQLNamedType } from "graphql";

/**
* Deprecated, use 'source'
*/
module?: string;
/**
* The module for where to look for the types.

@@ -62,3 +58,3 @@ * This uses the node resolution algorthm via require.resolve,

/**
* Array of files to match for a type
* Array of TypegenConfigSourceModule's to look in and match the type names against.
*

@@ -65,0 +61,0 @@ * ```

{
"name": "nexus",
"version": "0.9.11",
"version": "0.9.12",
"main": "dist",

@@ -78,4 +78,4 @@ "types": "dist",

"type": "git",
"url": "git://github.com/graphql-nexus/nexus.git"
"url": "git://github.com/prisma/nexus.git"
}
}

@@ -185,13 +185,3 @@ import {

};
if (this.isList) {
if (field.list) {
console.warn(
`It looks like you chained .list and set list for ${name}` +
"You should only do one or the other"
);
} else {
field.list = true;
}
}
this.typeBuilder.addField(field);
this.typeBuilder.addField(this.decorateField(field));
}

@@ -213,4 +203,18 @@

}
this.typeBuilder.addField(config);
this.typeBuilder.addField(this.decorateField(config));
}
protected decorateField(config: NexusOutputFieldDef): NexusOutputFieldDef {
if (this.isList) {
if (config.list) {
console.warn(
`It looks like you chained .list and set list for ${config.name}` +
"You should only do one or the other"
);
} else {
config.list = true;
}
}
return config;
}
}

@@ -284,6 +288,8 @@

) {
this.typeBuilder.addField({
name: fieldName,
...fieldConfig,
});
this.typeBuilder.addField(
this.decorateField({
name: fieldName,
...fieldConfig,
})
);
}

@@ -296,8 +302,24 @@

) {
this.typeBuilder.addField({
name: fieldName,
type: typeName,
...opts,
});
this.typeBuilder.addField(
this.decorateField({
name: fieldName,
type: typeName,
...opts,
})
);
}
protected decorateField(config: NexusOutputFieldDef): NexusOutputFieldDef {
if (this.isList) {
if (config.list) {
console.warn(
`It looks like you chained .list and set list for ${config.name}` +
"You should only do one or the other"
);
} else {
config.list = true;
}
}
return config;
}
}

@@ -304,0 +326,0 @@

@@ -21,4 +21,12 @@ import {

GraphQLInputField,
GraphQLArgument,
GraphQLType,
} from "graphql";
import { groupTypes, GroupedTypes, isInterfaceField, objValues } from "./utils";
import {
groupTypes,
GroupedTypes,
isInterfaceField,
objValues,
eachObj,
} from "./utils";

@@ -44,6 +52,7 @@ export function convertSDL(

protected groupedTypes: GroupedTypes;
protected usedImports: Set<string> = new Set();
constructor(
sdl: string,
commonjs: null | boolean = false,
protected commonjs: null | boolean = false,
protected json: JSON = JSON

@@ -58,3 +67,3 @@ ) {

print() {
return [
const body = [
this.printObjectTypes(),

@@ -66,3 +75,5 @@ this.printInterfaceTypes(),

this.printScalarTypes(),
]
];
return [this.printUsedImports()]
.concat(body)
.filter((f) => f)

@@ -72,4 +83,16 @@ .join("\n\n");

printUsedImports() {
if (this.commonjs) {
return `const { ${Array.from(this.usedImports).join(
", "
)} } = require('nexus');`;
}
return `import { ${Array.from(this.usedImports).join(
", "
)} } from 'nexus';`;
}
printObjectTypes() {
if (this.groupedTypes.object.length > 0) {
this.usedImports.add("objectType");
return this.groupedTypes.object

@@ -86,5 +109,3 @@ .map((t) => this.printObjectType(t))

implementing.length > 0
? ` t.implements(${implementing
.map((i) => this.json.stringify(i))
.join(", ")})`
? ` t.implements(${implementing.join(", ")})`
: "";

@@ -125,20 +146,3 @@ return this.printBlock([

) {
let fieldType = field.type;
let isNonNull = false;
const list = [];
while (isWrappingType(fieldType)) {
while (isListType(fieldType)) {
fieldType = fieldType.ofType;
if (isNonNullType(fieldType)) {
fieldType = fieldType.ofType;
list.unshift(true);
} else {
list.unshift(false);
}
}
if (isNonNullType(fieldType)) {
isNonNull = true;
fieldType = fieldType.ofType;
}
}
const { list, type: fieldType, isNonNull } = unwrapType(field.type);
const prefix = list.length === 1 ? `t.list.` : `t.`;

@@ -164,4 +168,8 @@ return ` ${prefix}${this.printFieldMethod(

const objectMeta: Record<string, any> = {};
if (field.description) {
objectMeta.description = field.description;
let str = "";
if (isCommonScalar(type)) {
str += `${type.name.toLowerCase()}("${field.name}"`;
} else {
objectMeta.type = type;
str += `field("${field.name}"`;
}

@@ -179,11 +187,28 @@ if ("deprecationReason" in field && field.deprecationReason) {

}
let str = "";
if (isCommonScalar(type)) {
str += `${type.name.toLowerCase()}("${field.name}"`;
if (field.description) {
objectMeta.description = field.description;
}
if (source === "output") {
const outputField = field as GraphQLField<any, any>;
if (outputField.args.length) {
objectMeta.args = outputField.args;
}
} else {
objectMeta.type = type;
str += `field("${field.name}"`;
const inputField = field as GraphQLInputField;
if (inputField.defaultValue !== undefined) {
objectMeta.default = inputField.defaultValue;
}
}
if (Object.keys(objectMeta).length > 0) {
str += `, ${this.json.stringify(objectMeta)}`;
const metaKeys = Object.keys(objectMeta);
if (metaKeys.length > 0) {
if (metaKeys.length === 1 && !objectMeta.args) {
const key = metaKeys[0];
str += `, { ${key}: ${this.printMeta(objectMeta[key], key)} }`;
} else {
str += `, {\n`;
eachObj(objectMeta, (val, key) => {
str += ` ${key}: ${this.printMeta(val, key)},\n`;
});
str += ` }`;
}
}

@@ -193,4 +218,64 @@ return `${str})`;

printMeta(val: any, key: string) {
if (key === "type") {
return val;
}
if (key === "list" || key === "required") {
return Array.isArray(val)
? `[${val.join(", ")}]`
: this.json.stringify(val);
}
if (key === "args") {
let str = `{\n`;
(val as GraphQLArgument[]).forEach((arg) => {
str += ` ${arg.name}: ${this.printArg(arg)}\n`;
});
str += ` }`;
return str;
}
return this.json.stringify(val);
}
printArg(arg: GraphQLArgument) {
const description = arg.description;
const { list, isNonNull, type } = unwrapType(arg.type);
const isArg = !isCommonScalar(type);
let str = "";
if (isArg) {
this.usedImports.add("arg");
str += `arg(`;
} else {
this.usedImports.add(`${type.toString().toLowerCase()}Arg`);
str += `${type.toString().toLowerCase()}Arg(`;
}
const metaToAdd = [];
if (isArg) {
metaToAdd.push(`type: ${type.name}`);
}
if (description) {
metaToAdd.push(`description: ${JSON.stringify(description)}`);
}
if (list.length) {
metaToAdd.push(
list.length === 1 ? `list: true` : `list: [${list.join(", ")}]`
);
}
if (arg.defaultValue !== undefined) {
metaToAdd.push(`default: ${this.json.stringify(arg.defaultValue)}`);
}
if (isNonNull) {
metaToAdd.push("required: true");
}
str +=
metaToAdd.length > 1
? `{\n ${metaToAdd.join(",\n ")}\n }`
: metaToAdd.length
? `{ ${metaToAdd[0]} }`
: "";
return `${str}),`;
}
printInterfaceTypes() {
if (this.groupedTypes.interface.length) {
this.usedImports.add("interfaceType");
return this.groupedTypes.interface

@@ -218,2 +303,3 @@ .map((t) => this.printInterfaceType(t))

if (this.groupedTypes.enum.length) {
this.usedImports.add("enumType");
return this.groupedTypes.enum

@@ -245,2 +331,3 @@ .map((t) => this.printEnumType(t))

if (this.groupedTypes.input.length) {
this.usedImports.add("inputObjectType");
return this.groupedTypes.input

@@ -267,2 +354,3 @@ .map((t) => this.printInputObjectType(t))

if (this.groupedTypes.union.length) {
this.usedImports.add("unionType");
return this.groupedTypes.union

@@ -283,3 +371,3 @@ .map((t) => this.printUnionType(t))

.getTypes()
.map((t) => JSON.stringify(t.name))
.map((t) => this.json.stringify(t.name))
.join(", ")})`,

@@ -293,2 +381,3 @@ ` }`,

if (this.groupedTypes.scalar.length) {
this.usedImports.add("scalarType");
return this.groupedTypes.scalar

@@ -324,3 +413,3 @@ .filter((s) => !isSpecifiedScalarType(s))

if (type.description) {
return ` description: ${JSON.stringify(type.description)},`;
return ` description: ${this.json.stringify(type.description)},`;
}

@@ -335,2 +424,24 @@ return null;

function unwrapType(type: GraphQLType) {
let finalType = type;
let isNonNull = false;
const list = [];
while (isWrappingType(finalType)) {
while (isListType(finalType)) {
finalType = finalType.ofType;
if (isNonNullType(finalType)) {
finalType = finalType.ofType;
list.unshift(true);
} else {
list.unshift(false);
}
}
if (isNonNullType(finalType)) {
isNonNull = true;
finalType = finalType.ofType;
}
}
return { type: finalType, isNonNull, list };
}
function isCommonScalar(field: GraphQLNamedType): boolean {

@@ -337,0 +448,0 @@ if (isScalarType(field)) {

@@ -25,6 +25,2 @@ import {

/**
* Deprecated, use 'source'
*/
module?: string;
/**
* The module for where to look for the types.

@@ -73,3 +69,3 @@ * This uses the node resolution algorthm via require.resolve,

/**
* Array of files to match for a type
* Array of TypegenConfigSourceModule's to look in and match the type names against.
*

@@ -76,0 +72,0 @@ * ```

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