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

Add support to skip queries #53

Closed
marton987 opened this issue Aug 18, 2020 · 6 comments
Closed

Add support to skip queries #53

marton987 opened this issue Aug 18, 2020 · 6 comments

Comments

@marton987
Copy link

Hi, can we have a parameter to skip a query depending on some conditionals to avoid undefined fetch?

Something similar to:

query(client, {
  query: USER_PROFILE,
  variables: { userId },
  skip: !userId
})
@timhall
Copy link
Owner

timhall commented Nov 18, 2020

I'm not sure this is still relevant. There might be a case for lazy queries, but will investigate in the future.

@timhall timhall closed this as completed Nov 18, 2020
@mohesem
Copy link

mohesem commented Jan 24, 2021

any update on lazy queries? TBH without lazy queries, it is completely unusable.

@t-lock
Copy link

t-lock commented Nov 17, 2022

Another use case for lazy queries:

Apollo Client depends on a jwt token which is passed in headers so gql server can do row-level authorization.
Said JWT token is provided by a cloud provider like AWS Cognito.
User refreshes the Svelte SPA, so the component immediately goes into initialization, but we're still waiting on authentication from AWS, query fails. (and no, we can't just read it directly from localStorage, because this refresh may be happening a day later when a user revisits a stale tab or something).

A lazy query lets you not only trigger a query based on a user action, but would let you react to the presence of another store like a $token or $user object which has it's own lifecycle. I'd go as far as to say it is equally as important as a query which runs during initialization, if not more.

The library feels unfinished without a way to "updateClient" and no way to trigger a query/mutation whenever you want. Perhaps that's why we're not at a 1.0 release on npm.

Is this library still under development?

@t-lock
Copy link

t-lock commented Nov 17, 2022

Actually, for anyone looking for lazy queries, you do not need an API exposed for this from svelte-apollo, though it would be super nice, you can just implement directly with @apollo/client library:

  import { getClient } from "svelte-apollo";
  import { gql } from "@apollo/client";
  
  const client = getClient();
  const query = gql`
     ...
  `;
  const variables = { ... }
  async function manualQuery() {
    const response = await client.query({
      query,
      variables,
    });
    console.log(response);
  }

@t-lock
Copy link

t-lock commented Nov 17, 2022

If I end up using this in the coming weeks and bulletproofing it, ill put up a PR to implement lazy queries.

Edit: Will not be adding a PR, since I have decided to move to URQL for my needs, see next comment. But it should be easy to implement, see previous comment.

@t-lock
Copy link

t-lock commented Nov 17, 2022

The exact API requested by OP @marton987 is implemented in URQL (an alternative to Apollo that has official Svelte bindings), so might be worth a look there for anyone finding this closed thread in the future: https://formidable.com/open-source/urql/docs/basics/svelte/#queries

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

4 participants