DustinJSilk / qwik-urql

Urql support for Qwik projects.
MIT License
19 stars 6 forks source link

Can't use useQuery inside onGet #36

Open tuurbo opened 1 year ago

tuurbo commented 1 year ago

I'm curious your thoughts on not being able to use useQuery, useMutation, etc.. within the onGet method. You will receive this error if you try. [vite] Internal server error: Code(14): Invoking 'use*()' method outside of invocation context.

https://qwik.builder.io/qwikcity/data/retrieve/#implement-onget

export const onGet: RequestHandler = ({ request }) => {
  return useQuery(Query, {}, { requestPolicy: 'network-only' });
};

Also, I believe the only way to modify the page title, meta tags, etc.. is through this DocumentHead method below, which receives its data from onGet. https://qwik.builder.io/qwikcity/content/head/#dynamic-document-head

export const head: DocumentHead<EndpointData> = ({ data }) => {
  return {
    title: `Product - ` + data.description,
  };
};
DustinJSilk commented 1 year ago

I’ve been thinking about this quite a bit lately, actually. I don’t quite have an answer yet.

Qwiks API is quite limiting for anyone coming from a SPA with a separate API, unfortunately.

There’s a few other issues, some we can work around:

I can get the client working in an onGet without using hooks, but that wouldn’t solve the 2nd and 3rd points above. At least it’s something.

There might be a nice way to solve this with some kind of Qwik City support for a function similar to onGet but it could be loaded to the browser, unlike the onGet, and it’s data could also be called in a useEndpoint and returned in the head function. onQuery, onRequest, onFetch?

I’m not sure if the Qwik team would be open to something like this, they’ve been mostly unresponsive to my concerns so my guess is that it would take a PR along with some good justification to get something like this merged in.

tuurbo commented 1 year ago

Thanks for the detailed response. I'm currently using a simple fetch w/ graphql-request and @graphql-codegen, and a mix of onGet and useResource$ as I migrate an ecommerce site from Angular 14 to Qwik. I have 2 to 3 months of migrating to go, so hoping it works out in the end without a major refactor.

DustinJSilk commented 1 year ago

You’re welcome. Hopefully this will be clearer soon. Im also going to be migrating a large angular + Apollo site to qwik soon. I haven’t started yet, this is all in preparation and the only major hurdle I see.

Please let me know how it goes, happy to help when I can. I’ll scope this issue out some more over the next week