timhall / svelte-apollo

Svelte integration for Apollo GraphQL
MIT License
947 stars 68 forks source link

Why $query sends a request to server on component mount? #80

Open mohesem opened 3 years ago

mohesem commented 3 years ago

I am trying to write a login form.

import { gql } from '@apollo/client';
const LOGIN_ADMIN = gql`
  query LoginAdmin(<STH>) {
    loginAdmin(<STH>) {
      <STH>
    }
  }
`;
const login = query(LOGIN_ADMIN, options);
const onSubmit = () => { 
   login.refetch(<STH>)
}

Until here everything is fine, and refetch query works totally as expected,

$: if ($login) {
   console.log($login.error, $login.loading, $login.data)
}

the problem is $login sends a query to the server when the component is mounting.

Am I missing something or this behavior is normal?

MrDweebus commented 3 years ago

I think this is why we'd need lazy queries; dev said in this comment that they'd investigate in the future, but I agree that missing the lazy queries makes you have to code in circles to keep from sending lots of random queries from things like search forms.

https://github.com/timhall/svelte-apollo/issues/53#issuecomment-729357088