-
-
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
Usage With Svelte Kit #97
Comments
Hey Scott, I've seen this a couple times but in my case it's only happening when called from Ignore this, I don't know if I missed it or something changed, I am seeing this everywhere now. |
I'm trying to track this down. I did find sveltejs/sapper#592 which might be relevant but I'm not entirely sure. Clarification on the many uses of "context" between svelte and svelte kit: sveltejs/kit#984 I think an important thing I recognized from these two is that svelte's Update: Scott, I just realize I assumed you are using svelte kit, are you? If not, my bad for hijacking your issue here. |
This is One workaround is to alias Here's the config path in const config = {
kit: {
vite: {
resolve: {
alias: {
'svelte-apollo': '/node_modules/svelte-apollo/dist/svelte-apollo.es.js'
},
},
}
}
}; Another trick to make it work well with sveltekit is to create a wrapper component around the layout slot then instantiate the client inside the wrapper component. |
Putting this into the optimizeDeps: {
include: [
"@apollo/client/core",
"@apollo/client/cache",
"@apollo/client/link/ws",
"@apollo/client/link/context",
"@apollo/client/link/error",
"@apollo/client/utilities",
],
exclude: ["@apollo/client", "svelte-apollo"],
}, |
Getting stuck here as well. The workaround from @unlocomqx allowed use of |
When I gave up trying to use this library everything started working. My understanding is this lib was designed to be used in svelte components and is incompatible with and possibly even redundant to using the load api. I haven't explored. In my layout component I placed my client into the load API context and accessed it from there on all other pages. |
@cschmatzler Yeah the current design only allows making a query or mutation during component initialisation because it's coupled with svelte context (which only allows reading/writing during component initialisation) The solution is to use lazy queries according to the lib author which is still unimplemented so this is currently definitely unusable |
FWIW I've written a guide for Sapper integration some time ago, which should still work in SvelteKit. Haven't got the time to do a refresh on it. Note: It uses the Apollo client directly. |
I think it's better to just encapsulate the module functions in the client directly I can now create the client in a separate file and export it, then import it wherever needed client.query(...);
client.mutate(...); Much better than dealing with the context because it's very limiting |
I get this error. Hope someone has a solution for that: > node_modules/@apollo/client/react/hooks/useReactiveVar.js:1:36: error: Could not resolve "react" (mark it as external to exclude it from the bundle)
1 │ import { useEffect, useState } from 'react';
╵ ~~~~~~~
> node_modules/@apollo/client/react/context/ApolloProvider.js:2:23: error: Could not resolve "react" (mark it as external to exclude it from the bundle)
2 │ import * as React from 'react';
╵ ~~~~~~~
> node_modules/@apollo/client/react/hooks/useApolloClient.js:2:23: error: Could not resolve "react" (mark it as external to exclude it from the bundle)
2 │ import * as React from 'react';
╵ ~~~~~~~
> node_modules/@apollo/client/react/context/ApolloConsumer.js:2:23: error: Could not resolve "react" (mark it as external to exclude it from the bundle)
2 │ import * as React from 'react';
╵ ~~~~~~~
> node_modules/@apollo/client/react/context/ApolloContext.js:1:23: error: Could not resolve "react" (mark it as external to exclude it from the bundle)
1 │ import * as React from 'react';
╵ ~~~~~~~
> node_modules/@apollo/client/react/hooks/useSubscription.js:3:68: error: Could not resolve "react" (mark it as external to exclude it from the bundle)
3 │ import { useContext, useState, useRef, useEffect, useReducer } from 'react';
╵ ~~~~~~~
> node_modules/@apollo/client/react/hooks/useMutation.js:2:56: error: Could not resolve "react" (mark it as external to exclude it from the bundle)
2 │ import { useContext, useState, useRef, useEffect } from 'react';
╵ ~~~~~~~
> node_modules/@apollo/client/react/hooks/utils/useBaseQuery.js:3:58: error: Could not resolve "react" (mark it as external to exclude it from the bundle)
3 │ import { useContext, useEffect, useReducer, useRef } from 'react';
╵ ~~~~~~~
error: Could not resolve "react" (mark it as external to exclude it from the bundle)
2 │ import { useEffect, useRef } from "react";
╵ ~~~~~~~
> node_modules/@apollo/client/react/hooks/utils/useDeepMemo.js:1:23: error: Could not resolve "react" (mark it as external to exclude it from the bundle)
1 │ import { useRef } from 'react';
╵ ~~~~~~~
> Build failed with 10 errors:
node_modules/@apollo/client/react/context/ApolloConsumer.js:2:23: error: Could not resolve "react" (mark it as external to exclude it from the bundle)
node_modules/@apollo/client/react/context/ApolloContext.js:1:23: error: Could not resolve "react" (mark it as external to exclude it from the bundle)
node_modules/@apollo/client/react/context/ApolloProvider.js:2:23: error: Could not resolve "react" (mark it as external to exclude it from the bundle)
node_modules/@apollo/client/react/hooks/useApolloClient.js:2:23: error: Could not resolve "react" (mark it as external to exclude it from the bundle)
node_modules/@apollo/client/react/hooks/useMutation.js:2:56: error: Could not resolve "react" (mark it as external to exclude it from the bundle)
...
Error: Build failed with 10 errors:
node_modules/@apollo/client/react/context/ApolloConsumer.js:2:23: error: Could not resolve "react" (mark it as external to exclude it from the bundle)
node_modules/@apollo/client/react/context/ApolloContext.js:1:23: error: Could not resolve "react" (mark it as external to exclude it from the bundle)
node_modules/@apollo/client/react/context/ApolloProvider.js:2:23: error: Could not resolve "react" (mark it as external to exclude it from the bundle)
node_modules/@apollo/client/react/hooks/useApolloClient.js:2:23: error: Could not resolve "react" (mark it as external to exclude it from the bundle)
node_modules/@apollo/client/react/hooks/useMutation.js:2:56: error: Could not resolve "react" (mark it as external to exclude it from the bundle)
...
at failureErrorWithLog (D:\dev\web\tests\crm\node_modules\esbuild\lib\main.js:1478:15)
at D:\dev\web\tests\crm\node_modules\esbuild\lib\main.js:1136:28
at runOnEndCallbacks (D:\dev\web\tests\crm\node_modules\esbuild\lib\main.js:926:63)
at buildResponseToResult (D:\dev\web\tests\crm\node_modules\esbuild\lib\main.js:1134:7)
at D:\dev\web\tests\crm\node_modules\esbuild\lib\main.js:1243:14
at D:\dev\web\tests\crm\node_modules\esbuild\lib\main.js:614:9
at handleIncomingPacket (D:\dev\web\tests\crm\node_modules\esbuild\lib\main.js:711:9)
at Socket.readFromStdout (D:\dev\web\tests\crm\node_modules\esbuild\lib\main.js:581:7)
at Socket.emit (events.js:400:28)
at Socket.emit (domain.js:470:12) package.json{
"name": "~TODO~",
"version": "0.0.1",
"scripts": {
"dev": "svelte-kit dev",
"build": "svelte-kit build",
"preview": "svelte-kit preview",
"check": "svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-check --tsconfig ./tsconfig.json --watch",
"lint": "prettier --ignore-path .gitignore --check --plugin-search-dir=. . && eslint --ignore-path .gitignore .",
"format": "prettier --ignore-path .gitignore --write --plugin-search-dir=. ."
},
"devDependencies": {
"@sveltejs/kit": "next",
"@typescript-eslint/eslint-plugin": "^4.19.0",
"@typescript-eslint/parser": "^4.19.0",
"eslint": "^7.22.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-svelte3": "^3.2.0",
"prettier": "~2.2.1",
"prettier-plugin-svelte": "^2.2.0",
"svelte": "^3.34.0",
"svelte-check": "^2.0.0",
"svelte-preprocess": "^4.0.0",
"tslib": "^2.0.0",
"typescript": "^4.0.0"
},
"type": "module",
"dependencies": {
"@apollo/client": "^3.4.11",
"graphql": "^15.5.3",
"svelte-apollo": "^0.4.0"
}
} |
@vladimirdrayling Try adding |
Btw the error originally reported has been fixed in |
This is not entirely related to the Credit goes to Reddit user: Here's how I got it working for Apollo: // apolloClient.js
import Apollo, * as ApolloScope from '@apollo/client/core/core.cjs.js';
const HttpLink = Apollo?.HttpLink || ApolloScope?.HttpLink;
const ApolloClient = Apollo?.ApolloClient || ApolloScope?.ApolloClient;
const InMemoryCache = Apollo?.InMemoryCache || ApolloScope?.InMemoryCache;
const link = new HttpLink({
uri: `YOUR_GRAPHQL_ENDPOINT`
});
const cache = new InMemoryCache();
const apolloClient = new ApolloClient({
link,
cache
});
export default apolloClient; From here on you may use the client as you will. For instance, I would like to wrap it around my app so I'm calling it at // __layout.svelte
<script context="module">
import apolloClient from '$lib/apolloClient';
/**
* @type {import('@sveltejs/kit').Load}
*/
export async function load({ stuff }) {
const client = apolloClient;
return {
stuff: {
...stuff,
client
}
};
}
</script>
<slot /> You don't need the Lastly, you may query data in components/pages like so: // someDataComponent.js
<script context="module">
export async function load({ stuf }) {
return {
props: {
someData: await stuff.client.query({ query: SOME_DATA_QUERY })
}
};
}
</script>
<script>
import { SOME_DATA_QUERY } from '$lib/data-queries';
export let someData
</script>
<section>
<h1>{someData.heading}</h1>
<p>{someData.paragraph}</p>
</section> That's it! Hope this helps! |
Possible fix? timhall/svelte-apollo#97
Another workaround not mentioned here is to add export map to @apollo/client package directly: For me it was
Make sure to delete all previous "solutions" and workaround as they would conflict. Track progress on issue here: apollographql/apollo-client#8218 UPDATE:
if you don't have |
@tmrp Hmm, did u exclude anything? I tried your answer but I'm still getting "Could not resolve 'react' (mark it as external to exclude it from the bundle)" with following:
update: |
@tobiasgranlof updates to Svelte (presumably Vite) have prevented the method I mentioned from working. I started a new SvelteKit project and got a similar error. It took me a while to rack my brain around it, but I got it working now. To get it to work now you have to slightly restructure import { HttpLink, InMemoryCache, ApolloClient } from '@apollo/client/core';
import { environmentVariables } from './environment-variables';
const link = new HttpLink({
uri: environmentVariables.starWarsApi
});
const cache = new InMemoryCache();
const apolloClient = new ApolloClient({
link,
cache
});
export default apolloClient; And you would have to add the following to your kit: {
...
vite: {
ssr: {
noExternal: ['@apollo/client']
}
}
} I made a working demo project you may checkout here I hope this helps! |
@tobiasgranlof, I think you made a mistake while trying to type check the environment variable here: const link = new HttpLink({
// you would want to give uri a string type here and not transform it to a string (because it's already a string)
uri: environmentVariables.graphqlApi.toString()
// replace the above with
uri: environmentVariables.graphqlApi as string
}); To be safe, I changed my import adapter from '@sveltejs/adapter-auto';
import preprocess from 'svelte-preprocess';
/** @type {import('@sveltejs/kit').Config} */
const config = {
// Consult https://github.com/sveltejs/svelte-preprocess
// for more information about preprocessors
preprocess: preprocess(),
kit: {
adapter: adapter(),
// hydrate the <div id="svelte"> element in src/app.html
target: '#svelte',
vite: {
optimizeDeps: {
exclude: ['@apollo/client']
},
ssr: {
noExternal: ['@apollo/client']
}
}
}
};
export default config;
I made a TypeScript version, and you may check out the repo here here I hope this helps! Cheers! |
Yea, that worked. Not a fan of this setup but if it works, it works 😊 The only thing that is unclear, even tho we pass a cache to the apollo client is that it cant be reached server side.
update: |
it looks like the library was updated 10 days ago (and there is a new release), it now includes an example with sveltekit.
in case anyone else has seen and experienced this. |
@happysalada I got that as well (also reported here: #119) Adding this to my
under |
ref: #97 (comment) @tmrp Thanks, it worked! |
In our __layout.svelte
Produces [HMR][Svelte] Unrecoverable error in <__layout>: next update will trigger a full reload
logError @ proxy.js:15
Proxy<__layout> @ proxy.js:372
create_fragment @ root.svelte? [sm]:36
init @ index.mjs?v=e94d9004:1500
Root @ root.svelte? [sm]:16
createProxiedComponent @ svelte-hooks.js:245
ProxyComponent @ proxy.js:241
Proxy @ proxy.js:341
_init @ start.js:646
start @ start.js:530
async function (async)
start @ start.js:483
start @ start.js:1096
(anonymous) @ (index):39
Removing setClient(client) fixes error, breaks apollo (obviously). Any thoughts?
The text was updated successfully, but these errors were encountered: