IBM / openapi-to-graphql

Translate APIs described by OpenAPI Specifications (OAS) into GraphQL
https://developer.ibm.com/open/projects/openapi-to-graphql/
MIT License
1.61k stars 211 forks source link

Arrays in query strings with cross-fetch #421

Open thomsonbw opened 3 years ago

thomsonbw commented 3 years ago

"request" used the "qs" query string manipulation package and exposed its options in the requestOptions object. One of these, the "arrayFormat" setting, supported a variety of formats for an array parameter { a: [ "b", "c"] } 'indices' a[0]=b&a[1]=c (the default) 'brackets' a[]=b&a[]=c 'repeat' a=b&a=c 'comma' a=b,c

With the switch to cross-fetch this flexibility was lost, as query parameters are handled by resolver_builder code that only implements the first format.

eokoneyo commented 2 years ago

@thomsonbw I've also stumbled on this problem, the unfortunate issue is there is no standard way of handling arrays in query strings, there's a whole discussion about this here https://stackoverflow.com/questions/6243051/how-to-pass-an-array-within-a-query-string. I wonder if it might make sense to have a config that allows the end user to specify how they'd like their arrays processed

thomsonbw commented 2 years ago

@eokoneyo we effectively had a config option before cross-fetch. You could set the arrayFormat in the o2g requestOptions object and it would be passed through to the underlying package, but the current code doesn't provide similar flexibility.

Alan-Cha commented 2 years ago

@thomsonbw Sorry for the very late reply. I'm also sorry that this functionality was lost. I'll try to look into this but if any of you have suggestions on how this could be resolved, I'd love to know.

DanailPenev commented 2 years ago

@thomsonbw, I have opened a Pull request that adds an option for one of the other formats https://github.com/IBM/openapi-to-graphql/pull/453

nicolasparada commented 2 years ago

It's true that there are many ways to define arrays in query strings, but URLSearchParams API uses the "repeat" format:

const q = new URLSearchParams()
q.append("test", "one")
q.append("test", "two")
q.append("test", "three")
q.toString() === "test=one&test=two&test=three"

It should at the very least support that.