Skip to content
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 #61

Closed
albertms10 opened this issue Oct 11, 2020 · 9 comments
Closed

Error: Function called outside component initialization #61

albertms10 opened this issue Oct 11, 2020 · 9 comments

Comments

@albertms10
Copy link

albertms10 commented Oct 11, 2020

While setting up svelte-apollo with Sapper (#9), as there is no main App.svelte component, I tried to set the client in the _layout.svelte file.

<!-- _layout.svelte -->
<script lang="ts">
  import { setClient } from "svelte-apollo";
  import client from "../database/apollo";

  setClient(client);
</script>

However, I get the following error:

Uncaught (in promise) Error: Function called outside component initialization
    at get_current_component (index.mjs:629)
    at setContext (index.mjs:659)
    at setClient (svelte-apollo.es.js:14)
    at _layout.svelte:33
    at run (index.mjs:18)
    at Array.map (<anonymous>)
    at index.mjs:1390
    at flush (index.mjs:723)
    at init (index.mjs:1477)
    at new App (App.svelte:17)

Where should I set the client instead?

Edit

In previous versions of v0.3.0, the solution that #9 (comment) comments would work fine, but in v0.4.0 we must set the client with the proper method.

@patrickleet
Copy link

you need to add svelte-apollo as a dev dependency not a dependency

@albertms10
Copy link
Author

Whoops, totally correct. Thanks!

@patrickleet
Copy link

well, it's not exactly intuitive - I made the same mistake :)

@SamJacobs
Copy link

@patrickleet why would you add it as a dev dependency? it's part of the build right?
i did try that and it's still not fixing the problem for me, where does that fix come from?

@patrickleet
Copy link

sveltejs/sapper#592

@patrickleet
Copy link

A bundle is created so technically most of the deps can just exist for the build step.

I saw more discussion somewhere about what should and shouldn't be a devDep with that in mind but can't find where that was.

@fnune
Copy link

fnune commented Apr 3, 2021

I have the same problem and moving svelte-apollo to dev dependencies doesn't help:

// App.ts
<script lang="ts">
  import { ApolloClient, InMemoryCache } from "@apollo/client"
  import { setClient } from "svelte-apollo"

  setClient(new ApolloClient({ cache: new InMemoryCache() }))
</script>

<main />

<style lang="scss">
</style>

One special aspect of my setup is that I have a Yarn workspace and my Svelte app (built with Svelte-Kit) sits inside one of its packages. So the node_modules directory is on the root of the workspace, and the Svelte app has its own node_modules directory that contains only .vite and .bin.

@flashspys
Copy link

flashspys commented Apr 11, 2021

If svelte is built by vite, you have to add svelte-apollo to the optimizeDeps.exclude array in vite.config.js
Should this piece of information be added in the readme?

tmaxmax added a commit to tmaxmax/svelte-apollo that referenced this issue Apr 27, 2021
The error "Function called outside component initialization" is encountered by many, and its cause always is obscure to the developer. This issue is commonly raised on Stackoverflow, Github, and other platforms, with no success at receiving a concrete solution.

This was also recently mentioned in issue timhall#61, but no initiative was taken.
@markusschmitz53
Copy link

dd svelte-apollo to the optimizeDeps.exclude array in vite.config.js
Should this piec

Unfortunately, this doesn't solve the issue at hand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants