Skip to content

Commit

Permalink
Don't run setup on queries without load (#1068)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlecAivazis committed Apr 22, 2023
1 parent 5f3bc42 commit 7e30f68
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/twelve-ravens-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'houdini-svelte': patch
---

Fix bug with lazy component queries causing them to subscribe to the cache before fetching
1 change: 1 addition & 0 deletions e2e/kit/src/lib/utils/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const routes = {
Stores_Session: '/stores/session',
Stores_Comp_disable_auto_fetch: '/stores/comp_disable_auto_fetch',
Stores_Comp_init: '/stores/component_init',
Stores_Component_no_load_no_setup: '/stores/component_no_load_no_setup',

Stores_Partial_List: '/stores/partial/partial_List',
Stores_Partial_Off: '/stores/partial-off',
Expand Down
48 changes: 48 additions & 0 deletions e2e/kit/src/routes/stores/component_no_load_no_setup/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<script lang="ts">
import { graphql } from '$houdini';
const q1 = graphql(`
query q1 @load {
cities {
name
}
}
`);
const q2 = graphql(`
query q2 {
city(id: 1) {
name
}
}
`);
const q3 = graphql(`
query q3 {
city(id: 1) {
name
}
}
`);
</script>

<div>
<h3>query 1</h3>
<pre>
{JSON.stringify($q1.data, null, 2)}
</pre>
</div>

<div>
<h3>query 2</h3>
<pre>
{JSON.stringify($q2.data, null, 2)}
</pre>
<button id="load2" on:click={() => q2.fetch()}>Run</button>
</div>

<h3>query 3</h3>
<div id="result">
{JSON.stringify($q3.data, null, 2)}
</div>
<button on:click={() => q3.fetch()}>Run</button>
18 changes: 18 additions & 0 deletions e2e/kit/src/routes/stores/component_no_load_no_setup/spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { test } from '@playwright/test';
import { routes } from '../../../lib/utils/routes.js';
import { expect_to_be, goto } from '../../../lib/utils/testsHelper.js';
import { sleep } from '@kitql/helper';

test("Components without load shouldn't subscribe to the cache", async ({ page }) => {
await goto(page, routes.Stores_Component_no_load_no_setup);

await expect_to_be(page, 'null');

// click on the button
await page.click('#load2');

await sleep(100);

// make sure we still have null as the value
await expect_to_be(page, 'null');
});
2 changes: 0 additions & 2 deletions packages/houdini-svelte/src/plugin/artifactData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ describe('blocking', () => {
"pluginData": {
"test": {
"isManualLoad": true,
"set_blocking": true
}
},
Expand Down Expand Up @@ -221,7 +220,6 @@ describe('blocking', () => {
"pluginData": {
"test": {
"isManualLoad": true,
"set_blocking": false
}
},
Expand Down
4 changes: 2 additions & 2 deletions packages/houdini-svelte/src/plugin/artifactData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export function artifactData({
config: Config
document: Document
}): PluginArtifactData {
// put together the type information for the filter for every list
let isManualLoad = true
// only documents in svelte files require opting into a load
let isManualLoad = document.filename.endsWith('.svelte')
let set_blocking: boolean | undefined = undefined

graphql.visit(document.document, {
Expand Down
6 changes: 3 additions & 3 deletions packages/houdini-svelte/src/runtime/stores/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class BaseStore<
_Artifact extends DocumentArtifact = DocumentArtifact
> {
// the underlying data
#params: ObserveParams<_Data, _Artifact>
#params: ObserveParams<_Data, _Artifact> & { initialize?: boolean }
get artifact() {
return this.#params.artifact
}
Expand All @@ -31,7 +31,7 @@ export class BaseStore<
#store: DocumentStore<_Data, _Input>
#unsubscribe: (() => void) | null = null

constructor(params: ObserveParams<_Data, _Artifact>) {
constructor(params: ObserveParams<_Data, _Artifact> & { initialize?: boolean }) {
// we pass null here so that the store is a zombie - we will never
// send a request until the client has loaded
this.#store = new DocumentStore({
Expand Down Expand Up @@ -116,7 +116,7 @@ export class BaseStore<
})

// only initialize when told to
if (init) {
if (init && this.#params.initialize) {
return this.observer.send({
setup: true,
variables: get(this.observer).variables,
Expand Down
7 changes: 6 additions & 1 deletion packages/houdini-svelte/src/runtime/stores/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ export class QueryStore<
// except for manual queries, which should be false, it will be manualy triggered
const fetching = artifact.pluginData['houdini-svelte']?.isManualLoad !== true

super({ artifact, fetching })
super({
artifact,
fetching,
// only initialize the store if it was automatically loaded
initialize: !artifact.pluginData['houdini-svelte'].isManualLoad,
})

this.storeName = storeName
this.variables = variables
Expand Down

0 comments on commit 7e30f68

Please sign in to comment.