Open pgilad opened 6 years ago
+1 for being able to configure the default qs.stringify
somehow, whether it be this method or another. All I needed was to change the arrayFormat and have to pass in a pathBuilder every time I'm passing an array in the query string. Even just a global pathBuilder
option for the RestLink would work, but this solution is definitely more robust.
@pgilad / @zachdixon I'd be happy to merge an API like this one if somebody wants to submit a pull-request! -- Sorry I didn't comment sooner. It was a busy end-of-year!
@fbartho @zachdixon @pgilad May I create the pull-request if nobody planning?
Hey all, I'm not sure if any work has been done with this yet? I'd certainly +1 the ability to use our own serialiser for the search params. I had a problem today where an array was incorrectly serialised by encodeURIComponent
and there don't appear to be any non-hacky was to solve the issue.
In the end I intercepted the variables from teh useQuery
and serialised them before passing them as a param to the query itself. Not ideal and would be great to have control over how the serialisation works.
Checking back in -- if people want to submit a PR it's up for grabs, if no progress happens next time I do an Issue sweep, then I'll close this ticket!
Several key points come to mind after having worked with
apollo-link-rest
for a legacy backend (read: not graphql), also seeing that you guys requested comments on the design of it. I'm not a fan of having both path (backend-uri) and query (search) parameters in the same string. It makes handling more complicated cases too complex. I'm also not a fan of forcing the user to useqs
module without his ability to configure it, or use an alternative module (or none at all).For the following points, I make observations on terms:
path
: the backend-uri, not including search/filter parametersquery
: the search parameters (the ones that come after?
)The handling of
path
andquery
should be:path
remains a@rest
directive variable. We drop all legacy variations (i.e:id
) and support only{}
expansions, we also support nested objects expansions off course. The expanded variable is always converted to string (so object will turn in to [object object])path
(withoutquery
) isencodeURI
by default, configurable fromRestLink
options as a global behavior, with possible override by@rest
directive. If the user turns this off then it's the user responsibility to encode his path and variables correctly.@rest
directive, which is namedquery
(or perhapssearch
). This variable behaves similarly topath
with variable expansion - i.e using{}
. If the expanded variable is a string it isencodeURIComponent
. If the variable is an object it gets stringified using either the defaultqueryStringifiers
function, or as a dictionary lookup for a specificqueryStringifiers
function.Re:
queryStringifiers
- it is a dictionary defined atRestLink
options. Defining a default function is required if the user has an expandedquery
variable that is an object (otherwise an error is thrown). Using this strategy allows the user to opt-in to query stringifier, to opt-in for how to stringify the query object and which library to use. Off course we recommend a default function that the user can copy-paste (and installqs
on his own, the way he does today because it's apeer-dependency
).Suggested usage:
We'll need to modify how
pathBuilder
works, but overall it would be very simple (regex replace).Off course this change breaks backwards compat, and we'll need to bump
major
version for this.Thoughts? Comments?