Skip to content

Commit

Permalink
fix(generator): no touch schema path
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt committed Sep 27, 2024
1 parent a940c49 commit 88629a7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 28 deletions.
33 changes: 22 additions & 11 deletions src/cli/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
},
})
6 changes: 3 additions & 3 deletions src/layers/4_generator/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -14,15 +14,15 @@ 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()
expect(field).toBeDefined()
})

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()
})
12 changes: 6 additions & 6 deletions src/layers/4_generator/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -103,8 +103,8 @@ export const createConfig = async (input: Input): Promise<Config> => {
// 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

Expand Down Expand Up @@ -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 }
Expand Down
17 changes: 9 additions & 8 deletions tests/_/schemas/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand All @@ -38,7 +39,7 @@ const generate = async (
name,
...input.generatorInput,
})
console.log(`generated at`, sourceDirPath)
console.log(`generated at`, inputPathRootDir)
}

await generate({
Expand Down

0 comments on commit 88629a7

Please sign in to comment.