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

nexus

Package Overview
Dependencies
Maintainers
4
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 1.1.1-next.2 to 1.2.0-next.3

22

dist-esm/builder.d.ts

@@ -31,2 +31,22 @@ import { GraphQLField, GraphQLFieldConfig, GraphQLFieldConfigArgumentMap, GraphQLFieldConfigMap, GraphQLFieldResolver, GraphQLInputFieldConfig, GraphQLInputFieldConfigMap, GraphQLInputObjectType, GraphQLInputType, GraphQLInterfaceType, GraphQLList, GraphQLNamedType, GraphQLNonNull, GraphQLObjectType, GraphQLOutputType, GraphQLSchemaConfig, GraphQLType, printSchema } from 'graphql';

declare type PossibleInputType = string | AllNexusNamedInputTypeDefs | GraphQLType;
export interface ConfiguredTypegen {
/** Path for the generated type defs */
outputPath: string;
/**
* Determine the path the "globals" are output, useful when you have a monorepo setup and need to isolate
* the globals from the rest of the types in order to have multiple schemas/ts projects
*/
globalsPath?: string;
/**
* If globalsPath is defined, these headers are added to the "globals" generated file, rather than the
* typegen generated file
*/
globalsHeaders?: string[];
/**
* If "true", declares dedicated interfaces for any inputs / args
*
* @default true
*/
declareInputs?: boolean;
}
export interface BuilderConfigInput {

@@ -49,3 +69,3 @@ /**

*/
typegen?: boolean | string;
typegen?: boolean | string | ConfiguredTypegen;
/**

@@ -52,0 +72,0 @@ * GraphQL SDL file generation toggle and location.

5

dist-esm/makeSchema.d.ts

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

import { SchemaConfig } from './builder';
import { ConfiguredTypegen, SchemaConfig } from './builder';
import type { NexusGraphQLSchema } from './definitions/_types';

@@ -13,7 +13,8 @@ /**

export declare namespace generateSchema {
var withArtifacts: (config: SchemaConfig, typeFilePath?: string | null) => Promise<{
var withArtifacts: (config: SchemaConfig, typegen?: string | ConfiguredTypegen | null) => Promise<{
schema: NexusGraphQLSchema;
schemaTypes: string;
tsTypes: string;
globalTypes: string | null;
}>;
}

@@ -59,10 +59,10 @@ import { __awaiter } from "tslib";

*/
generateSchema.withArtifacts = (config, typeFilePath = null) => __awaiter(void 0, void 0, void 0, function* () {
generateSchema.withArtifacts = (config, typegen = null) => __awaiter(void 0, void 0, void 0, function* () {
const { schema, missingTypes, finalConfig } = makeSchemaInternal(config);
const typegenConfig = resolveTypegenConfig(finalConfig);
const { schemaTypes, tsTypes } = yield new TypegenMetadata(typegenConfig).generateArtifactContents(schema, typeFilePath);
const { schemaTypes, tsTypes, globalTypes } = yield new TypegenMetadata(typegenConfig).generateArtifactContents(schema, typegen);
assertNoMissingTypes(schema, missingTypes);
runAbstractTypeRuntimeChecks(schema, finalConfig.features);
return { schema, schemaTypes, tsTypes };
return { schema, schemaTypes, tsTypes, globalTypes };
});
//# sourceMappingURL=makeSchema.js.map
import { GraphQLSchema } from 'graphql';
import type { BuilderConfigInput, TypegenInfo } from './builder';
import type { ConfiguredTypegen } from './core';
import type { NexusGraphQLSchema } from './definitions/_types';

@@ -8,3 +9,3 @@ export interface TypegenMetadataConfig extends Omit<BuilderConfigInput, 'outputs' | 'shouldGenerateArtifacts'> {

schema: null | string;
typegen: null | string;
typegen: null | string | ConfiguredTypegen;
};

@@ -21,5 +22,6 @@ }

generateArtifacts(schema: NexusGraphQLSchema): Promise<void>;
generateArtifactContents(schema: NexusGraphQLSchema, typeFilePath: string | null): Promise<{
generateArtifactContents(schema: NexusGraphQLSchema, typegen: string | null | ConfiguredTypegen): Promise<{
schemaTypes: string;
tsTypes: string;
globalTypes: string | null;
}>;

@@ -32,3 +34,12 @@ sortSchema(schema: NexusGraphQLSchema): NexusGraphQLSchema;

generateTypesFile(schema: NexusGraphQLSchema, typegenPath: string): Promise<string>;
/** Generates the type definitions */
generateConfiguredTypes(schema: NexusGraphQLSchema, typegen: ConfiguredTypegen): Promise<{
tsTypes: string;
globalTypes: string;
} | {
tsTypes: string;
globalTypes: null;
}>;
getTypegenInfo(schema: GraphQLSchema, typegenPath?: string): Promise<TypegenInfo>;
private normalizeTypegenPath;
}

@@ -20,9 +20,18 @@ import { __awaiter } from "tslib";

const sortedSchema = this.sortSchema(schema);
if (this.config.outputs.schema || this.config.outputs.typegen) {
const { schemaTypes, tsTypes } = yield this.generateArtifactContents(sortedSchema, this.config.outputs.typegen);
const { typegen } = this.config.outputs;
if (this.config.outputs.schema || typegen) {
const { schemaTypes, tsTypes, globalTypes } = yield this.generateArtifactContents(sortedSchema, typegen);
if (this.config.outputs.schema) {
yield this.writeFile('schema', schemaTypes, this.config.outputs.schema);
}
if (this.config.outputs.typegen) {
yield this.writeFile('types', tsTypes, this.config.outputs.typegen);
if (typegen) {
if (typeof typegen === 'string') {
yield this.writeFile('types', tsTypes, typegen);
}
else {
yield this.writeFile('types', tsTypes, typegen.outputPath);
if (typeof typegen.globalsPath === 'string') {
yield this.writeFile('types', globalTypes !== null && globalTypes !== void 0 ? globalTypes : '', typegen.globalsPath);
}
}
}

@@ -32,9 +41,21 @@ }

}
generateArtifactContents(schema, typeFilePath) {
generateArtifactContents(schema, typegen) {
return __awaiter(this, void 0, void 0, function* () {
const [schemaTypes, tsTypes] = yield Promise.all([
this.generateSchemaFile(schema),
typeFilePath ? this.generateTypesFile(schema, typeFilePath) : '',
]);
return { schemaTypes, tsTypes };
const result = {
schemaTypes: this.generateSchemaFile(schema),
tsTypes: '',
globalTypes: null,
};
if (!typegen) {
return result;
}
if (typeof typegen === 'string') {
result.tsTypes = yield this.generateTypesFile(schema, typegen);
}
else {
const generateResult = yield this.generateConfiguredTypes(schema, typegen);
result.tsTypes = generateResult.tsTypes;
result.globalTypes = generateResult.globalTypes;
}
return result;
});

@@ -108,5 +129,15 @@ }

const typegenInfo = yield this.getTypegenInfo(schema, typegenPath);
return new TypegenPrinter(schema, Object.assign(Object.assign({}, typegenInfo), { typegenPath })).print();
return new TypegenPrinter(schema, Object.assign(Object.assign({ declareInputs: true }, typegenInfo), { typegenPath })).print();
});
}
/** Generates the type definitions */
generateConfiguredTypes(schema, typegen) {
return __awaiter(this, void 0, void 0, function* () {
const { outputPath: typegenPath, globalsPath, declareInputs = true } = typegen;
const typegenInfo = yield this.getTypegenInfo(schema, typegenPath);
return new TypegenPrinter(schema, Object.assign(Object.assign({}, typegenInfo), { typegenPath,
globalsPath,
declareInputs })).printConfigured();
});
}
getTypegenInfo(schema, typegenPath) {

@@ -118,3 +149,3 @@ return __awaiter(this, void 0, void 0, function* () {

if (this.config.sourceTypes) {
return typegenAutoConfig(this.config.sourceTypes, this.config.contextType)(schema, typegenPath || this.config.outputs.typegen || '');
return typegenAutoConfig(this.config.sourceTypes, this.config.contextType)(schema, typegenPath || this.normalizeTypegenPath(this.config.outputs.typegen) || '');
}

@@ -130,3 +161,6 @@ return {

}
normalizeTypegenPath(typegen) {
return typeof typegen === 'string' ? typegen : typegen ? typegen.outputPath : null;
}
}
//# sourceMappingURL=typegenMetadata.js.map

@@ -11,2 +11,5 @@ import { GraphQLAbstractType, GraphQLArgument, GraphQLField, GraphQLInputField, GraphQLInputType, GraphQLInterfaceType, GraphQLObjectType, GraphQLOutputType, GraphQLScalarType, GraphQLUnionType } from 'graphql';

typegenPath: string;
globalsPath?: string;
globalsHeaders?: string;
declareInputs?: boolean;
}

@@ -32,3 +35,13 @@ /**

print(): string;
printConfigured(): {
tsTypes: string;
globalTypes: string;
} | {
tsTypes: string;
globalTypes: null;
};
private printCommon;
printHeaders(): string;
private printHeadersCommon;
private printHeadersGlobal;
printGenTypeMap(): string;

@@ -67,2 +80,5 @@ printDynamicImport(): string;

printArgTypeMap(): string;
private getArgsName;
printNamedObj(name: string, obj: Record<string, [string, string]>): string;
printNamedMap(name: string, obj: Record<string, any>): string;
buildReturnTypeMap(): Record<string, Record<string, [string, string]>>;

@@ -69,0 +85,0 @@ buildReturnTypeNamesMap(): Record<string, Record<string, [string, string]>>;

import { __rest } from "tslib";
import { getNamedType, isEnumType, isInputObjectType, isInterfaceType, isListType, isNonNullType, isObjectType, isScalarType, isSpecifiedScalarType, isUnionType, } from 'graphql';
import { isNexusPrintedGenTyping, isNexusPrintedGenTypingImport } from './definitions/wrapping';
import { eachObj, getOwnPackage, graphql15InterfaceType, groupTypes, mapObj, mapValues, resolveImportPath, } from './utils';
import { TYPEGEN_HEADER } from './lang';
import { eachObj, getOwnPackage, graphql15InterfaceType, groupTypes, mapObj, mapValues, relativePathTo, resolveImportPath, } from './utils';
const SpecifiedScalars = {

@@ -40,3 +41,22 @@ ID: 'string',

print() {
const body = [
const body = [this.printCommon(), this.printPlugins()].join('\n\n');
return [this.printHeaders(), body].join('\n\n');
}
printConfigured() {
if (this.typegenInfo.globalsPath) {
const common = this.printCommon();
const tsTypes = [this.printHeadersCommon(), common].join('\n\n');
const globalTypes = [this.printHeadersGlobal(), this.printPlugins()].join('\n\n');
return {
tsTypes,
globalTypes,
};
}
return {
tsTypes: this.print(),
globalTypes: null,
};
}
printCommon() {
return [
this.printInputTypeMap(),

@@ -65,12 +85,8 @@ this.printEnumTypeMap(),

this.printGenTypeMap(),
this.printPlugins(),
].join('\n\n');
return [this.printHeaders(), body].join('\n\n');
}
printHeaders() {
const fieldDefs = [
this.printDynamicInputFieldDefinitions(),
this.printDynamicOutputFieldDefinitions(),
this.printDynamicOutputPropertyDefinitions(),
];
return [this.printHeadersCommon(), this.printHeadersGlobal()].join('\n');
}
printHeadersCommon() {
return [

@@ -80,6 +96,18 @@ this.typegenInfo.headers.join('\n'),

this.printDynamicImport(),
...fieldDefs,
GLOBAL_DECLARATION,
].join('\n');
}
printHeadersGlobal() {
var _a, _b;
const headers = [
this.printDynamicInputFieldDefinitions(),
this.printDynamicOutputFieldDefinitions(),
this.printDynamicOutputPropertyDefinitions(),
GLOBAL_DECLARATION,
];
if (this.typegenInfo.globalsPath) {
headers.unshift(`import { NexusGenTypes } from '${relativePathTo(this.typegenInfo.typegenPath, (_a = this.typegenInfo.globalsPath) !== null && _a !== void 0 ? _a : '')}'`);
headers.unshift((_b = this.typegenInfo.globalsHeaders) !== null && _b !== void 0 ? _b : TYPEGEN_HEADER);
}
return headers.join('\n');
}
printGenTypeMap() {

@@ -372,6 +400,18 @@ return [`export interface NexusGenTypes {`]

printInputTypeMap() {
return this.printTypeFieldInterface('NexusGenInputs', this.buildInputTypeMap(), 'input type');
const inputTypeMap = this.buildInputTypeMap();
if (this.typegenInfo.declareInputs) {
const declaredInputs = mapObj(inputTypeMap, (fields, inputName) => this.printNamedObj(inputName, fields));
return [...declaredInputs, this.printNamedMap('NexusGenInputs', inputTypeMap)].join('\n\n');
}
return this.printTypeFieldInterface('NexusGenInputs', inputTypeMap, 'input type');
}
printEnumTypeMap() {
return this.printTypeInterface('NexusGenEnums', this.buildEnumTypeMap());
const enumTypeMap = this.buildEnumTypeMap();
if (this.typegenInfo.declareInputs) {
return [
...mapObj(enumTypeMap, (val, name) => `export type ${name} = ${val}`),
this.printNamedMap('NexusGenEnums', enumTypeMap),
].join('\n\n');
}
return this.printTypeInterface('NexusGenEnums', enumTypeMap);
}

@@ -511,4 +551,27 @@ printScalarTypeMap() {

printArgTypeMap() {
return this.printArgTypeFieldInterface(this.buildArgTypeMap());
const argTypeMap = this.buildArgTypeMap();
if (this.typegenInfo.declareInputs) {
const declaredArgs = [];
eachObj(argTypeMap, (fields, typeName) => {
eachObj(fields, (args, fieldName) => {
declaredArgs.push(this.printNamedObj(this.getArgsName(typeName, fieldName), args));
});
});
return [...declaredArgs, this.printArgTypeFieldInterface(argTypeMap)].join('\n\n');
}
return this.printArgTypeFieldInterface(argTypeMap);
}
getArgsName(typeName, fieldName) {
return `${typeName}${fieldName.slice(0, 1).toUpperCase().concat(fieldName.slice(1))}Args`;
}
printNamedObj(name, obj) {
return [
`export interface ${name} {`,
...mapObj(obj, (val, key) => ` ${key}${val[0]} ${val[1]}`),
`}`,
].join('\n');
}
printNamedMap(name, obj) {
return [`export interface ${name} {`, ...mapObj(obj, (val, key) => ` ${key}: ${key}`), `}`].join('\n');
}
buildReturnTypeMap() {

@@ -575,3 +638,8 @@ const returnTypeMap = {};

else if (isEnumType(type)) {
typing.push(`NexusGenEnums['${type.name}']`);
if (this.typegenInfo.declareInputs) {
typing.push(type.name);
}
else {
typing.push(`NexusGenEnums['${type.name}']`);
}
}

@@ -631,6 +699,16 @@ else if (isObjectType(type) || isInterfaceType(type) || isUnionType(type)) {

else if (isEnumType(arg)) {
typing.push(`NexusGenEnums['${arg.name}']`);
if (this.typegenInfo.declareInputs) {
typing.push(arg.name);
}
else {
typing.push(`NexusGenEnums['${arg.name}']`);
}
}
else if (isInputObjectType(arg)) {
typing.push(`NexusGenInputs['${arg.name}']`);
if (this.typegenInfo.declareInputs) {
typing.push(arg.name);
}
else {
typing.push(`NexusGenInputs['${arg.name}']`);
}
}

@@ -667,4 +745,10 @@ return typing;

return [`export interface NexusGenArgTypes {`]
.concat(mapObj(typeMapping, (val, key) => {
return [` ${key}: {`]
.concat(mapObj(typeMapping, (val, typeName) => {
if (this.typegenInfo.declareInputs) {
return [` ${typeName}: {`]
.concat(mapObj(val, (_, fieldName) => ` ${fieldName}: ${this.getArgsName(typeName, fieldName)}`))
.concat(' }')
.join('\n');
}
return [` ${typeName}: {`]
.concat(mapObj(val, this.printObj(' ', 'args')))

@@ -671,0 +755,0 @@ .concat(' }')

@@ -31,2 +31,8 @@ import { __rest } from "tslib";

}
else if (typeof outputs.typegen === 'object') {
typegenFilePath = Object.assign(Object.assign({}, outputs.typegen), { outputPath: assertAbsolutePath(outputs.typegen.outputPath, 'outputs.typegen.outputPath') });
if (outputs.typegen.globalsPath) {
typegenFilePath.globalsPath = assertAbsolutePath(outputs.typegen.globalsPath, 'outputs.typegen.globalsPath');
}
}
}

@@ -33,0 +39,0 @@ else if (outputs !== false) {

@@ -31,2 +31,22 @@ import { GraphQLField, GraphQLFieldConfig, GraphQLFieldConfigArgumentMap, GraphQLFieldConfigMap, GraphQLFieldResolver, GraphQLInputFieldConfig, GraphQLInputFieldConfigMap, GraphQLInputObjectType, GraphQLInputType, GraphQLInterfaceType, GraphQLList, GraphQLNamedType, GraphQLNonNull, GraphQLObjectType, GraphQLOutputType, GraphQLSchemaConfig, GraphQLType, printSchema } from 'graphql';

declare type PossibleInputType = string | AllNexusNamedInputTypeDefs | GraphQLType;
export interface ConfiguredTypegen {
/** Path for the generated type defs */
outputPath: string;
/**
* Determine the path the "globals" are output, useful when you have a monorepo setup and need to isolate
* the globals from the rest of the types in order to have multiple schemas/ts projects
*/
globalsPath?: string;
/**
* If globalsPath is defined, these headers are added to the "globals" generated file, rather than the
* typegen generated file
*/
globalsHeaders?: string[];
/**
* If "true", declares dedicated interfaces for any inputs / args
*
* @default true
*/
declareInputs?: boolean;
}
export interface BuilderConfigInput {

@@ -49,3 +69,3 @@ /**

*/
typegen?: boolean | string;
typegen?: boolean | string | ConfiguredTypegen;
/**

@@ -52,0 +72,0 @@ * GraphQL SDL file generation toggle and location.

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

import { SchemaConfig } from './builder';
import { ConfiguredTypegen, SchemaConfig } from './builder';
import type { NexusGraphQLSchema } from './definitions/_types';

@@ -13,7 +13,8 @@ /**

export declare namespace generateSchema {
var withArtifacts: (config: SchemaConfig, typeFilePath?: string | null) => Promise<{
var withArtifacts: (config: SchemaConfig, typegen?: string | ConfiguredTypegen | null) => Promise<{
schema: NexusGraphQLSchema;
schemaTypes: string;
tsTypes: string;
globalTypes: string | null;
}>;
}

@@ -64,10 +64,10 @@ "use strict";

*/
generateSchema.withArtifacts = (config, typeFilePath = null) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
generateSchema.withArtifacts = (config, typegen = null) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const { schema, missingTypes, finalConfig } = builder_1.makeSchemaInternal(config);
const typegenConfig = typegenUtils_1.resolveTypegenConfig(finalConfig);
const { schemaTypes, tsTypes } = yield new typegenMetadata_1.TypegenMetadata(typegenConfig).generateArtifactContents(schema, typeFilePath);
const { schemaTypes, tsTypes, globalTypes } = yield new typegenMetadata_1.TypegenMetadata(typegenConfig).generateArtifactContents(schema, typegen);
utils_1.assertNoMissingTypes(schema, missingTypes);
utils_1.runAbstractTypeRuntimeChecks(schema, finalConfig.features);
return { schema, schemaTypes, tsTypes };
return { schema, schemaTypes, tsTypes, globalTypes };
});
//# sourceMappingURL=makeSchema.js.map
import { GraphQLSchema } from 'graphql';
import type { BuilderConfigInput, TypegenInfo } from './builder';
import type { ConfiguredTypegen } from './core';
import type { NexusGraphQLSchema } from './definitions/_types';

@@ -8,3 +9,3 @@ export interface TypegenMetadataConfig extends Omit<BuilderConfigInput, 'outputs' | 'shouldGenerateArtifacts'> {

schema: null | string;
typegen: null | string;
typegen: null | string | ConfiguredTypegen;
};

@@ -21,5 +22,6 @@ }

generateArtifacts(schema: NexusGraphQLSchema): Promise<void>;
generateArtifactContents(schema: NexusGraphQLSchema, typeFilePath: string | null): Promise<{
generateArtifactContents(schema: NexusGraphQLSchema, typegen: string | null | ConfiguredTypegen): Promise<{
schemaTypes: string;
tsTypes: string;
globalTypes: string | null;
}>;

@@ -32,3 +34,12 @@ sortSchema(schema: NexusGraphQLSchema): NexusGraphQLSchema;

generateTypesFile(schema: NexusGraphQLSchema, typegenPath: string): Promise<string>;
/** Generates the type definitions */
generateConfiguredTypes(schema: NexusGraphQLSchema, typegen: ConfiguredTypegen): Promise<{
tsTypes: string;
globalTypes: string;
} | {
tsTypes: string;
globalTypes: null;
}>;
getTypegenInfo(schema: GraphQLSchema, typegenPath?: string): Promise<TypegenInfo>;
private normalizeTypegenPath;
}

@@ -23,9 +23,18 @@ "use strict";

const sortedSchema = this.sortSchema(schema);
if (this.config.outputs.schema || this.config.outputs.typegen) {
const { schemaTypes, tsTypes } = yield this.generateArtifactContents(sortedSchema, this.config.outputs.typegen);
const { typegen } = this.config.outputs;
if (this.config.outputs.schema || typegen) {
const { schemaTypes, tsTypes, globalTypes } = yield this.generateArtifactContents(sortedSchema, typegen);
if (this.config.outputs.schema) {
yield this.writeFile('schema', schemaTypes, this.config.outputs.schema);
}
if (this.config.outputs.typegen) {
yield this.writeFile('types', tsTypes, this.config.outputs.typegen);
if (typegen) {
if (typeof typegen === 'string') {
yield this.writeFile('types', tsTypes, typegen);
}
else {
yield this.writeFile('types', tsTypes, typegen.outputPath);
if (typeof typegen.globalsPath === 'string') {
yield this.writeFile('types', globalTypes !== null && globalTypes !== void 0 ? globalTypes : '', typegen.globalsPath);
}
}
}

@@ -35,9 +44,21 @@ }

}
generateArtifactContents(schema, typeFilePath) {
generateArtifactContents(schema, typegen) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const [schemaTypes, tsTypes] = yield Promise.all([
this.generateSchemaFile(schema),
typeFilePath ? this.generateTypesFile(schema, typeFilePath) : '',
]);
return { schemaTypes, tsTypes };
const result = {
schemaTypes: this.generateSchemaFile(schema),
tsTypes: '',
globalTypes: null,
};
if (!typegen) {
return result;
}
if (typeof typegen === 'string') {
result.tsTypes = yield this.generateTypesFile(schema, typegen);
}
else {
const generateResult = yield this.generateConfiguredTypes(schema, typegen);
result.tsTypes = generateResult.tsTypes;
result.globalTypes = generateResult.globalTypes;
}
return result;
});

@@ -111,5 +132,15 @@ }

const typegenInfo = yield this.getTypegenInfo(schema, typegenPath);
return new typegenPrinter_1.TypegenPrinter(schema, Object.assign(Object.assign({}, typegenInfo), { typegenPath })).print();
return new typegenPrinter_1.TypegenPrinter(schema, Object.assign(Object.assign({ declareInputs: true }, typegenInfo), { typegenPath })).print();
});
}
/** Generates the type definitions */
generateConfiguredTypes(schema, typegen) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const { outputPath: typegenPath, globalsPath, declareInputs = true } = typegen;
const typegenInfo = yield this.getTypegenInfo(schema, typegenPath);
return new typegenPrinter_1.TypegenPrinter(schema, Object.assign(Object.assign({}, typegenInfo), { typegenPath,
globalsPath,
declareInputs })).printConfigured();
});
}
getTypegenInfo(schema, typegenPath) {

@@ -121,3 +152,3 @@ return tslib_1.__awaiter(this, void 0, void 0, function* () {

if (this.config.sourceTypes) {
return typegenAutoConfig_1.typegenAutoConfig(this.config.sourceTypes, this.config.contextType)(schema, typegenPath || this.config.outputs.typegen || '');
return typegenAutoConfig_1.typegenAutoConfig(this.config.sourceTypes, this.config.contextType)(schema, typegenPath || this.normalizeTypegenPath(this.config.outputs.typegen) || '');
}

@@ -133,4 +164,7 @@ return {

}
normalizeTypegenPath(typegen) {
return typeof typegen === 'string' ? typegen : typegen ? typegen.outputPath : null;
}
}
exports.TypegenMetadata = TypegenMetadata;
//# sourceMappingURL=typegenMetadata.js.map

@@ -11,2 +11,5 @@ import { GraphQLAbstractType, GraphQLArgument, GraphQLField, GraphQLInputField, GraphQLInputType, GraphQLInterfaceType, GraphQLObjectType, GraphQLOutputType, GraphQLScalarType, GraphQLUnionType } from 'graphql';

typegenPath: string;
globalsPath?: string;
globalsHeaders?: string;
declareInputs?: boolean;
}

@@ -32,3 +35,13 @@ /**

print(): string;
printConfigured(): {
tsTypes: string;
globalTypes: string;
} | {
tsTypes: string;
globalTypes: null;
};
private printCommon;
printHeaders(): string;
private printHeadersCommon;
private printHeadersGlobal;
printGenTypeMap(): string;

@@ -67,2 +80,5 @@ printDynamicImport(): string;

printArgTypeMap(): string;
private getArgsName;
printNamedObj(name: string, obj: Record<string, [string, string]>): string;
printNamedMap(name: string, obj: Record<string, any>): string;
buildReturnTypeMap(): Record<string, Record<string, [string, string]>>;

@@ -69,0 +85,0 @@ buildReturnTypeNamesMap(): Record<string, Record<string, [string, string]>>;

@@ -7,2 +7,3 @@ "use strict";

const wrapping_1 = require("./definitions/wrapping");
const lang_1 = require("./lang");
const utils_1 = require("./utils");

@@ -44,3 +45,22 @@ const SpecifiedScalars = {

print() {
const body = [
const body = [this.printCommon(), this.printPlugins()].join('\n\n');
return [this.printHeaders(), body].join('\n\n');
}
printConfigured() {
if (this.typegenInfo.globalsPath) {
const common = this.printCommon();
const tsTypes = [this.printHeadersCommon(), common].join('\n\n');
const globalTypes = [this.printHeadersGlobal(), this.printPlugins()].join('\n\n');
return {
tsTypes,
globalTypes,
};
}
return {
tsTypes: this.print(),
globalTypes: null,
};
}
printCommon() {
return [
this.printInputTypeMap(),

@@ -69,12 +89,8 @@ this.printEnumTypeMap(),

this.printGenTypeMap(),
this.printPlugins(),
].join('\n\n');
return [this.printHeaders(), body].join('\n\n');
}
printHeaders() {
const fieldDefs = [
this.printDynamicInputFieldDefinitions(),
this.printDynamicOutputFieldDefinitions(),
this.printDynamicOutputPropertyDefinitions(),
];
return [this.printHeadersCommon(), this.printHeadersGlobal()].join('\n');
}
printHeadersCommon() {
return [

@@ -84,6 +100,18 @@ this.typegenInfo.headers.join('\n'),

this.printDynamicImport(),
...fieldDefs,
GLOBAL_DECLARATION,
].join('\n');
}
printHeadersGlobal() {
var _a, _b;
const headers = [
this.printDynamicInputFieldDefinitions(),
this.printDynamicOutputFieldDefinitions(),
this.printDynamicOutputPropertyDefinitions(),
GLOBAL_DECLARATION,
];
if (this.typegenInfo.globalsPath) {
headers.unshift(`import { NexusGenTypes } from '${utils_1.relativePathTo(this.typegenInfo.typegenPath, (_a = this.typegenInfo.globalsPath) !== null && _a !== void 0 ? _a : '')}'`);
headers.unshift((_b = this.typegenInfo.globalsHeaders) !== null && _b !== void 0 ? _b : lang_1.TYPEGEN_HEADER);
}
return headers.join('\n');
}
printGenTypeMap() {

@@ -376,6 +404,18 @@ return [`export interface NexusGenTypes {`]

printInputTypeMap() {
return this.printTypeFieldInterface('NexusGenInputs', this.buildInputTypeMap(), 'input type');
const inputTypeMap = this.buildInputTypeMap();
if (this.typegenInfo.declareInputs) {
const declaredInputs = utils_1.mapObj(inputTypeMap, (fields, inputName) => this.printNamedObj(inputName, fields));
return [...declaredInputs, this.printNamedMap('NexusGenInputs', inputTypeMap)].join('\n\n');
}
return this.printTypeFieldInterface('NexusGenInputs', inputTypeMap, 'input type');
}
printEnumTypeMap() {
return this.printTypeInterface('NexusGenEnums', this.buildEnumTypeMap());
const enumTypeMap = this.buildEnumTypeMap();
if (this.typegenInfo.declareInputs) {
return [
...utils_1.mapObj(enumTypeMap, (val, name) => `export type ${name} = ${val}`),
this.printNamedMap('NexusGenEnums', enumTypeMap),
].join('\n\n');
}
return this.printTypeInterface('NexusGenEnums', enumTypeMap);
}

@@ -515,4 +555,27 @@ printScalarTypeMap() {

printArgTypeMap() {
return this.printArgTypeFieldInterface(this.buildArgTypeMap());
const argTypeMap = this.buildArgTypeMap();
if (this.typegenInfo.declareInputs) {
const declaredArgs = [];
utils_1.eachObj(argTypeMap, (fields, typeName) => {
utils_1.eachObj(fields, (args, fieldName) => {
declaredArgs.push(this.printNamedObj(this.getArgsName(typeName, fieldName), args));
});
});
return [...declaredArgs, this.printArgTypeFieldInterface(argTypeMap)].join('\n\n');
}
return this.printArgTypeFieldInterface(argTypeMap);
}
getArgsName(typeName, fieldName) {
return `${typeName}${fieldName.slice(0, 1).toUpperCase().concat(fieldName.slice(1))}Args`;
}
printNamedObj(name, obj) {
return [
`export interface ${name} {`,
...utils_1.mapObj(obj, (val, key) => ` ${key}${val[0]} ${val[1]}`),
`}`,
].join('\n');
}
printNamedMap(name, obj) {
return [`export interface ${name} {`, ...utils_1.mapObj(obj, (val, key) => ` ${key}: ${key}`), `}`].join('\n');
}
buildReturnTypeMap() {

@@ -579,3 +642,8 @@ const returnTypeMap = {};

else if (graphql_1.isEnumType(type)) {
typing.push(`NexusGenEnums['${type.name}']`);
if (this.typegenInfo.declareInputs) {
typing.push(type.name);
}
else {
typing.push(`NexusGenEnums['${type.name}']`);
}
}

@@ -635,6 +703,16 @@ else if (graphql_1.isObjectType(type) || graphql_1.isInterfaceType(type) || graphql_1.isUnionType(type)) {

else if (graphql_1.isEnumType(arg)) {
typing.push(`NexusGenEnums['${arg.name}']`);
if (this.typegenInfo.declareInputs) {
typing.push(arg.name);
}
else {
typing.push(`NexusGenEnums['${arg.name}']`);
}
}
else if (graphql_1.isInputObjectType(arg)) {
typing.push(`NexusGenInputs['${arg.name}']`);
if (this.typegenInfo.declareInputs) {
typing.push(arg.name);
}
else {
typing.push(`NexusGenInputs['${arg.name}']`);
}
}

@@ -671,4 +749,10 @@ return typing;

return [`export interface NexusGenArgTypes {`]
.concat(utils_1.mapObj(typeMapping, (val, key) => {
return [` ${key}: {`]
.concat(utils_1.mapObj(typeMapping, (val, typeName) => {
if (this.typegenInfo.declareInputs) {
return [` ${typeName}: {`]
.concat(utils_1.mapObj(val, (_, fieldName) => ` ${fieldName}: ${this.getArgsName(typeName, fieldName)}`))
.concat(' }')
.join('\n');
}
return [` ${typeName}: {`]
.concat(utils_1.mapObj(val, this.printObj(' ', 'args')))

@@ -675,0 +759,0 @@ .concat(' }')

@@ -34,2 +34,8 @@ "use strict";

}
else if (typeof outputs.typegen === 'object') {
typegenFilePath = Object.assign(Object.assign({}, outputs.typegen), { outputPath: utils_1.assertAbsolutePath(outputs.typegen.outputPath, 'outputs.typegen.outputPath') });
if (outputs.typegen.globalsPath) {
typegenFilePath.globalsPath = utils_1.assertAbsolutePath(outputs.typegen.globalsPath, 'outputs.typegen.globalsPath');
}
}
}

@@ -36,0 +42,0 @@ else if (outputs !== false) {

{
"name": "nexus",
"version": "1.1.1-next.2",
"version": "1.2.0-next.3",
"description": "Scalable, strongly typed GraphQL schema development",

@@ -5,0 +5,0 @@ "keywords": [

@@ -179,2 +179,23 @@ import {

export interface ConfiguredTypegen {
/** Path for the generated type defs */
outputPath: string
/**
* Determine the path the "globals" are output, useful when you have a monorepo setup and need to isolate
* the globals from the rest of the types in order to have multiple schemas/ts projects
*/
globalsPath?: string
/**
* If globalsPath is defined, these headers are added to the "globals" generated file, rather than the
* typegen generated file
*/
globalsHeaders?: string[]
/**
* If "true", declares dedicated interfaces for any inputs / args
*
* @default true
*/
declareInputs?: boolean
}
export interface BuilderConfigInput {

@@ -199,3 +220,3 @@ /**

*/
typegen?: boolean | string
typegen?: boolean | string | ConfiguredTypegen
/**

@@ -202,0 +223,0 @@ * GraphQL SDL file generation toggle and location.

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

import { makeSchemaInternal, SchemaConfig } from './builder'
import { ConfiguredTypegen, makeSchemaInternal, SchemaConfig } from './builder'
import type { NexusGraphQLSchema } from './definitions/_types'

@@ -61,3 +61,3 @@ import { TypegenMetadata } from './typegenMetadata'

config: SchemaConfig,
typeFilePath: string | null = null
typegen: string | null | ConfiguredTypegen = null
): Promise<{

@@ -67,12 +67,12 @@ schema: NexusGraphQLSchema

tsTypes: string
globalTypes: string | null
}> => {
const { schema, missingTypes, finalConfig } = makeSchemaInternal(config)
const typegenConfig = resolveTypegenConfig(finalConfig)
const { schemaTypes, tsTypes } = await new TypegenMetadata(typegenConfig).generateArtifactContents(
schema,
typeFilePath
)
const { schemaTypes, tsTypes, globalTypes } = await new TypegenMetadata(
typegenConfig
).generateArtifactContents(schema, typegen)
assertNoMissingTypes(schema, missingTypes)
runAbstractTypeRuntimeChecks(schema, finalConfig.features)
return { schema, schemaTypes, tsTypes }
return { schema, schemaTypes, tsTypes, globalTypes }
}
import { GraphQLSchema, lexicographicSortSchema, printSchema } from 'graphql'
import * as path from 'path'
import type { BuilderConfigInput, TypegenInfo } from './builder'
import type { ConfiguredTypegen } from './core'
import type { NexusGraphQLSchema } from './definitions/_types'

@@ -15,3 +16,3 @@ import { SDL_HEADER, TYPEGEN_HEADER } from './lang'

schema: null | string
typegen: null | string
typegen: null | string | ConfiguredTypegen
}

@@ -30,12 +31,17 @@ }

const sortedSchema = this.sortSchema(schema)
if (this.config.outputs.schema || this.config.outputs.typegen) {
const { schemaTypes, tsTypes } = await this.generateArtifactContents(
sortedSchema,
this.config.outputs.typegen
)
const { typegen } = this.config.outputs
if (this.config.outputs.schema || typegen) {
const { schemaTypes, tsTypes, globalTypes } = await this.generateArtifactContents(sortedSchema, typegen)
if (this.config.outputs.schema) {
await this.writeFile('schema', schemaTypes, this.config.outputs.schema)
}
if (this.config.outputs.typegen) {
await this.writeFile('types', tsTypes, this.config.outputs.typegen)
if (typegen) {
if (typeof typegen === 'string') {
await this.writeFile('types', tsTypes, typegen)
} else {
await this.writeFile('types', tsTypes, typegen.outputPath)
if (typeof typegen.globalsPath === 'string') {
await this.writeFile('types', globalTypes ?? '', typegen.globalsPath)
}
}
}

@@ -45,8 +51,19 @@ }

async generateArtifactContents(schema: NexusGraphQLSchema, typeFilePath: string | null) {
const [schemaTypes, tsTypes] = await Promise.all([
this.generateSchemaFile(schema),
typeFilePath ? this.generateTypesFile(schema, typeFilePath) : '',
])
return { schemaTypes, tsTypes }
async generateArtifactContents(schema: NexusGraphQLSchema, typegen: string | null | ConfiguredTypegen) {
const result = {
schemaTypes: this.generateSchemaFile(schema),
tsTypes: '',
globalTypes: null as null | string,
}
if (!typegen) {
return result
}
if (typeof typegen === 'string') {
result.tsTypes = await this.generateTypesFile(schema, typegen)
} else {
const generateResult = await this.generateConfiguredTypes(schema, typegen)
result.tsTypes = generateResult.tsTypes
result.globalTypes = generateResult.globalTypes
}
return result
}

@@ -121,2 +138,3 @@

return new TypegenPrinter(schema, {
declareInputs: true,
...typegenInfo,

@@ -127,2 +145,15 @@ typegenPath,

/** Generates the type definitions */
async generateConfiguredTypes(schema: NexusGraphQLSchema, typegen: ConfiguredTypegen) {
const { outputPath: typegenPath, globalsPath, declareInputs = true } = typegen
const typegenInfo = await this.getTypegenInfo(schema, typegenPath)
return new TypegenPrinter(schema, {
...typegenInfo,
typegenPath,
globalsPath,
declareInputs,
}).printConfigured()
}
async getTypegenInfo(schema: GraphQLSchema, typegenPath?: string): Promise<TypegenInfo> {

@@ -138,3 +169,3 @@ if ('typegenConfig' in this.config) {

schema,
typegenPath || this.config.outputs.typegen || ''
typegenPath || this.normalizeTypegenPath(this.config.outputs.typegen) || ''
)

@@ -151,2 +182,6 @@ }

}
private normalizeTypegenPath(typegen: string | ConfiguredTypegen | null) {
return typeof typegen === 'string' ? typegen : typegen ? typegen.outputPath : null
}
}

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

import type { NexusGraphQLSchema } from './definitions/_types'
import { TYPEGEN_HEADER } from './lang'
import type { StringLike } from './plugin'

@@ -38,2 +39,3 @@ import {

PrintedGenTypingImport,
relativePathTo,
resolveImportPath,

@@ -57,2 +59,5 @@ } from './utils'

typegenPath: string
globalsPath?: string
globalsHeaders?: string
declareInputs?: boolean
}

@@ -83,3 +88,24 @@

print() {
const body = [
const body = [this.printCommon(), this.printPlugins()].join('\n\n')
return [this.printHeaders(), body].join('\n\n')
}
printConfigured() {
if (this.typegenInfo.globalsPath) {
const common = this.printCommon()
const tsTypes = [this.printHeadersCommon(), common].join('\n\n')
const globalTypes = [this.printHeadersGlobal(), this.printPlugins()].join('\n\n')
return {
tsTypes,
globalTypes,
}
}
return {
tsTypes: this.print(),
globalTypes: null,
}
}
private printCommon() {
return [
this.printInputTypeMap(),

@@ -108,13 +134,10 @@ this.printEnumTypeMap(),

this.printGenTypeMap(),
this.printPlugins(),
].join('\n\n')
return [this.printHeaders(), body].join('\n\n')
}
printHeaders() {
const fieldDefs = [
this.printDynamicInputFieldDefinitions(),
this.printDynamicOutputFieldDefinitions(),
this.printDynamicOutputPropertyDefinitions(),
]
return [this.printHeadersCommon(), this.printHeadersGlobal()].join('\n')
}
private printHeadersCommon() {
return [

@@ -124,7 +147,26 @@ this.typegenInfo.headers.join('\n'),

this.printDynamicImport(),
...fieldDefs,
GLOBAL_DECLARATION,
].join('\n')
}
private printHeadersGlobal() {
const headers = [
this.printDynamicInputFieldDefinitions(),
this.printDynamicOutputFieldDefinitions(),
this.printDynamicOutputPropertyDefinitions(),
GLOBAL_DECLARATION,
]
if (this.typegenInfo.globalsPath) {
headers.unshift(
`import { NexusGenTypes } from '${relativePathTo(
this.typegenInfo.typegenPath,
this.typegenInfo.globalsPath ?? ''
)}'`
)
headers.unshift(this.typegenInfo.globalsHeaders ?? TYPEGEN_HEADER)
}
return headers.join('\n')
}
printGenTypeMap() {

@@ -476,7 +518,25 @@ return [`export interface NexusGenTypes {`]

printInputTypeMap() {
return this.printTypeFieldInterface('NexusGenInputs', this.buildInputTypeMap(), 'input type')
const inputTypeMap = this.buildInputTypeMap()
if (this.typegenInfo.declareInputs) {
const declaredInputs: string[] = mapObj(inputTypeMap, (fields, inputName) =>
this.printNamedObj(inputName, fields)
)
return [...declaredInputs, this.printNamedMap('NexusGenInputs', inputTypeMap)].join('\n\n')
}
return this.printTypeFieldInterface('NexusGenInputs', inputTypeMap, 'input type')
}
printEnumTypeMap() {
return this.printTypeInterface('NexusGenEnums', this.buildEnumTypeMap())
const enumTypeMap = this.buildEnumTypeMap()
if (this.typegenInfo.declareInputs) {
return [
...mapObj(enumTypeMap, (val, name) => `export type ${name} = ${val}`),
this.printNamedMap('NexusGenEnums', enumTypeMap),
].join('\n\n')
}
return this.printTypeInterface('NexusGenEnums', enumTypeMap)
}

@@ -641,5 +701,32 @@

printArgTypeMap() {
return this.printArgTypeFieldInterface(this.buildArgTypeMap())
const argTypeMap = this.buildArgTypeMap()
if (this.typegenInfo.declareInputs) {
const declaredArgs: string[] = []
eachObj(argTypeMap, (fields, typeName) => {
eachObj(fields, (args, fieldName) => {
declaredArgs.push(this.printNamedObj(this.getArgsName(typeName, fieldName), args))
})
})
return [...declaredArgs, this.printArgTypeFieldInterface(argTypeMap)].join('\n\n')
}
return this.printArgTypeFieldInterface(argTypeMap)
}
private getArgsName(typeName: string, fieldName: string) {
return `${typeName}${fieldName.slice(0, 1).toUpperCase().concat(fieldName.slice(1))}Args`
}
printNamedObj(name: string, obj: Record<string, [string, string]>) {
return [
`export interface ${name} {`,
...mapObj(obj, (val, key) => ` ${key}${val[0]} ${val[1]}`),
`}`,
].join('\n')
}
printNamedMap(name: string, obj: Record<string, any>) {
return [`export interface ${name} {`, ...mapObj(obj, (val, key) => ` ${key}: ${key}`), `}`].join('\n')
}
buildReturnTypeMap() {

@@ -706,3 +793,7 @@ const returnTypeMap: TypeFieldMapping = {}

} else if (isEnumType(type)) {
typing.push(`NexusGenEnums['${type.name}']`)
if (this.typegenInfo.declareInputs) {
typing.push(type.name)
} else {
typing.push(`NexusGenEnums['${type.name}']`)
}
} else if (isObjectType(type) || isInterfaceType(type) || isUnionType(type)) {

@@ -769,5 +860,13 @@ typing.push(`NexusGenRootTypes['${type.name}']`)

} else if (isEnumType(arg)) {
typing.push(`NexusGenEnums['${arg.name}']`)
if (this.typegenInfo.declareInputs) {
typing.push(arg.name)
} else {
typing.push(`NexusGenEnums['${arg.name}']`)
}
} else if (isInputObjectType(arg)) {
typing.push(`NexusGenInputs['${arg.name}']`)
if (this.typegenInfo.declareInputs) {
typing.push(arg.name)
} else {
typing.push(`NexusGenInputs['${arg.name}']`)
}
}

@@ -811,4 +910,12 @@ return typing

.concat(
mapObj(typeMapping, (val, key) => {
return [` ${key}: {`]
mapObj(typeMapping, (val, typeName) => {
if (this.typegenInfo.declareInputs) {
return [` ${typeName}: {`]
.concat(
mapObj(val, (_, fieldName) => ` ${fieldName}: ${this.getArgsName(typeName, fieldName)}`)
)
.concat(' }')
.join('\n')
}
return [` ${typeName}: {`]
.concat(mapObj(val, this.printObj(' ', 'args')))

@@ -815,0 +922,0 @@ .concat(' }')

import * as path from 'path'
import type { BuilderConfigInput } from './builder'
import type { ConfiguredTypegen } from './core'
import type { TypegenMetadataConfig } from './typegenMetadata'

@@ -16,3 +17,3 @@ import { assertAbsolutePath, getOwnPackage, isProductionStage } from './utils'

let typegenFilePath: string | null = null
let typegenFilePath: string | ConfiguredTypegen | null = null
let sdlFilePath: string | null = null

@@ -36,2 +37,13 @@

typegenFilePath = assertAbsolutePath(outputs.typegen, 'outputs.typegen')
} else if (typeof outputs.typegen === 'object') {
typegenFilePath = {
...outputs.typegen,
outputPath: assertAbsolutePath(outputs.typegen.outputPath, 'outputs.typegen.outputPath'),
} as ConfiguredTypegen
if (outputs.typegen.globalsPath) {
typegenFilePath.globalsPath = assertAbsolutePath(
outputs.typegen.globalsPath,
'outputs.typegen.globalsPath'
)
}
}

@@ -38,0 +50,0 @@ } else if (outputs !== false) {

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

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

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