Skip to content

Commit

Permalink
feat(postgraphql): improve type identifier parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
calebmer committed Oct 9, 2016
1 parent 0acf0bc commit f8b2d42
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/postgraphql/cli/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ program
.option('-i, --graphiql <path>', 'the route to mount the GraphiQL interface on. defaults to `/graphiql`')
.option('-b, --disable-graphiql', 'disables the GraphiQL interface. overrides the GraphiQL route option')
.option('-e, --secret <string>', 'the secret to be used when creating and verifying JWTs. if none is provided auth will be disabled')
.option('-t, --token <identifier>', 'the Postgres identifier for a composite type that will be used to create tokens', (option: string) => ({ namespaceName: option.split('.', 2)[0], typeName: option.split('.', 2)[1] }))
.option('-t, --token <identifier>', 'the Postgres identifier for a composite type that will be used to create tokens', parseTypeIdentifier)
.option('-o, --cors', 'enable generous CORS settings. this is disabled by default, if possible use a proxy instead')
.option('-a, --classic-ids', 'use classic global id field name. required to support Relay 1')
.option('-j, --dynamic-json', 'enable dynamic JSON in GraphQL inputs and outputs. uses stringified JSON by default')
Expand Down Expand Up @@ -114,3 +114,21 @@ server.listen(port, hostname, () => {
console.log(chalk.gray('* * *'))
console.log('')
})

/**
* Enables the parsing of type identifiers. Type identifiers are a bit tricky,
* but this function uses a regular expression to do it. Fun.
*
* @private
*/
function parseTypeIdentifier (typeIdentifier: string): { namespaceName: string, typeName: string } {
const match = typeIdentifier.match(/^(?:([a-zA-Z1-2_]+)|"([^"]*)")\.(?:([a-zA-Z1-2_]+)|"([^"]*)")$/)

if (!match)
throw new Error(`Type identifier '${typeIdentifier}' is of the incorrect form.`)

return {
namespaceName: match[1] || match[2],
typeName: match[3] || match[4],
}
}
7 changes: 7 additions & 0 deletions src/postgraphql/postgraphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ export default function postgraphql (
return subGraphqlSchema
})()

// If we fail to build our schema, log the error and exit the process.
graphqlSchema.catch(error => {
// tslint:disable-next-line no-console
console.error(`${error.stack}\n`)
process.exit(1)
})

// Finally create our HTTP request handler using our options, the Postgres
// pool, and GraphQL schema. Return the final result.
return createPostGraphQLHTTPRequestHandler(Object.assign({}, options, {
Expand Down

0 comments on commit f8b2d42

Please sign in to comment.