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

Refetch no longer working if query returns an error #68

Open
liquiad opened this issue Nov 2, 2020 · 2 comments
Open

Refetch no longer working if query returns an error #68

liquiad opened this issue Nov 2, 2020 · 2 comments

Comments

@liquiad
Copy link

liquiad commented Nov 2, 2020

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
Copy link

bradphelan commented Nov 13, 2020

I've noticed the same problem.

Maybe related apollographql/apollo-client#2513

@bradphelan
Copy link

bradphelan commented Nov 18, 2020

@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.

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

2 participants