-
-
Notifications
You must be signed in to change notification settings - Fork 28
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
Custom fetch
function
#1563
Comments
Hey there, I am not really familiar with SvelteKit, is there Our generated client allows you to write your own query fetcher, does that serves that Svelte needs? |
No the |
No worries, I don't think it's out of scope. Do you want to pass a fetch impl for each fetch? Because tRPC's httpLink feels like it is defined at top level where your |
You are right, the |
Understood, adding such an option in the |
Hi again, came back to this and saw the new updated docs. Looks good! This is the custom query fetcher I'm using. Looking forward to seeing the official SvelteKit integration later. export const gqty = (event: Partial<RequestEvent> & Pick<RequestEvent, 'fetch'>) => {
const { resolve } = createClient<GeneratedSchema>({
schema: generatedSchema,
scalars: scalarsEnumsHash,
cache,
fetchOptions: {
fetcher: async ({ query, variables, operationName }, fetchOptions) => {
const response = await event.fetch('http://macbook:3005/api/graphql', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
query,
variables,
operationName
}),
...fetchOptions
})
return await response.json()
}
}
})
return resolve
} And then fetching data like this with SSR working as expected. export const load = async ({ url, ...event }) => {
const query = url.searchParams.get('query') ?? ''
const { results } = await gqty(event)(({ query: { search } }) => ({
results: search({ query }).map((result) => ({ ...result }))
}))
return {
query,
results,
deffered: {
locales: gqty(event)(({ query: { locales } }) =>
locales.map((locale) => ({
...locale
}))
)
}
}
} |
@hanssonduck Thanks for trying out the alpha, our internal tests are almost done and we are close to announce a public beta! Let me leak a little bit of my unreleased plans. There is this I am planning to expose the You will be able to do this in the future: await resolve(
({ query }) => ({ locales: query.locales.map((locale) => ({ ...locale })) }),
{
extensions: {
fetch: event.fetch,
},
}
); You can then make your queryFetcher generic again like this: const queryFetcher = async (
{ query, variables, operationName, extensions },
fetchOptions
) => {
const fetchImpl = extensions?.fetch ?? fetch;
const response = await fetchImpl("http://macbook:3005/api/graphql", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
query,
variables,
operationName,
}),
...fetchOptions,
});
return await response.json();
}; |
@vicary That seems like a great solution. Looking forward to the alpha version getting stable! |
@hanssonduck I want to slip this through before alpha ends, you may try latest alpha and let me know how it feels. |
Please feel free to re-open if you think the current approach is not enough for your use case! |
Hello!
This issue is related to #1185.
In SvelteKit you need to use their custom
fetch
function when making http calls inside load functions. See https://kit.svelte.dev/docs/load#making-fetch-requests and https://kit.svelte.dev/docs/web-standards#fetch-apis.One solution would be to simply allow passing a custom
fetch
function like we already do forfetchOptions
.The text was updated successfully, but these errors were encountered: