fix(postgraphile): check pgConfig constructor's function, not its name #992
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a bug that occurs when you plug in a pg.Pool into the postgraphile middleware while using webpack's uglify. The "quacksLikePgPool" check fails in 2 places and the postgraphile middleware never runs.
First,
(pgConfig instanceof Pool)
at https://github.com/graphile/postgraphile/blob/577c1e6df08ebe47e8880665ce99580cf7d375a7/src/postgraphile/postgraphile.ts#L223 is always false, as far as I can tell. Right now the passing comparison would be(pgConfig instanceof Pool.super_)
. I included both checks for the sake of future-proofing when node-postgres decides to change how it handles subclasses. brianc/node-postgres#1612 and brianc/node-postgres#1541Second, the check
(constructorName(pgConfig) !== 'Pool' && constructorName(pgConfig) !== 'BoundPool')
will always fail when we use the uglify plugin for webpack, because uglify eliminates subclass names. webpack-contrib/uglifyjs-webpack-plugin#269. The Uglify team does not consider this a bug, and recommends comparing constructor functions rather than names.So instead of comparing constructor names in quacksLikePgPool
(constructorName(pgConfig) !== 'Pool' && constructorName(pgConfig) !== 'BoundPool')
at https://github.com/graphile/postgraphile/blob/577c1e6df08ebe47e8880665ce99580cf7d375a7/src/postgraphile/postgraphile.ts#L227, I proposed a change to compare the actual constructor functionobj.constructor === Pool.super_
. Typescript does not recognize thatsuper_
is a property ofPool
, so I casted it as anany
type.Basically, this fix will give users the options of plugging in a pg.Pool into the middleware while using uglify.