Skip to content

Commit

Permalink
feat(generator): allow using sdl and url at same time (#1092)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored Sep 15, 2024
1 parent 9438531 commit 031503f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 20 deletions.
6 changes: 2 additions & 4 deletions examples/10_transport-http/transport-http_abort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const graffle = Graffle.create({

const resultPromise = graffle
.with({ transport: { signal: abortController.signal } })
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// ^^^^^^^^^^^^^^^
.rawString({
document: `
{
Expand All @@ -26,10 +26,8 @@ const resultPromise = graffle
})

abortController.abort()
// ^^^^^^^
// ^^^^^

const result = await resultPromise.catch((error: unknown) => (error as Error).message)

show(result)

// todo .with(...) variant
34 changes: 24 additions & 10 deletions src/cli/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,12 @@ const args = Command.create().description(`Generate a type safe GraphQL client.`
`The name of your client. If you are not generating multiple clients you probably do not need this. Otherwise you need to differentiate the clients so that their global type registrations do not conflict. It is possible to leave one client unnamed which will become the default client at the type level (e.g. in configuration etc.)`,
),
)
// TODO allow for URL to be read from an environment variable and henceforth referenced that way instead of being hardcoded in the generated client output.
// TODO allow explicitly providing URL for code generate even when reading from an SDL file. Allow faster generation times with URL still prefilled in generated client.
.parameter(
`schema`,
z.string().min(1).describe(
`Path to where your GraphQL schema is. If a URL is given it will be introspected. Otherwise assumed to be a file path to your GraphQL SDL file.`,
),
)
.parameter(
`output`,
z.string().min(1).default(`./graffle`).describe(
`Directory path for where to output the generated TypeScript files.`,
),
)
.parametersExclusive(
`schemaErrorType`,
$ =>
Expand All @@ -43,6 +35,23 @@ const args = Command.create().description(`Generate a type safe GraphQL client.`
),
).default(`schemaErrorTypes`, true),
)
.parameter(
`defaultSchemaUrl`,
z.union([
z.boolean().describe(
`If a GraphQL endpoint is given for "--schema", should it be set as the default URL in the generated client. If true then the client will default to using this URL when sending requests.`,
),
z.string().min(1).describe(
`A GraphQL endpoint to be used as the default URL in the generated client for requests.`,
),
]).default(true),
)
.parameter(
`output`,
z.string().min(1).default(`./graffle`).describe(
`Directory path for where to output the generated TypeScript files.`,
),
)
.parameter(`format`, z.boolean().describe(`Format the generated files using dprint.`).default(true))
.parameter(
`libraryPathClient`,
Expand Down Expand Up @@ -70,10 +79,15 @@ 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

await generateFiles({
sourceSchema: url ? { type: `url`, url } : { type: `sdl`, dirPath: Path.dirname(args.schema) },
defaultSchemaUrl: url ?? undefined,
sourceSchema: url
? { type: `url`, url }
: { type: `sdl`, dirPath: Path.dirname(args.schema) },
defaultSchemaUrl,
name: args.name,
libraryPaths: {
client: args.libraryPathClient,
Expand Down
16 changes: 13 additions & 3 deletions src/layers/2_generator/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ export interface Input {
outputDirPath?: string
name?: string
code?: Omit<GenerateInput, 'schemaSource' | 'sourceDirPath' | 'options'>
defaultSchemaUrl?: URL
defaultSchemaUrl?: boolean | URL
sourceSchema: {
type: 'sdl'
/**
* Defaults to the source directory if set, otherwise the current working directory.
*/
dirPath?: string
} | { type: 'url'; url: URL }
} | {
type: 'url'
url: URL
}
/**
* Defaults to the current working directory.
*/
Expand Down Expand Up @@ -73,9 +76,16 @@ export const generateFiles = async (input: Input) => {
)
const customScalarCodecsPathExists = await fileExists(customScalarCodecsFilePath)
const typeScriptFormatter = (input.format ?? true) ? await getTypeScriptFormatter() : undefined
// dprint-ignore
const defaultSchemaUrl =
typeof input.defaultSchemaUrl === `boolean`
? input.sourceSchema.type === `url`
? input.sourceSchema.url
: undefined
: input.defaultSchemaUrl

const codes = generateCode({
defaultSchemaUrl: input.defaultSchemaUrl,
defaultSchemaUrl,
libraryPaths: {
...input.libraryPaths,
},
Expand Down
6 changes: 3 additions & 3 deletions src/layers/5_select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ type Create = <$Name extends GlobalRegistry.SchemaNames>(
) => TypeSelectionSets<GlobalRegistry.GetSchemaIndexOrDefault<$Name>>

export const create: Create = (_name) => {
return idProxy as any
return identityProxy as any
}

const idProxy = new Proxy({}, {
const identityProxy = new Proxy({}, {
get: () => (value: unknown) => value,
})

// eslint-disable-next-line
// @ts-ignore generated types
export const select: TypeSelectionSets<GlobalRegistry.SchemaIndexDefault> = idProxy
export const select: TypeSelectionSets<GlobalRegistry.SchemaIndexDefault> = identityProxy

0 comments on commit 031503f

Please sign in to comment.