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

Fixes #2911: Nested query parameters shouldn't cause errors. #3635

Merged
merged 3 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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