Skip to content

Commit ba06c57

Browse files
committed
Support explode attribute for query string serialization
- Related issue: ardatan#1255 - Related PR: IBM/openapi-to-graphql#312
1 parent c5e763e commit ba06c57

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

packages/handlers/openapi/src/openapi-to-graphql/resolver_builder.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,17 @@ export function getResolver<TSource, TContext, TArgs>(
478478
for (const paramName in query) {
479479
const val = query[paramName];
480480
if (val !== undefined) {
481-
urlObject.searchParams.set(paramName, val);
481+
/**
482+
* If the query parameter is an array, that means `explode: true` was set and we should append each key/value
483+
* as a new search parameter. Else, it is a either a singular value or a comma-separated string of the values
484+
* and we can just set the search parameter.
485+
* https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams
486+
*/
487+
if (Array.isArray(val)) {
488+
val.forEach(v => urlObject.searchParams.append(paramName, v));
489+
} else {
490+
urlObject.searchParams.set(paramName, val);
491+
}
482492
}
483493
}
484494

@@ -1235,7 +1245,18 @@ export function extractRequestDataFromArgs<TSource, TContext, TArgs>(
12351245

12361246
// Query parameters
12371247
case 'query':
1238-
qs[param.name] = args[sanitizedParamName];
1248+
/**
1249+
* Spec-compliant query string serialization:
1250+
* http:pec.openapis.org/oas/v3.0.3#style-examples
1251+
*
1252+
* Whenever the query string value is an array, we check if it
1253+
* should be `exploded`. In this case, we don't serialize anything.
1254+
* Otherwise, the array will be joined in comma-separated fashion.
1255+
*/
1256+
const arg = args[sanitizedParamName];
1257+
const shouldBeCommaSeparated = Array.isArray(arg) && !param.explode;
1258+
1259+
qs[param.name] = shouldBeCommaSeparated ? arg.join(',') : arg;
12391260
break;
12401261

12411262
// Header parameters

0 commit comments

Comments
 (0)