Comparing version
@@ -14,2 +14,5 @@ "use strict"; | ||
const config_1 = require("../helpers/config"); | ||
const scalarTypes_1 = require("../helpers/scalarTypes"); | ||
const makeTypesFile_1 = require("../generate/makeTypesFile"); | ||
const makePossibleTypes_1 = require("../generate/makePossibleTypes"); | ||
function resolveToRootType(type) { | ||
@@ -142,3 +145,3 @@ if (type instanceof graphql_1.GraphQLList || type instanceof graphql_1.GraphQLNonNull) { | ||
name: "BuilderResult", | ||
constraint: makeSelectionResult(`PossibleTypes_${rootType.name}`), | ||
constraint: makeSelectionResult(rootType), | ||
isConst: true, | ||
@@ -204,12 +207,9 @@ }; | ||
if (type instanceof graphql_1.GraphQLObjectType) { | ||
possibleTypes = [type.name]; | ||
possibleTypes = [type]; | ||
} | ||
else if (type instanceof graphql_1.GraphQLInterfaceType) { | ||
possibleTypes = [ | ||
type.name, | ||
...schema.getPossibleTypes(type).map((t) => t.name), | ||
]; | ||
possibleTypes = [type, ...schema.getPossibleTypes(type)]; | ||
} | ||
else if (type instanceof graphql_1.GraphQLUnionType) { | ||
possibleTypes = [type.name, ...type.getTypes().map((t) => t.name)]; | ||
possibleTypes = [type, ...type.getTypes()]; | ||
} | ||
@@ -220,6 +220,3 @@ else { | ||
for (const possibleType of possibleTypes) { | ||
let outputPossibleTypes = `Exclude<PossibleTypes_${type.name}, "${type.name}">`; | ||
if (type instanceof graphql_1.GraphQLObjectType) { | ||
outputPossibleTypes = `PossibleTypes_${type.name}`; | ||
} | ||
const ptType = `PossibleTypes_${possibleType.name}`; | ||
methods.push({ | ||
@@ -231,3 +228,3 @@ kind: ts_morph_1.StructureKind.MethodSignature, | ||
name: "Result", | ||
constraint: makeSelectionResult(`PossibleTypes_${possibleType}`), | ||
constraint: makeSelectionResult(possibleType), | ||
isConst: true, | ||
@@ -246,3 +243,3 @@ }, | ||
], | ||
returnType: `InlineFragment<${outputPossibleTypes}, "${possibleType}", SelectionSetOutput<Result, ${outputPossibleTypes}>>`, | ||
returnType: `InlineFragment<${ptType}, "${possibleType.name}", SelectionSetOutput<Result, ${ptType}>>`, | ||
}); | ||
@@ -339,3 +336,16 @@ methods.push({ | ||
} | ||
function makeSelectionResult(possibleTypes) { | ||
function makeSelectionResult(type) { | ||
let possibleTypes; | ||
if (type instanceof graphql_1.GraphQLObjectType) { | ||
possibleTypes = `PossibleTypes_${type.name}`; | ||
} | ||
else if (type instanceof graphql_1.GraphQLInterfaceType) { | ||
possibleTypes = `PossibleTypes_${type.name} | "${type.name}"`; | ||
} | ||
else if (type instanceof graphql_1.GraphQLUnionType) { | ||
possibleTypes = `PossibleTypes_${type.name} | "${type.name}"`; | ||
} | ||
else { | ||
throw new Error("Expected object, interface, or union type"); | ||
} | ||
return `ReadonlyArray<SelectionSetSelection<${possibleTypes}>>`; | ||
@@ -345,3 +355,3 @@ } | ||
const { name } = type; | ||
const selectionSetResult = makeSelectionResult(`PossibleTypes_${name}`); | ||
const selectionSetResult = makeSelectionResult(type); | ||
return { | ||
@@ -418,3 +428,3 @@ kind: ts_morph_1.StructureKind.Interface, | ||
const { output } = config; | ||
const file = project.createSourceFile(path_1.default.join(process.cwd(), output, "types.d.ts"), undefined, { | ||
const file = project.createSourceFile(path_1.default.join(process.cwd(), output, "builder.d.ts"), undefined, { | ||
overwrite: true, | ||
@@ -455,3 +465,2 @@ }); | ||
type: [ | ||
`"${name}"`, | ||
...schema.getPossibleTypes(type).map((t) => `"${t.name}"`), | ||
@@ -497,3 +506,6 @@ ].join(" | "), | ||
name: `PossibleTypes_${name}`, | ||
type: [`"${name}"`, ...type.getTypes().map((t) => `"${t.name}"`)].join(" | "), | ||
type: type | ||
.getTypes() | ||
.map((t) => `"${t.name}"`) | ||
.join(" | "), | ||
}); | ||
@@ -534,11 +546,3 @@ const inlineFragmentBuilders = builderFunctionsForInlineFragments(name, schema, type); | ||
else if (type instanceof graphql_1.GraphQLScalarType) { | ||
const { scalarTypes } = config; | ||
const baseTypes = { | ||
String: "string", | ||
Int: "number", | ||
Float: "number", | ||
Boolean: "boolean", | ||
ID: "string | number", | ||
...scalarTypes, | ||
}; | ||
const scalarTypes = (0, scalarTypes_1.getScalarTypes)(config); | ||
statements.push({ | ||
@@ -554,7 +558,7 @@ kind: ts_morph_1.StructureKind.Interface, | ||
name: "_input", | ||
type: baseTypes[name] ?? "unknown", | ||
type: scalarTypes[name] ?? "unknown", | ||
}, | ||
{ | ||
name: "_output", | ||
type: baseTypes[name] ?? "unknown", | ||
type: scalarTypes[name] ?? "unknown", | ||
}, | ||
@@ -673,7 +677,2 @@ ], | ||
.map((type) => { | ||
const resultTypeParameter = { | ||
name: "Result", | ||
constraint: makeSelectionResult(`PossibleTypes_${type.name}`), | ||
isConst: true, | ||
}; | ||
if (type instanceof graphql_1.GraphQLObjectType || | ||
@@ -685,2 +684,7 @@ type instanceof graphql_1.GraphQLInterfaceType || | ||
} | ||
const resultTypeParameter = { | ||
name: "Result", | ||
constraint: makeSelectionResult(type), | ||
isConst: true, | ||
}; | ||
let possibleTypes = `Exclude<PossibleTypes_${type.name}, "${type.name}">`; | ||
@@ -798,3 +802,3 @@ if (type instanceof graphql_1.GraphQLObjectType) { | ||
kind: ts_morph_1.StructureKind.Module, | ||
name: `"./types"`, | ||
name: `"./builder"`, | ||
hasDeclareKeyword: true, | ||
@@ -819,8 +823,13 @@ declarationKind: ts_morph_1.ModuleDeclarationKind.Module, | ||
await promises_1.default.mkdir(outputDir, { recursive: true }); | ||
await promises_1.default.writeFile(path_1.default.join(outputDir, "types.d.ts"), text); | ||
await promises_1.default.writeFile(path_1.default.join(outputDir, "builder.d.ts"), text); | ||
if (config.emitTypes) { | ||
await promises_1.default.writeFile(path_1.default.join(outputDir, "types.d.ts"), await (0, makeTypesFile_1.makeTypesFile)(schema, config, project, outputDir)); | ||
} | ||
await promises_1.default.writeFile(path_1.default.join(outputDir, "index.ts"), await prettier_1.default.format(` | ||
/* eslint-disable */ | ||
import type {Builder} from "./types"; | ||
import type {Builder} from "./builder"; | ||
import {builder} from "@gqlb/core" | ||
export const b = builder as any as Builder; | ||
${config.possibleTypes ? (0, makePossibleTypes_1.makePossibleTypes)(schema) : ""} | ||
${config.emitTypes ? `export type { types } from "./types";` : ""} | ||
`, { | ||
@@ -827,0 +836,0 @@ parser: "typescript", |
@@ -19,2 +19,4 @@ import { z } from "zod"; | ||
scalarTypes: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>; | ||
possibleTypes: z.ZodDefault<z.ZodBoolean>; | ||
emitTypes: z.ZodDefault<z.ZodBoolean>; | ||
}, "strict", z.ZodTypeAny, { | ||
@@ -31,2 +33,4 @@ schema: ({ | ||
output: string; | ||
possibleTypes: boolean; | ||
emitTypes: boolean; | ||
scalarTypes?: Record<string, string> | undefined; | ||
@@ -45,2 +49,4 @@ }, { | ||
scalarTypes?: Record<string, string> | undefined; | ||
possibleTypes?: boolean | undefined; | ||
emitTypes?: boolean | undefined; | ||
}>>; | ||
@@ -66,2 +72,4 @@ convert: z.ZodOptional<z.ZodDefault<z.ZodObject<{ | ||
output: string; | ||
possibleTypes: boolean; | ||
emitTypes: boolean; | ||
scalarTypes?: Record<string, string> | undefined; | ||
@@ -85,2 +93,4 @@ }>; | ||
scalarTypes?: Record<string, string> | undefined; | ||
possibleTypes?: boolean | undefined; | ||
emitTypes?: boolean | undefined; | ||
}>; | ||
@@ -103,2 +113,4 @@ convert?: { | ||
output: string; | ||
possibleTypes: boolean; | ||
emitTypes: boolean; | ||
scalarTypes?: Record<string, string> | undefined; | ||
@@ -105,0 +117,0 @@ }>; |
@@ -18,2 +18,4 @@ "use strict"; | ||
scalarTypes: zod_1.z.record(zod_1.z.string(), zod_1.z.string()).optional(), | ||
possibleTypes: zod_1.z.boolean().default(false), | ||
emitTypes: zod_1.z.boolean().default(true), | ||
})), | ||
@@ -20,0 +22,0 @@ convert: zod_1.z |
{ | ||
"name": "@gqlb/cli", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -9,2 +9,3 @@ import fs from "fs/promises"; | ||
GraphQLList, | ||
GraphQLNamedType, | ||
GraphQLNonNull, | ||
@@ -39,2 +40,5 @@ GraphQLObjectType, | ||
import { loadConfig, schema } from "../helpers/config"; | ||
import { getScalarTypes } from "../helpers/scalarTypes"; | ||
import { makeTypesFile } from "../generate/makeTypesFile"; | ||
import { makePossibleTypes } from "../generate/makePossibleTypes"; | ||
@@ -191,3 +195,3 @@ function resolveToRootType(type: GraphQLType) { | ||
name: "BuilderResult", | ||
constraint: makeSelectionResult(`PossibleTypes_${rootType.name}`), | ||
constraint: makeSelectionResult(rootType), | ||
isConst: true, | ||
@@ -261,12 +265,11 @@ }; | ||
const methods: MethodSignatureStructure[] = []; | ||
let possibleTypes: string[]; | ||
let possibleTypes: Array< | ||
GraphQLObjectType | GraphQLInterfaceType | GraphQLUnionType | ||
>; | ||
if (type instanceof GraphQLObjectType) { | ||
possibleTypes = [type.name]; | ||
possibleTypes = [type]; | ||
} else if (type instanceof GraphQLInterfaceType) { | ||
possibleTypes = [ | ||
type.name, | ||
...schema.getPossibleTypes(type).map((t) => t.name), | ||
]; | ||
possibleTypes = [type, ...schema.getPossibleTypes(type)]; | ||
} else if (type instanceof GraphQLUnionType) { | ||
possibleTypes = [type.name, ...type.getTypes().map((t) => t.name)]; | ||
possibleTypes = [type, ...type.getTypes()]; | ||
} else { | ||
@@ -276,6 +279,3 @@ throw new Error("Expected object, interface, or union type"); | ||
for (const possibleType of possibleTypes) { | ||
let outputPossibleTypes = `Exclude<PossibleTypes_${type.name}, "${type.name}">`; | ||
if (type instanceof GraphQLObjectType) { | ||
outputPossibleTypes = `PossibleTypes_${type.name}`; | ||
} | ||
const ptType = `PossibleTypes_${possibleType.name}`; | ||
methods.push({ | ||
@@ -287,3 +287,3 @@ kind: StructureKind.MethodSignature, | ||
name: "Result", | ||
constraint: makeSelectionResult(`PossibleTypes_${possibleType}`), | ||
constraint: makeSelectionResult(possibleType), | ||
isConst: true, | ||
@@ -302,3 +302,3 @@ }, | ||
], | ||
returnType: `InlineFragment<${outputPossibleTypes}, "${possibleType}", SelectionSetOutput<Result, ${outputPossibleTypes}>>`, | ||
returnType: `InlineFragment<${ptType}, "${possibleType.name}", SelectionSetOutput<Result, ${ptType}>>`, | ||
}); | ||
@@ -406,3 +406,13 @@ methods.push({ | ||
function makeSelectionResult(possibleTypes: string) { | ||
function makeSelectionResult(type: GraphQLNamedType) { | ||
let possibleTypes: string; | ||
if (type instanceof GraphQLObjectType) { | ||
possibleTypes = `PossibleTypes_${type.name}`; | ||
} else if (type instanceof GraphQLInterfaceType) { | ||
possibleTypes = `PossibleTypes_${type.name} | "${type.name}"`; | ||
} else if (type instanceof GraphQLUnionType) { | ||
possibleTypes = `PossibleTypes_${type.name} | "${type.name}"`; | ||
} else { | ||
throw new Error("Expected object, interface, or union type"); | ||
} | ||
return `ReadonlyArray<SelectionSetSelection<${possibleTypes}>>`; | ||
@@ -415,3 +425,3 @@ } | ||
const { name } = type; | ||
const selectionSetResult = makeSelectionResult(`PossibleTypes_${name}`); | ||
const selectionSetResult = makeSelectionResult(type); | ||
return { | ||
@@ -494,3 +504,3 @@ kind: StructureKind.Interface, | ||
const file = project.createSourceFile( | ||
path.join(process.cwd(), output, "types.d.ts"), | ||
path.join(process.cwd(), output, "builder.d.ts"), | ||
undefined, | ||
@@ -541,3 +551,2 @@ { | ||
type: [ | ||
`"${name}"`, | ||
...schema.getPossibleTypes(type).map((t) => `"${t.name}"`), | ||
@@ -586,5 +595,6 @@ ].join(" | "), | ||
name: `PossibleTypes_${name}`, | ||
type: [`"${name}"`, ...type.getTypes().map((t) => `"${t.name}"`)].join( | ||
" | " | ||
), | ||
type: type | ||
.getTypes() | ||
.map((t) => `"${t.name}"`) | ||
.join(" | "), | ||
}); | ||
@@ -629,11 +639,3 @@ | ||
} else if (type instanceof GraphQLScalarType) { | ||
const { scalarTypes } = config; | ||
const baseTypes: Record<string, string> = { | ||
String: "string", | ||
Int: "number", | ||
Float: "number", | ||
Boolean: "boolean", | ||
ID: "string | number", | ||
...scalarTypes, | ||
}; | ||
const scalarTypes = getScalarTypes(config); | ||
statements.push({ | ||
@@ -649,7 +651,7 @@ kind: StructureKind.Interface, | ||
name: "_input", | ||
type: baseTypes[name] ?? "unknown", | ||
type: scalarTypes[name] ?? "unknown", | ||
}, | ||
{ | ||
name: "_output", | ||
type: baseTypes[name] ?? "unknown", | ||
type: scalarTypes[name] ?? "unknown", | ||
}, | ||
@@ -776,7 +778,2 @@ ], | ||
.map((type): CallSignatureDeclarationStructure[] => { | ||
const resultTypeParameter = { | ||
name: "Result", | ||
constraint: makeSelectionResult(`PossibleTypes_${type.name}`), | ||
isConst: true, | ||
}; | ||
if ( | ||
@@ -790,2 +787,7 @@ type instanceof GraphQLObjectType || | ||
} | ||
const resultTypeParameter = { | ||
name: "Result", | ||
constraint: makeSelectionResult(type), | ||
isConst: true, | ||
}; | ||
let possibleTypes = `Exclude<PossibleTypes_${type.name}, "${type.name}">`; | ||
@@ -909,3 +911,3 @@ if (type instanceof GraphQLObjectType) { | ||
kind: StructureKind.Module, | ||
name: `"./types"`, | ||
name: `"./builder"`, | ||
hasDeclareKeyword: true, | ||
@@ -935,3 +937,9 @@ declarationKind: ModuleDeclarationKind.Module, | ||
await fs.mkdir(outputDir, { recursive: true }); | ||
await fs.writeFile(path.join(outputDir, "types.d.ts"), text); | ||
await fs.writeFile(path.join(outputDir, "builder.d.ts"), text); | ||
if (config.emitTypes) { | ||
await fs.writeFile( | ||
path.join(outputDir, "types.d.ts"), | ||
await makeTypesFile(schema, config, project, outputDir) | ||
); | ||
} | ||
@@ -943,5 +951,7 @@ await fs.writeFile( | ||
/* eslint-disable */ | ||
import type {Builder} from "./types"; | ||
import type {Builder} from "./builder"; | ||
import {builder} from "@gqlb/core" | ||
export const b = builder as any as Builder; | ||
${config.possibleTypes ? makePossibleTypes(schema) : ""} | ||
${config.emitTypes ? `export type { types } from "./types";` : ""} | ||
`, | ||
@@ -948,0 +958,0 @@ { |
@@ -18,2 +18,4 @@ import { cosmiconfig } from "cosmiconfig"; | ||
scalarTypes: z.record(z.string(), z.string()).optional(), | ||
possibleTypes: z.boolean().default(false), | ||
emitTypes: z.boolean().default(true), | ||
}) | ||
@@ -20,0 +22,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
Sorry, the diff of this file is not supported yet
128484
16.57%42
55.56%2767
15.24%