diff --git a/CHANGELOG.md b/CHANGELOG.md index caa9256b98..e34cbcd410 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,8 @@ - **github**: Allow for error handling - **cli**: Adds a relevant file path to an error message [#1887](https://github.com/kamilkisiela/graphql-inspector/pull/1887) - **cli**: Adds `--left-header` and `--right-header` [#1899](https://github.com/kamilkisiela/graphql-inspector/pull/1899) -- **core**: add rule to make dangerous changes breaking (`dangerousBreaking`) [#1899](https://github.com/kamilkisiela/graphql-inspector/pull/1899) +- **core**: Add rule to make dangerous changes breaking (`dangerousBreaking`) [#1899](https://github.com/kamilkisiela/graphql-inspector/pull/1899) +- **core**: Make sure default argument values are printable [#1959](https://github.com/kamilkisiela/graphql-inspector/pull/1959) ### v2.5.0 diff --git a/packages/core/__tests__/utils/string.ts b/packages/core/__tests__/utils/string.ts new file mode 100644 index 0000000000..ea282387c1 --- /dev/null +++ b/packages/core/__tests__/utils/string.ts @@ -0,0 +1,20 @@ +import {safeString} from '../../src/utils/string'; + +test('scalars', () => { + expect(safeString(0)).toBe('0'); + expect(safeString(42)).toBe('42'); + expect(safeString(42.42)).toBe('42.42'); + expect(safeString('42')).toBe('42'); + expect(safeString('true')).toBe('true'); + expect(safeString(true)).toBe('true'); + expect(safeString('false')).toBe('false'); + expect(safeString(false)).toBe('false'); +}); + +test('null', () => { + expect(safeString(null)).toBe('null'); +}); + +test('undefined', () => { + expect(safeString(undefined)).toBe('undefined'); +}); diff --git a/packages/core/package.json b/packages/core/package.json index 3a1325ce3d..ce1d6e79d2 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -30,8 +30,12 @@ }, "dependencies": { "dependency-graph": "0.11.0", + "object-inspect": "1.10.3", "tslib": "^2.0.0" }, + "devDependencies": { + "@types/object-inspect": "1.8.0" + }, "scripts": { "prepack": "bob prepack" } diff --git a/packages/core/src/utils/string.ts b/packages/core/src/utils/string.ts index ea76c5fab4..a2f1b166fe 100644 --- a/packages/core/src/utils/string.ts +++ b/packages/core/src/utils/string.ts @@ -1,3 +1,5 @@ +import inspect from 'object-inspect'; + export interface Target { typeId: string; value: string; @@ -75,3 +77,10 @@ function wordLetterPairs(str: string) { const pairs = str.toUpperCase().split(' ').map(letterPairs); return flattenDeep(pairs); } + +export function safeString(obj: any) { + if (obj != null && typeof obj.toString === 'function') { + return `${obj}`; + } + return inspect(obj); +} diff --git a/yarn.lock b/yarn.lock index b8e08512f9..122e8790b0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2467,6 +2467,11 @@ resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e" integrity sha512-f5j5b/Gf71L+dbqxIpQ4Z2WlmI/mPJ0fOkGGmFgtb6sAu97EPczzbS3/tJKxmcYDj55OX6ssqwDAWOHIYDRDGA== +"@types/object-inspect@1.8.0": + version "1.8.0" + resolved "https://registry.yarnpkg.com/@types/object-inspect/-/object-inspect-1.8.0.tgz#d3a4537b771d06af85bdd2f25d20954433c25c49" + integrity sha512-r0xcaoZLwB0qCiR4lw1xZue3T9p8i/JdKslESAwfMEvjHIYK8DhQaV02K6WTJ29ueZOUe9ttscvP2Tv3ujBAHg== + "@types/parse-json@^4.0.0": version "4.0.0" resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0" @@ -7685,6 +7690,11 @@ object-copy@^0.1.0: define-property "^0.2.5" kind-of "^3.0.3" +object-inspect@1.10.3: + version "1.10.3" + resolved "https://registry.yarnpkg.com/object-inspect/-/object-inspect-1.10.3.tgz#c2aa7d2d09f50c99375704f7a0adf24c5782d369" + integrity sha512-e5mCJlSH7poANfC8z8S9s9S2IN5/4Zb3aZ33f5s8YqoazCFzNLloLU8r5VCG+G7WoqLvAAZoVMcy3tp/3X0Plw== + object-inspect@^1.7.0, object-inspect@^1.8.0: version "1.8.0" resolved "https://registry.yarnpkg.com/object-inspect/-/object-inspect-1.8.0.tgz#df807e5ecf53a609cc6bfe93eac3cc7be5b3a9d0"