timhall / svelte-apollo

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

Add support to skip queries #53

Closed marton987 closed 3 years ago

marton987 commented 4 years ago

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 commented 3 years ago

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

mohesem commented 3 years ago

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

t-lock commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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