-
-
Notifications
You must be signed in to change notification settings - Fork 68
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
Error: Function called outside component initialization #99
Comments
I have encountered the same problem, and finally solved this by copy the svelte-apollo code to my own repo and modify the context.ts to use a global variable rather than svelte context: import type { ApolloClient } from "@apollo/client";
let globalClient: ApolloClient<any>
export function getClient<TCache = any>(): ApolloClient<TCache> {
const client = globalClient;
if (!client) {
throw new Error(
"ApolloClient has not been set yet, use setClient(new ApolloClient({ ... })) to define it"
);
}
return client as ApolloClient<TCache>;
}
export function setClient<TCache = any>(client: ApolloClient<TCache>): void {
globalClient = client
} |
Same problem here, but i couldn't find where to put these. |
Same issue here. Couldn't make @xpol's code work either. Still seeing the error when trying to execute a query from a function within the script block of a .svelte component. |
To fix that problem I did the following:
So, I initialize my mutation from the svelte component and use it where I need it:
|
hey @grodasgomez it seems to me your code doesn't fix the issue, as you still call the closure from within component context? If you were to try and call createAddMutation() from any .js file (as op wrote) you still get the "Function called outside component initialization" error. |
@markusschmitz53 you're right, I misunderstood the problem, my code only works if we call it from a .svelte file, I think we can't call it from a regular js file for now. If the base code is refactored to something like @xpol's code, we'll be able to do it |
@timhall Multiple forks have been made to address this issue. I can make a PR for it if the repo is being maintained https://github.com/tenno-dev/svelte-apollo/commit/c95741b08b7798161b8104ee4bfa956508cea7f2 |
Hi, guys. Any updates on this? @timhall |
@timhall |
Its been many months and still nothing on this. Safe to assume @timhall is no longer supporting. |
I'm trying to refactor some CRUD functions from some Svelte (gui) components into a JS file.
Does svelte-apollo also work within a regular js file or do I need to instantiate query and mutation inside the script tags of a Svelte component?
This works inside a Svelte component
This throws an error in a regular js file when trying to call update() from a component
Error: Function called outside component initialization
The text was updated successfully, but these errors were encountered: