From 88629a7e09c6bb38a9dfd0c63ed9d6963cd8c6b8 Mon Sep 17 00:00:00 2001 From: Jason Kuhrt Date: Fri, 27 Sep 2024 16:55:58 -0400 Subject: [PATCH] fix(generator): no touch schema path --- src/cli/generate.ts | 33 ++++++++++++++++++--------- src/layers/4_generator/config.test.ts | 6 ++--- src/layers/4_generator/config.ts | 12 +++++----- tests/_/schemas/generate.ts | 17 +++++++------- 4 files changed, 40 insertions(+), 28 deletions(-) diff --git a/src/cli/generate.ts b/src/cli/generate.ts index a159979bd..f7ac6e2e4 100755 --- a/src/cli/generate.ts +++ b/src/cli/generate.ts @@ -52,7 +52,13 @@ const args = Command.create().description(`Generate a type safe GraphQL client.` `Directory path for where to output the generated TypeScript files.`, ), ) - .parameter(`format`, z.boolean().describe(`Format the generated files using dprint.`).default(true)) + .parameter( + `format`, + z.boolean().describe( + `Try to format the generated files. At attempt to use dprint will be made. You need to have these dependencies installed in your project: @dprint/formatter, @dprint/typescript.`, + ) + .default(true), + ) .parameter( `libraryPathClient`, z.string().optional().describe( @@ -85,27 +91,32 @@ const args = Command.create().description(`Generate a type safe GraphQL client.` .parse() const url = urlParseSafe(args.schema) + const defaultSchemaUrl = typeof args.defaultSchemaUrl === `string` ? new URL(args.defaultSchemaUrl) : args.defaultSchemaUrl +const format = args.format + +const schemaSource = url + ? { type: `url` as const, url } + : { type: `sdl` as const, dirOrFilePath: args.schema } + await Generator.generate({ - sourceSchema: url - ? { type: `url`, url } - : { type: `sdl`, dirOrFilePath: Path.dirname(args.schema) }, + format, + schemaSource, defaultSchemaUrl, name: args.name, - libraryPaths: { - client: args.libraryPathClient, - schema: args.libraryPathSchema, - scalars: args.libraryPathScalars, - utilitiesForGenerated: args.libraryPathUtilitiesForGenerated, - }, outputDirPath: args.output, - format: args.format, errorTypeNamePattern: args.schemaErrorType._tag === `schemaErrorTypePattern` ? new RegExp(args.schemaErrorType.value) : args.schemaErrorType.value ? /^Error.+/ : undefined, + libraryPaths: { + client: args.libraryPathClient, + schema: args.libraryPathSchema, + scalars: args.libraryPathScalars, + utilitiesForGenerated: args.libraryPathUtilitiesForGenerated, + }, }) diff --git a/src/layers/4_generator/config.test.ts b/src/layers/4_generator/config.test.ts index ce2c8d4b8..dda1b8014 100644 --- a/src/layers/4_generator/config.test.ts +++ b/src/layers/4_generator/config.test.ts @@ -5,7 +5,7 @@ import { createConfig } from './config.js' test(`can load schema from custom path`, async () => { const customPathFile = `./tests/_/fixtures/custom.graphql` - const config = await createConfig({ sourceSchema: { type: `sdl`, dirOrFilePath: customPathFile } }) + const config = await createConfig({ schemaSource: { type: `sdl`, dirOrFilePath: customPathFile } }) const field = config.schema.instance.getQueryType()?.getFields()[`customNamedSchemaFile`] expect(config.paths.project.inputs.schema).toEqual(customPathFile) expect(config.schema.sdl).toMatchSnapshot() @@ -14,7 +14,7 @@ test(`can load schema from custom path`, async () => { test(`can load schema from custom dir using default file name`, async () => { const customPathDir = `tests/_/fixtures` - const config = await createConfig({ sourceSchema: { type: `sdl`, dirOrFilePath: customPathDir } }) + const config = await createConfig({ schemaSource: { type: `sdl`, dirOrFilePath: customPathDir } }) const field = config.schema.instance.getQueryType()?.getFields()[`defaultNamedSchemaFile`] expect(config.paths.project.inputs.schema).toEqual(customPathDir + `/schema.graphql`) expect(config.schema.sdl).toMatchSnapshot() @@ -22,7 +22,7 @@ test(`can load schema from custom dir using default file name`, async () => { }) test(`can introspect schema from url`, async ({ pokemonService }) => { - const config = await createConfig({ sourceSchema: { type: `url`, url: pokemonService.url } }) + const config = await createConfig({ schemaSource: { type: `url`, url: pokemonService.url } }) expect(config.paths.project.inputs.schema).toEqual(null) expect(config.schema.sdl).toMatchSnapshot() }) diff --git a/src/layers/4_generator/config.ts b/src/layers/4_generator/config.ts index 5733c8bc9..669fb6dcc 100644 --- a/src/layers/4_generator/config.ts +++ b/src/layers/4_generator/config.ts @@ -8,7 +8,7 @@ import { omitUndefinedKeys } from '../../lib/prelude.js' import { fileExists, isPathToADirectory } from './helpers/fs.js' export interface Input { - sourceSchema: { + schemaSource: { type: 'sdl' /** * Defaults to the source directory if set, otherwise the current working directory. @@ -103,8 +103,8 @@ export const createConfig = async (input: Input): Promise => { // dprint-ignore const defaultSchemaUrl = typeof input.defaultSchemaUrl === `boolean` - ? input.sourceSchema.type === `url` - ? input.sourceSchema.url + ? input.schemaSource.type === `url` + ? input.schemaSource.url : null : input.defaultSchemaUrl??null @@ -184,14 +184,14 @@ const defaultSchemaFileName = `schema.graphql` const resolveSourceSchema = async ( input: Input, ): Promise<{ type: 'introspection'; content: string } | { type: 'file'; content: string; path: string }> => { - if (input.sourceSchema.type === `sdl`) { - const fileOrDirPath = input.sourceSchema.dirOrFilePath ?? input.sourceDirPath ?? process.cwd() + if (input.schemaSource.type === `sdl`) { + const fileOrDirPath = input.schemaSource.dirOrFilePath ?? input.sourceDirPath ?? process.cwd() const isDir = await isPathToADirectory(fileOrDirPath) const finalPath = isDir ? Path.join(fileOrDirPath, defaultSchemaFileName) : fileOrDirPath const sdl = await fs.readFile(finalPath, `utf8`) return { type: `file`, content: sdl, path: finalPath } } else { - const data = await introspectionQuery(input.sourceSchema.url) + const data = await introspectionQuery(input.schemaSource.url) const schema = buildClientSchema(data) const sdl = printSchema(schema) return { type: `introspection`, content: sdl } diff --git a/tests/_/schemas/generate.ts b/tests/_/schemas/generate.ts index ae2dbc739..b4051a329 100644 --- a/tests/_/schemas/generate.ts +++ b/tests/_/schemas/generate.ts @@ -15,20 +15,21 @@ const generate = async ( const name = input.name === false ? undefined : pascalCase(input.dirName) const rootDir = join(`./tests/_/schemas/`, input.dirName) - const outputSchemaPath = join(rootDir, `schema.graphql`) - const sourceDirPath = dirname(outputSchemaPath) - const outputDirPath = join(rootDir, `/graffle`) + const outputSchemaPath = join(rootDir, `schema.graphql`) const { schema } = await import(`./${input.dirName}/schema.js`) - await fs.writeFile(outputSchemaPath, printSchema(schema)) + const inputPathRootDir = dirname(outputSchemaPath) + const outputPathRootDir = join(rootDir, `/graffle`) + await Generator.generate({ - sourceSchema: { type: `sdl` }, - sourceDirPath, + schemaSource: { type: `sdl` }, + // todo funky between this and passing path to sdl + sourceDirPath: inputPathRootDir, defaultSchemaUrl: input.defaultSchemaUrl, sourceCustomScalarCodecsFilePath: join(`./tests/_/customScalarCodecs.ts`), - outputDirPath, + outputDirPath: outputPathRootDir, libraryPaths: { client: `../../../../../../src/entrypoints/client.js`, schema: `../../../../../../src/entrypoints/schema.js`, @@ -38,7 +39,7 @@ const generate = async ( name, ...input.generatorInput, }) - console.log(`generated at`, sourceDirPath) + console.log(`generated at`, inputPathRootDir) } await generate({