Skip to content

Commit

Permalink
SCAL-216295 : Fix for objects in arrays (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
sastaachar authored Aug 8, 2024
1 parent 4cd38b0 commit 06328a4
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@thoughtspot/rise",
"version": "0.7.8",
"version": "0.7.9",
"description": "Rise above the REST with GraphQL",
"main": "dist/index.js",
"scripts": {
Expand Down
17 changes: 13 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,20 @@ export function rise(
args = { ...args, ...source?.__args };

Check warning on line 154 in src/index.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Assignment to function parameter 'args'

Object.keys(args).forEach((arg) => {
if (typeof args[arg] !== 'object' || Array.isArray(args[arg])) {
urlToFetch = urlToFetch.replace(`$${arg}`, encodeURI(args[arg]));
} else {
urlToFetch = urlToFetch.replace(`$${arg}`, '');
const argToReplace = `$${arg}`;
if (!urlToFetch.includes(argToReplace)) return;

let argToReplaceValue = '';

try {
if (typeof args[arg] !== 'object' || Array.isArray(args[arg])) {
argToReplaceValue = encodeURI(args[arg]);
}
} catch (e) {
console.debug(`[Rise] Error encoding ${arg}, Message: ${(e as any)?.message || ''}`);

Check warning on line 167 in src/index.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Unexpected console statement
}

urlToFetch = urlToFetch.replace(argToReplace, argToReplaceValue);
});

if (method !== 'GET' && method !== 'HEAD') {
Expand Down
36 changes: 36 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,42 @@ describe('Should call the Target', () => {
});
expect(res3.errors).toBeUndefined();
});

test('when array of object is passed', async () => {
const testIdentifier = 'test123';
nock('https://rise.com/callosum/v1')
.get(() => true)
.reply(200, function () {
const query = new URLSearchParams(this.req.path.split('?')[1]);
expect(this.req.path).toContain(`/v2/users/${testIdentifier}`);
expect(query.get('paramObj')).toEqual('');
return {
data: {
params: query.get('paramObj'),
},
};
});
const res = await graphql({
schema,
source: `
mutation {
getUserWithQueryParamArrayObject(identifier: "${testIdentifier}", paramObj: [{email: "hi"}]) {
params
}
}
`,
contextValue: {
req: {
headers: {
cookie: 'a=a',
foo: 'bar',
},
},
},
});

expect(res.errors).toBeUndefined();
});
});
});

Expand Down
6 changes: 6 additions & 0 deletions test/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,10 @@
path: "/v2/users/?params=$qParamsStr&paramsInt=$qParamsInt&test=yes&paramObj=$paramObj",
method: "GET",
),

getUserWithQueryParamArrayObject(identifier: String!, paramObj: [ParamInput]): ParamResponse
@service(
path: "/v2/users/$identifier?paramObj=$paramObj",
method: "GET",
),
}

0 comments on commit 06328a4

Please sign in to comment.