Skip to content

Commit

Permalink
Fixes #2911: Nested query parameters shouldn't cause errors. (#3635)
Browse files Browse the repository at this point in the history
* Fixes #2911: Nested query parameters shouldn't cause errors.

* ..

* ..

Co-authored-by: Arda TANRIKULU <[email protected]>
  • Loading branch information
JasonKong-Quantium and ardatan authored Aug 3, 2022
1 parent 54f87a6 commit 42e19c5
Show file tree
Hide file tree
Showing 6 changed files with 567 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changeset/tender-walls-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-mesh/openapi': patch
---

Use `qs` to stringify query parameters because URLSearchParameters doesn't respect nested values
1 change: 1 addition & 0 deletions packages/handlers/openapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"openapi-diff": "0.23.5",
"graphql-scalars": "1.17.0",
"pluralize": "8.0.0",
"qs": "6.10.3",
"swagger2openapi": "7.0.8",
"url-join": "4.0.1",
"openapi-types": "12.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { PreprocessingData } from './types/preprocessing_data';
// Imports:
import * as Oas3Tools from './oas_3_tools';
import * as JSONPath from 'jsonpath-plus';
import qs from 'qs';
import { GraphQLError, GraphQLFieldResolver, GraphQLResolveInfo } from 'graphql';
import formurlencoded from 'form-urlencoded';
import urlJoin from 'url-join';
Expand Down Expand Up @@ -468,17 +469,9 @@ export function getResolver<TSource, TContext, TArgs>(
};
}

for (const paramName in query) {
const val = query[paramName];
const allQueryParams = {};

if (Array.isArray(val)) {
for (let index = 0; index < val.length; index++) {
urlObject.searchParams.append(paramName, val[index]);
}
} else if (val !== undefined) {
urlObject.searchParams.set(paramName, val);
}
}
Object.assign(allQueryParams, query);

/**
* Determine possible payload
Expand Down Expand Up @@ -521,22 +514,12 @@ export function getResolver<TSource, TContext, TArgs>(
}
// Query string:
if (typeof data.options.qs === 'object') {
for (const query in data.options.qs) {
const val = data.options.qs[query];
if (val) {
urlObject.searchParams.set(query, val);
}
}
Object.assign(allQueryParams, data.options.qs);
}
}

if (typeof customQs === 'object') {
for (const query in customQs) {
const val = customQs[query];
if (val) {
urlObject.searchParams.set(query, val);
}
}
Object.assign(allQueryParams, customQs);
}

// Get authentication headers and query parameters
Expand All @@ -550,12 +533,8 @@ export function getResolver<TSource, TContext, TArgs>(
options.headers[headerName] = headerValue;
}
}
for (const query in authQs) {
const val = authQs[query];
if (val) {
urlObject.searchParams.set(query, val);
}
}

Object.assign(allQueryParams, authQs);

// Add authentication cookie if created
if (authCookie !== null) {
Expand All @@ -567,12 +546,7 @@ export function getResolver<TSource, TContext, TArgs>(
// Extract OAuth token from context (if available)
if (data.options.sendOAuthTokenInQuery) {
const oauthQueryObj = createOAuthQS(data, ctx, logger);
for (const query in oauthQueryObj) {
const val = oauthQueryObj[query];
if (val) {
urlObject.searchParams.set(query, val);
}
}
Object.assign(allQueryParams, oauthQueryObj);
} else {
const oauthHeader = createOAuthHeader(data, ctx, logger);
for (const headerName in oauthHeader) {
Expand All @@ -583,6 +557,8 @@ export function getResolver<TSource, TContext, TArgs>(
}
}

urlObject.search = qs.stringify(allQueryParams);

const urlWithoutQuery = urlObject.href.replace(urlObject.search, '');
resolveData.url = urlWithoutQuery;
resolveData.usedRequestOptions = Object.assign({}, options);
Expand Down
Loading

1 comment on commit 42e19c5

@vercel
Copy link

@vercel vercel bot commented on 42e19c5 Aug 3, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.