Skip to content

Commit 88629a7

Browse files
committed
fix(generator): no touch schema path
1 parent a940c49 commit 88629a7

File tree

4 files changed

+40
-28
lines changed

4 files changed

+40
-28
lines changed

src/cli/generate.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@ const args = Command.create().description(`Generate a type safe GraphQL client.`
5252
`Directory path for where to output the generated TypeScript files.`,
5353
),
5454
)
55-
.parameter(`format`, z.boolean().describe(`Format the generated files using dprint.`).default(true))
55+
.parameter(
56+
`format`,
57+
z.boolean().describe(
58+
`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.`,
59+
)
60+
.default(true),
61+
)
5662
.parameter(
5763
`libraryPathClient`,
5864
z.string().optional().describe(
@@ -85,27 +91,32 @@ const args = Command.create().description(`Generate a type safe GraphQL client.`
8591
.parse()
8692

8793
const url = urlParseSafe(args.schema)
94+
8895
const defaultSchemaUrl = typeof args.defaultSchemaUrl === `string`
8996
? new URL(args.defaultSchemaUrl)
9097
: args.defaultSchemaUrl
9198

99+
const format = args.format
100+
101+
const schemaSource = url
102+
? { type: `url` as const, url }
103+
: { type: `sdl` as const, dirOrFilePath: args.schema }
104+
92105
await Generator.generate({
93-
sourceSchema: url
94-
? { type: `url`, url }
95-
: { type: `sdl`, dirOrFilePath: Path.dirname(args.schema) },
106+
format,
107+
schemaSource,
96108
defaultSchemaUrl,
97109
name: args.name,
98-
libraryPaths: {
99-
client: args.libraryPathClient,
100-
schema: args.libraryPathSchema,
101-
scalars: args.libraryPathScalars,
102-
utilitiesForGenerated: args.libraryPathUtilitiesForGenerated,
103-
},
104110
outputDirPath: args.output,
105-
format: args.format,
106111
errorTypeNamePattern: args.schemaErrorType._tag === `schemaErrorTypePattern`
107112
? new RegExp(args.schemaErrorType.value)
108113
: args.schemaErrorType.value
109114
? /^Error.+/
110115
: undefined,
116+
libraryPaths: {
117+
client: args.libraryPathClient,
118+
schema: args.libraryPathSchema,
119+
scalars: args.libraryPathScalars,
120+
utilitiesForGenerated: args.libraryPathUtilitiesForGenerated,
121+
},
111122
})

src/layers/4_generator/config.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { createConfig } from './config.js'
55

66
test(`can load schema from custom path`, async () => {
77
const customPathFile = `./tests/_/fixtures/custom.graphql`
8-
const config = await createConfig({ sourceSchema: { type: `sdl`, dirOrFilePath: customPathFile } })
8+
const config = await createConfig({ schemaSource: { type: `sdl`, dirOrFilePath: customPathFile } })
99
const field = config.schema.instance.getQueryType()?.getFields()[`customNamedSchemaFile`]
1010
expect(config.paths.project.inputs.schema).toEqual(customPathFile)
1111
expect(config.schema.sdl).toMatchSnapshot()
@@ -14,15 +14,15 @@ test(`can load schema from custom path`, async () => {
1414

1515
test(`can load schema from custom dir using default file name`, async () => {
1616
const customPathDir = `tests/_/fixtures`
17-
const config = await createConfig({ sourceSchema: { type: `sdl`, dirOrFilePath: customPathDir } })
17+
const config = await createConfig({ schemaSource: { type: `sdl`, dirOrFilePath: customPathDir } })
1818
const field = config.schema.instance.getQueryType()?.getFields()[`defaultNamedSchemaFile`]
1919
expect(config.paths.project.inputs.schema).toEqual(customPathDir + `/schema.graphql`)
2020
expect(config.schema.sdl).toMatchSnapshot()
2121
expect(field).toBeDefined()
2222
})
2323

2424
test(`can introspect schema from url`, async ({ pokemonService }) => {
25-
const config = await createConfig({ sourceSchema: { type: `url`, url: pokemonService.url } })
25+
const config = await createConfig({ schemaSource: { type: `url`, url: pokemonService.url } })
2626
expect(config.paths.project.inputs.schema).toEqual(null)
2727
expect(config.schema.sdl).toMatchSnapshot()
2828
})

src/layers/4_generator/config.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { omitUndefinedKeys } from '../../lib/prelude.js'
88
import { fileExists, isPathToADirectory } from './helpers/fs.js'
99

1010
export interface Input {
11-
sourceSchema: {
11+
schemaSource: {
1212
type: 'sdl'
1313
/**
1414
* Defaults to the source directory if set, otherwise the current working directory.
@@ -103,8 +103,8 @@ export const createConfig = async (input: Input): Promise<Config> => {
103103
// dprint-ignore
104104
const defaultSchemaUrl =
105105
typeof input.defaultSchemaUrl === `boolean`
106-
? input.sourceSchema.type === `url`
107-
? input.sourceSchema.url
106+
? input.schemaSource.type === `url`
107+
? input.schemaSource.url
108108
: null
109109
: input.defaultSchemaUrl??null
110110

@@ -184,14 +184,14 @@ const defaultSchemaFileName = `schema.graphql`
184184
const resolveSourceSchema = async (
185185
input: Input,
186186
): Promise<{ type: 'introspection'; content: string } | { type: 'file'; content: string; path: string }> => {
187-
if (input.sourceSchema.type === `sdl`) {
188-
const fileOrDirPath = input.sourceSchema.dirOrFilePath ?? input.sourceDirPath ?? process.cwd()
187+
if (input.schemaSource.type === `sdl`) {
188+
const fileOrDirPath = input.schemaSource.dirOrFilePath ?? input.sourceDirPath ?? process.cwd()
189189
const isDir = await isPathToADirectory(fileOrDirPath)
190190
const finalPath = isDir ? Path.join(fileOrDirPath, defaultSchemaFileName) : fileOrDirPath
191191
const sdl = await fs.readFile(finalPath, `utf8`)
192192
return { type: `file`, content: sdl, path: finalPath }
193193
} else {
194-
const data = await introspectionQuery(input.sourceSchema.url)
194+
const data = await introspectionQuery(input.schemaSource.url)
195195
const schema = buildClientSchema(data)
196196
const sdl = printSchema(schema)
197197
return { type: `introspection`, content: sdl }

tests/_/schemas/generate.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,21 @@ const generate = async (
1515
const name = input.name === false ? undefined : pascalCase(input.dirName)
1616

1717
const rootDir = join(`./tests/_/schemas/`, input.dirName)
18-
const outputSchemaPath = join(rootDir, `schema.graphql`)
19-
const sourceDirPath = dirname(outputSchemaPath)
20-
const outputDirPath = join(rootDir, `/graffle`)
2118

19+
const outputSchemaPath = join(rootDir, `schema.graphql`)
2220
const { schema } = await import(`./${input.dirName}/schema.js`)
23-
2421
await fs.writeFile(outputSchemaPath, printSchema(schema))
2522

23+
const inputPathRootDir = dirname(outputSchemaPath)
24+
const outputPathRootDir = join(rootDir, `/graffle`)
25+
2626
await Generator.generate({
27-
sourceSchema: { type: `sdl` },
28-
sourceDirPath,
27+
schemaSource: { type: `sdl` },
28+
// todo funky between this and passing path to sdl
29+
sourceDirPath: inputPathRootDir,
2930
defaultSchemaUrl: input.defaultSchemaUrl,
3031
sourceCustomScalarCodecsFilePath: join(`./tests/_/customScalarCodecs.ts`),
31-
outputDirPath,
32+
outputDirPath: outputPathRootDir,
3233
libraryPaths: {
3334
client: `../../../../../../src/entrypoints/client.js`,
3435
schema: `../../../../../../src/entrypoints/schema.js`,
@@ -38,7 +39,7 @@ const generate = async (
3839
name,
3940
...input.generatorInput,
4041
})
41-
console.log(`generated at`, sourceDirPath)
42+
console.log(`generated at`, inputPathRootDir)
4243
}
4344

4445
await generate({

0 commit comments

Comments
 (0)