refactor: add typegen utils module

This commit is contained in:
Jason Kuhrt 2020-04-07 11:20:44 -04:00
parent 17fad3a78a
commit 358c33efaa
4 changed files with 46 additions and 44 deletions

View File

@ -138,6 +138,7 @@ import {
AllInputTypes, AllInputTypes,
GetGen, GetGen,
} from "./typegenTypeHelpers"; } from "./typegenTypeHelpers";
import { resolveTypegenConfig } from "./typegenUtils";
import { import {
assertNoMissingTypes, assertNoMissingTypes,
consoleWarn, consoleWarn,
@ -146,7 +147,6 @@ import {
isObject, isObject,
mapValues, mapValues,
objValues, objValues,
resolveTypegenConfig,
UNKNOWN_TYPE_SCALAR, UNKNOWN_TYPE_SCALAR,
validateOnInstallHookResult, validateOnInstallHookResult,
} from "./utils"; } from "./utils";

View File

@ -26,4 +26,5 @@ export * from "./typegenFormatPrettier";
export * from "./typegenMetadata"; export * from "./typegenMetadata";
export * from "./typegenPrinter"; export * from "./typegenPrinter";
export * from "./typegenTypeHelpers"; export * from "./typegenTypeHelpers";
export * from "./typegenUtils";
export * from "./utils"; export * from "./utils";

44
src/typegenUtils.ts Normal file
View File

@ -0,0 +1,44 @@
import { BuilderConfig } from "./builder";
import { TypegenMetadataConfig } from "./typegenMetadata";
import { assertAbsolutePath } from "./utils";
/**
* Normalizes the builder config into the config we need for typegen
*
* @param config {BuilderConfig}
*/
export function resolveTypegenConfig(
config: BuilderConfig
): TypegenMetadataConfig {
const {
outputs,
shouldGenerateArtifacts = Boolean(
!process.env.NODE_ENV || process.env.NODE_ENV === "development"
),
...rest
} = config;
let typegenPath: string | false = false;
let schemaPath: string | false = false;
if (outputs && typeof outputs === "object") {
if (typeof outputs.schema === "string") {
schemaPath = assertAbsolutePath(outputs.schema, "outputs.schema");
}
if (typeof outputs.typegen === "string") {
typegenPath = assertAbsolutePath(outputs.typegen, "outputs.typegen");
}
} else if (outputs !== false) {
console.warn(
`You should specify a configuration value for outputs in Nexus' makeSchema. ` +
`Provide one to remove this warning.`
);
}
return {
...rest,
outputs: {
typegen: shouldGenerateArtifacts ? typegenPath : false,
schema: shouldGenerateArtifacts ? schemaPath : false,
},
};
}

View File

@ -22,11 +22,9 @@ import {
specifiedScalarTypes, specifiedScalarTypes,
} from "graphql"; } from "graphql";
import path from "path"; import path from "path";
import { BuilderConfig } from "./builder";
import { decorateType } from "./definitions/decorateType"; import { decorateType } from "./definitions/decorateType";
import { MissingType, NexusTypes, withNexusSymbol } from "./definitions/_types"; import { MissingType, NexusTypes, withNexusSymbol } from "./definitions/_types";
import { PluginConfig } from "./plugin"; import { PluginConfig } from "./plugin";
import { TypegenMetadataConfig } from "./typegenMetadata";
export const isInterfaceField = ( export const isInterfaceField = (
type: GraphQLObjectType, type: GraphQLObjectType,
@ -304,47 +302,6 @@ export function printedGenTyping(config: PrintedGenTypingConfig) {
return new PrintedGenTyping(config); return new PrintedGenTyping(config);
} }
/**
* Normalizes the builder config into the config we need for typegen
*
* @param config {BuilderConfig}
*/
export function resolveTypegenConfig(
config: BuilderConfig
): TypegenMetadataConfig {
const {
outputs,
shouldGenerateArtifacts = Boolean(
!process.env.NODE_ENV || process.env.NODE_ENV === "development"
),
...rest
} = config;
let typegenPath: string | false = false;
let schemaPath: string | false = false;
if (outputs && typeof outputs === "object") {
if (typeof outputs.schema === "string") {
schemaPath = assertAbsolutePath(outputs.schema, "outputs.schema");
}
if (typeof outputs.typegen === "string") {
typegenPath = assertAbsolutePath(outputs.typegen, "outputs.typegen");
}
} else if (outputs !== false) {
console.warn(
`You should specify a configuration value for outputs in Nexus' makeSchema. ` +
`Provide one to remove this warning.`
);
}
return {
...rest,
outputs: {
typegen: shouldGenerateArtifacts ? typegenPath : false,
schema: shouldGenerateArtifacts ? schemaPath : false,
},
};
}
export function unwrapType( export function unwrapType(
type: GraphQLType type: GraphQLType
): { type: GraphQLNamedType; isNonNull: boolean; list: boolean[] } { ): { type: GraphQLNamedType; isNonNull: boolean; list: boolean[] } {