Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Options:
-o, --output <./myClient> output directory
-e, --endpoint <http://example.com/graphql> GraphQL endpoint
-p, --post use POST for introspection query
-H, --header <'header: value'> headers to use in fetch
-s, --schema <./*.graphql> glob pattern to match GraphQL schema definition files
-f, --fetcher <./schemaFetcher.js> path to introspection query fetcher file
-c, --config <./myConfig.js> path to config file
Expand Down
19 changes: 15 additions & 4 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ import { validateConfigs } from './cliHelpers/validateConfigs'
import { Config } from './config'
import { requireModuleFromPath } from './helpers/files'

function collect(value: string, previous: string[]) {
return previous.concat([value])
}

program
.option('-o, --output <./myClient>', 'output directory')
.option('-e, --endpoint <http://example.com/graphql>', 'GraphQL endpoint')
.option('-p, --post', 'use POST for introspection query')
.option("-H, --header <'header: value'>", 'headers to use in fetch', collect, [])
.option('-s, --schema <./mySchema.graphql>', 'path to GraphQL schema definition file')
.option('-f, --fetcher <./schemaFetcher.js>', 'path to introspection query fetcher file')
.option('-c, --config <./myConfig.js>', 'path to config file')
Expand All @@ -27,6 +32,7 @@ const configs: Config[] = program.config
{
endpoint: program.endpoint,
post: program.post,
headers: program.header,
schema: program.schema,
output: program.output,
fetcher: program.fetcher,
Expand All @@ -35,7 +41,12 @@ const configs: Config[] = program.config

if (!validateConfigs(configs)) program.help()

new Listr(configs.map(config => task(config)), { renderer: program.verbose ? 'verbose' : 'default' }).run().catch(e => {
console.log(chalk.red(e.stack))
process.exit(1)
})
new Listr(
configs.map(config => task(config)),
{ renderer: program.verbose ? 'verbose' : 'default' },
)
.run()
.catch(e => {
console.log(chalk.red(e.stack))
process.exit(1)
})
2 changes: 1 addition & 1 deletion src/cliHelpers/schemaTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const schemaTask = (config: Config): ListrTask => {
return {
title: `fetching schema using ${config.post ? 'POST' : 'GET'} ${endpoint}`,
task: async ctx => {
ctx.schema = await fetchSchema(endpoint, config.post)
ctx.schema = await fetchSchema(endpoint, config.post, undefined, config.headers)
},
}
} else if (config.fetcher) {
Expand Down
6 changes: 6 additions & 0 deletions src/cliHelpers/validateConfigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ export const validateConfigs = (configs: Config[]) => {
)

if (!config.output) errors.push(`you didn't provide an \`output\` option in ${whichConfig}`)

if (config.headers) {
config.headers.forEach(header => {
if (!header.includes(':')) errors.push(`header options is invalid in ${whichConfig}`)
})
}
})

errors.forEach(error => console.log(chalk.red(`Error: ${error}`)))
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface Options {
export interface Config {
endpoint?: string
post?: boolean
headers?: string[]
schema?: string
output?: string
fetcher?: string | SchemaFetcher
Expand Down
35 changes: 28 additions & 7 deletions src/schema/fetchSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,41 @@ export interface SchemaFetcher {
(query: string, fetchImpl: typeof fetch, qsImpl: typeof qs): Promise<ExecutionResult<IntrospectionQuery>>
}

export const get = <T>(uri: string, query: { [arg: string]: any }): Promise<T> =>
fetch(`${uri}?${qs.stringify(query)}`).then(r => r.json())
export const get = <T>(uri: string, query: { [arg: string]: any }, headers: HeadersInit = {}): Promise<T> =>
fetch(`${uri}?${qs.stringify(query)}`, {
headers: { ...headers },
}).then(r => r.json())

export const post = <T>(uri: string, body: { [arg: string]: any }): Promise<T> =>
export const post = <T>(uri: string, body: { [arg: string]: any }, headers: HeadersInit = {}): Promise<T> =>
fetch(uri, {
method: 'POST',
body: JSON.stringify(body),
headers: { 'Content-Type': 'application/json' },
headers: { 'Content-Type': 'application/json', ...headers },
}).then(r => r.json())

export const fetchSchema = async (endpoint: string, usePost = false, options?: GraphQLSchemaValidationOptions) => {
export const fetchSchema = async (
endpoint: string,
usePost = false,
options?: GraphQLSchemaValidationOptions,
headers?: string[],
) => {
const jsonHeaders = headers
? headers.reduce(
(retval, header) => {
const split = header.split(':')
if (split.length > 1) {
retval[split[0].trim()] = split[1].trim()
}
return retval
},
{} as {
[key: string]: string
},
)
: undefined
const result = usePost
? await post<ExecutionResult<IntrospectionQuery>>(endpoint, { query: getIntrospectionQuery() })
: await get<ExecutionResult<IntrospectionQuery>>(endpoint, { query: getIntrospectionQuery() })
? await post<ExecutionResult<IntrospectionQuery>>(endpoint, { query: getIntrospectionQuery() }, jsonHeaders)
: await get<ExecutionResult<IntrospectionQuery>>(endpoint, { query: getIntrospectionQuery() }, jsonHeaders)

if (!result.data) {
throw new Error('introspection request did not receive a valid response')
Expand Down