timhall / svelte-apollo

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

Refetch no longer working if query returns an error #68

Open liquiad opened 3 years ago

liquiad commented 3 years ago

If a query returns an error, the store no longer updates with the newest data when using refetch. Is this intentional behavior?

Here's an example, I created a query that returns the time, and it will generate an error every odd second, just for testing purposes.

@Resolver()
export class TestResolver {
  @Query(() => String)
  async time() {
    const date = new Date();
    if (date.getSeconds() % 2 == 1) {
      return null;
    }

    return date.toString();
  }
}

Then I created my App.svelte with the following code:

<script>
  import { ApolloClient, InMemoryCache, gql, createHttpLink } from "@apollo/client";
  import { query, setClient } from "svelte-apollo";

  const client = new ApolloClient({
    link: createHttpLink({
        uri: "http://localhost:4000/graphql",
        credentials: "include",
    }),
    cache: new InMemoryCache()
  });
  setClient(client);

  const time = query(gql`
    query time {
        time
    }
  `)

  const reload = async () => {
      time.refetch();
  }
</script>

{#if $time.loading}
  Loading...
{:else if $time.error}
  Error: {$time.error.message}
{:else}
  {$time.data.time}
{/if}

<button on:click={reload}>Reload</button>

If the query returns an error, either when loading the app or when pressing refresh, refetch no longer updates the store. Once it errors, the reactive if-blocks stop working, even if the refetch request is successful.

Sorry if I'm just misunderstanding something, I'm very new to Svelte and GraphQL entirely.

bradphelan commented 3 years ago

I've noticed the same problem.

Maybe related https://github.com/apollographql/apollo-client/issues/2513

bradphelan commented 3 years ago

@liquiad The solution is simple.

https://www.apollographql.com/docs/react/data/error-handling/

when I create my query I add the options as indicated in the link above

let issueQuery = query(api.issues, {variables:{jql:""},errorPolicy:"all"} )

This tells the apollo engine to forward all errors to the application and keep the connection alive. By default apollo will treat all errors like network errors and disconnect.