Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pgsettings): Only use settings of type string or number #589

Merged
merged 4 commits into from
Nov 1, 2017
Merged

Conversation

skvale
Copy link

@skvale skvale commented Sep 26, 2017

For example, instead of an undefined pgSetting becoming "undefined" turn it into null so set_config can still accept it

Fixes: #566

Copy link
Member

@benjie benjie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for raising this PR! 🙏 This is a good start, but I'd like a few tweaks in behaviour before it's ready to merge.

I'm thinking at the moment that this should go into v4 because it's effectively a breaking change; however if it's important to you that it goes into v3 we should discuss the comments I've made and come up with a non-breaking way forward (I'm thinking wherever the throws are we should instead output deprecation notices and then String(...) as you've done).

Let me know which way you want to go with this 👍

'some.setting.not.defined': null,
'some.setting.zero': null,
'number.setting': '42',
'obj': 'bar',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These colons should be commas.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to just completely omit the null values than to set them to null - it should be more efficient and should be equivalent (assuming the values are not set globally, for example).

@@ -163,7 +163,7 @@ async function setupPgClientTransaction ({
// this prevents an accidentional overwriting
if (typeof pgSettings === 'object') {
for (const key of Object.keys(pgSettings)) {
localSettings.set(key, String(pgSettings[key]))
localSettings.set(key, !!pgSettings[key] ? String(pgSettings[key]) : null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a safe way to check if the value should be excluded - they might deliberately be setting it to the empty string "" or to the number zero 0 and this will instead set null. A safer way would be pgSettings[key] != null ? String(...) : null; however I'd rather throw if the values are invalid:

strings and numbers should be passed through, null/undefined should be dropped, and everything else should throw an error.
-- #566 (comment)

So I'd like to see some typeof's around here. For anyone wondering: I'm deliberately suggesting we should throw on booleans, and on objects that have a toString value.

@skvale
Copy link
Author

skvale commented Sep 26, 2017

@benjie
That sounds good.

  • I wasn't sure how to output deprecation warnings just console.warn?
  • Still thinking of a better error/deprecation message "Error converting pgSetting: ${pgSetting} is an Object."
  • Could also throw an error for Symbol types in response to

    everything else should throw an error

  • Not sure why we should throw for booleans, but could add that check too

@benjie
Copy link
Member

benjie commented Sep 26, 2017

I wasn't sure how to output deprecation warnings just console.warn?

Yup; with DEPRECATED prefixed. Feel free to use chalk to make it more obvious!

Still thinking of a better error/deprecation message "Error converting pgSetting: ${pgSetting} is an Object."

Generally you should add what it is (as you have done) and what it should be - i.e. append "expected string or number" (no need to include null in the list IMO).

Could also throw an error for Symbol types in response to

Yes, literally everything that doesn't match (pgSetting == null || ['string', 'number'].includes(typeof pgSetting) should throw.

Not sure why we should throw for booleans, but could add that check too

Because in SQL it's not clear if you'd check for it to exist or be NULL, or for it to be "0" vs "1"; or "TRUE" vs "FALSE"; or "t" vs "f"; or "true" vs "false"

Sam Kvale added 2 commits September 26, 2017 19:29
For example, instead of an undefined pgSetting becoming "undefined" turn it into null so set_config can still accept it
@skvale skvale changed the base branch from master to benjie/graphql-build September 27, 2017 00:32
@skvale
Copy link
Author

skvale commented Sep 27, 2017

I based this PR off of v4 postgraphql:benjie/graphql-build

@skvale skvale changed the title feat(pgsettings): Convert falsy settings to null instead of strings feat(pgsettings): Only use settings of type string or number Sep 27, 2017
@skvale
Copy link
Author

skvale commented Sep 30, 2017

and addressed all of the requested changes, I think.

@benjie
Copy link
Member

benjie commented Oct 29, 2017

I'd still like to see this; but the tests need to pass on CI first 👍

@benjie benjie merged commit 7a69eb2 into graphile:benjie/graphql-build Nov 1, 2017
@benjie
Copy link
Member

benjie commented Nov 1, 2017

Thanks @skvale!

Belline pushed a commit to Belline/postgraphql that referenced this pull request Dec 18, 2017
…e#589)

* feat(pgsettings): Convert falsy settings to null instead of strings

For example, instead of an undefined pgSetting becoming "undefined" turn it into null so set_config can still accept it

* feat(pgsettings): Ignore undefined an null pgSettings and throw for object types

* feat(pgsettings): Throw error for boolean values

* lint(tests): Add a missing comma
benjie added a commit that referenced this pull request Jan 27, 2020
* chore(tsdoc): enable TSDoc linting (#577)

* chore: add tests for GRAPHILE_TURBO (#582)

* chore(types): export GraphileResolverContext

* chore(release): use yarn commands

* feat(watch): manual schema reload notification (#583)

* chore(git): ignore .env file (#590)

* chore(tests): use Jest GraphQL schema serializer (#589)

* feat(omit) take "many" on constraints into account (#565)

* feat(omit) take "many" on constraints into account

Closes #505.

* move `@omit many` constraint condition to "many" relations only

and simplify the conditions a bit, outdenting `makeField` by one level

Co-authored-by: Benjie Gillam <[email protected]>

* feat(pg): support non-scalar range values (#591)

Co-authored-by: Benjie Gillam <[email protected]>

* Fix issues after rebase

* More reliable?

Co-authored-by: David Baumgold <[email protected]>
Co-authored-by: Andreas Bergmaier <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants