From 8ebc4a71340244234e1a071e5adf0d83671082e0 Mon Sep 17 00:00:00 2001 From: linchen1987 Date: Tue, 26 Sep 2023 09:46:31 +0800 Subject: [PATCH 1/4] fix: gql request crashs when formatting scalar args --- packages/sdk-util/src/util.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/packages/sdk-util/src/util.js b/packages/sdk-util/src/util.js index 5d1ba40..eda9dc6 100644 --- a/packages/sdk-util/src/util.js +++ b/packages/sdk-util/src/util.js @@ -139,11 +139,13 @@ const makeQuery = (fields, ignoreFields, argValues = {}) => ` .map(x => { const subQueryStr = `${x.name} { __typename - ${x.possibleTypes.filter(t => !ignoreFields.includes(t.name)).map( - t => `... on ${t.name} { + ${x.possibleTypes + .filter(t => !ignoreFields.includes(t.name)) + .map( + t => `... on ${t.name} { ${makeQuery(t.fields, ignoreFields, argValues).trim()} }` - )} + )} }`; return subQueryStr; @@ -190,12 +192,14 @@ const formatArgs = (values, specs = {}) => { // escape slash(\) and double quotes (") if ('String' === type) { - const container = isNull(value) ? null : (value || '').includes('\n') ? '"""' : '"'; - - return isNull(value) ? null : `${container}${value - .toString() - .replace(/\\/g, '\\\\') - .replace(/"/g, '\\"')}${container}`; + const container = isNull(value) ? null : String(value).includes('\n') ? '"""' : '"'; + + return isNull(value) + ? null + : `${container}${value + .toString() + .replace(/\\/g, '\\\\') + .replace(/"/g, '\\"')}${container}`; } if (['DateTime', 'ID', 'HexString'].includes(type)) { From de6c00aff6e4d923d5362bf79527d150ca80f361 Mon Sep 17 00:00:00 2001 From: linchen1987 Date: Tue, 26 Sep 2023 09:47:07 +0800 Subject: [PATCH 2/4] chore: bump version --- CHANGELOG.md | 3 +++ version | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e6dacc..98543ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 0.34.3 (September 26, 2023) +- fix: gql request crashes when formatting scalar args + ## 0.34.2 (September 26, 2023) - fix: gql request crashes when formatting scalar args diff --git a/version b/version index 3f8003c..0aeaf41 100644 --- a/version +++ b/version @@ -1 +1 @@ -0.34.2 +0.34.3 From 01bc70b103f523cd2a6ad1ee689b96bd6b964a41 Mon Sep 17 00:00:00 2001 From: linchen1987 Date: Tue, 26 Sep 2023 09:50:34 +0800 Subject: [PATCH 3/4] fix --- packages/sdk-util/src/util.js | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/packages/sdk-util/src/util.js b/packages/sdk-util/src/util.js index eda9dc6..18bb0fa 100644 --- a/packages/sdk-util/src/util.js +++ b/packages/sdk-util/src/util.js @@ -139,13 +139,11 @@ const makeQuery = (fields, ignoreFields, argValues = {}) => ` .map(x => { const subQueryStr = `${x.name} { __typename - ${x.possibleTypes - .filter(t => !ignoreFields.includes(t.name)) - .map( - t => `... on ${t.name} { + ${x.possibleTypes.filter(t => !ignoreFields.includes(t.name)).map( + t => `... on ${t.name} { ${makeQuery(t.fields, ignoreFields, argValues).trim()} }` - )} + )} }`; return subQueryStr; @@ -194,12 +192,10 @@ const formatArgs = (values, specs = {}) => { if ('String' === type) { const container = isNull(value) ? null : String(value).includes('\n') ? '"""' : '"'; - return isNull(value) - ? null - : `${container}${value - .toString() - .replace(/\\/g, '\\\\') - .replace(/"/g, '\\"')}${container}`; + return isNull(value) ? null : `${container}${value + .toString() + .replace(/\\/g, '\\\\') + .replace(/"/g, '\\"')}${container}`; } if (['DateTime', 'ID', 'HexString'].includes(type)) { From 82629ae71c276b6f38bcb3782e52d4bca0d8ddde Mon Sep 17 00:00:00 2001 From: linchen1987 Date: Tue, 26 Sep 2023 10:27:33 +0800 Subject: [PATCH 4/4] add test --- packages/sdk-util/test/util.spec.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/sdk-util/test/util.spec.js b/packages/sdk-util/test/util.spec.js index ead0920..48f26ea 100644 --- a/packages/sdk-util/test/util.spec.js +++ b/packages/sdk-util/test/util.spec.js @@ -107,6 +107,21 @@ describe('#formatArgs', () => { expect(args).toEqual('address: "xxx", height: 123'); }); + test('should process scalar types correctly (has break line)', () => { + const args = formatArgs({ address: 'ab\ncd', height: 123 }, specs); + expect(args).toEqual('address: """ab\ncd""", height: 123'); + }); + + test('should process scalar types correctly (object in string)', () => { + const args = formatArgs({ address: { name: 'abc' }, height: 123 }, specs); + expect(args).toEqual('address: "[object Object]", height: 123'); + }); + + test('should process scalar types correctly (null)', () => { + const args = formatArgs({ address: null, height: 123 }, specs); + expect(args).toEqual('address: null, height: 123'); + }); + test('should process null scalar values correctly', () => { const args = formatArgs({ address: null, height: 123 }, specs); expect(args).toEqual('address: null, height: 123');