timhall / svelte-apollo

Svelte integration for Apollo GraphQL
MIT License
946 stars 67 forks source link

Is react a required dependency? #82

Open disbelief opened 3 years ago

disbelief commented 3 years ago

Funny question, but I'm trying to use svelte-apollo in a new svelte project, but when I try to instantiate the ApolloClient, I'm getting errors that parts of @apollo/client are referencing an unknown module react.

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

  const client = new ApolloClient({
    uri: import.meta.env.SNOWPACK_PUBLIC_GRAPHQL_URI,
    cache: new InMemoryCache(),
  });
  setClient(client);
</script>

//...

build output:

[snowpack] ! installing dependencies...
[snowpack] node_modules/@apollo/client/react/hooks/useMutation.js
   Module "react" could not be resolved by Snowpack (Is it installed?).
[snowpack] node_modules/@apollo/client/react/context/ApolloProvider.js
   Module "react" could not be resolved by Snowpack (Is it installed?).
[snowpack] node_modules/@apollo/client/react/hooks/useReactiveVar.js
   Module "react" could not be resolved by Snowpack (Is it installed?).
[snowpack] node_modules/@apollo/client/react/context/ApolloConsumer.js
   Module "react" could not be resolved by Snowpack (Is it installed?).
[snowpack] node_modules/@apollo/client/react/hooks/useSubscription.js
   Module "react" could not be resolved by Snowpack (Is it installed?).
[snowpack] node_modules/@apollo/client/react/context/ApolloContext.js
   Module "react" could not be resolved by Snowpack (Is it installed?).
[snowpack] node_modules/@apollo/client/react/hooks/useApolloClient.js
   Module "react" could not be resolved by Snowpack (Is it installed?).
[snowpack] node_modules/@apollo/client/react/hooks/utils/useBaseQuery.js
   Module "react" could not be resolved by Snowpack (Is it installed?).
[snowpack] node_modules/@apollo/client/react/hooks/utils/useDeepMemo.js
   Module "react" could not be resolved by Snowpack (Is it installed?).

Seems strange and slightly dirty to have to add react as a dep for my project. Also a little odd because @apollo/client specifies react is an optional peer dependency.

disbelief commented 3 years ago

I guess this is more of an @apollo/client issue, but certainly relevant to this repo since a large percentage of svelte projects wouldn't have react installed.

There's an open issue about this react peer dependency here: https://github.com/apollographql/apollo-client/issues/7318

A workaround posted there is to import the ApolloClient and related modules from @apollo/client/core instead of @apollo/client

TL;DR this fixed it for me:

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

  const client = new ApolloClient({
    uri: import.meta.env.SNOWPACK_PUBLIC_GRAPHQL_URI,
    cache: new InMemoryCache(),
  });
  setClient(client);
</script>
//...