-
-
Notifications
You must be signed in to change notification settings - Fork 106
session refresh queries #1723
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
Merged
Merged
session refresh queries #1723
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
95ecb19
change is page.data will trigger all queries refresh fixes #1225
siddarthvader 7c11e4e
Merge branch 'main' into 707-session-refresh-queries
siddarthvader e6fc679
changeset added
siddarthvader eabd516
now subscription.all returns unique list of selection
siddarthvader 73cc67f
e2e tests
siddarthvader File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "houdini-svelte": patch | ||
| "houdini-core": patch | ||
| "houdini": patch | ||
| --- | ||
|
|
||
| Refresh active SvelteKit queries after `setSession` updates the Houdini session, ensuring refetched operations run with the latest session. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <script lang="ts"> | ||
| import { invalidateAll } from '$app/navigation' | ||
| import { CachePolicy, graphql } from '$houdini' | ||
| import { onMount } from 'svelte' | ||
|
|
||
| // This query deliberately lives outside a SvelteKit load function. invalidateAll() | ||
| // cannot rerun it directly; a second request must come from Houdini's session watcher. | ||
| const session = graphql(` | ||
| query ActiveSessionAfterInvalidate { | ||
| session | ||
| } | ||
| `) | ||
|
|
||
| onMount(() => { | ||
| session.fetch({ policy: CachePolicy.NetworkOnly }) | ||
| }) | ||
|
|
||
| async function updateSession() { | ||
| await fetch('/stores/session-refresh/update', { method: 'POST' }) | ||
| await invalidateAll() | ||
| } | ||
| </script> | ||
|
|
||
| <h1>Session Refresh</h1> | ||
|
|
||
| <div id="result"> | ||
| {$session.data?.session} | ||
| </div> | ||
|
|
||
| <button id="update-session" on:click={updateSession}>Update session</button> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { expect, test, type Request } from '@playwright/test' | ||
|
|
||
| import { routes } from '../../../lib/utils/routes.js' | ||
|
|
||
| const initialToken = '1234-Houdini-Token-5678' | ||
| const updatedToken = 'updated-Houdini-Token-0000' | ||
|
|
||
| function isActiveSessionQuery(request: Request) { | ||
| return ( | ||
| request.url().endsWith(routes.GraphQL) && | ||
| (request.postData()?.includes('query ActiveSessionAfterInvalidate') ?? false) | ||
| ) | ||
| } | ||
|
|
||
| test.describe('SvelteKit session refresh', () => { | ||
| test('refreshes active queries with the new Houdini session', async ({ page }) => { | ||
| const initialRequestPromise = page.waitForRequest(isActiveSessionQuery) | ||
| await page.goto(routes.Stores_Session_Refresh) | ||
|
|
||
| const initialRequest = await initialRequestPromise | ||
| expect(initialRequest.headers().authorization).toBe(`Bearer ${initialToken}`) | ||
| await expect(page.locator('#result')).toHaveText(initialToken) | ||
|
|
||
| const [refetchRequest] = await Promise.all([ | ||
| page.waitForRequest(isActiveSessionQuery), | ||
| page.waitForResponse((response) => response.url().endsWith('session-refresh/update')), | ||
| page.getByRole('button', { name: 'Update session' }).click(), | ||
| ]) | ||
|
|
||
| expect(refetchRequest.headers().authorization).toBe(`Bearer ${updatedToken}`) | ||
| await expect(page.locator('#result')).toHaveText(updatedToken) | ||
| }) | ||
| }) |
15 changes: 15 additions & 0 deletions
15
e2e/kit/src/routes/stores/session-refresh/update/+server.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { setSession } from '$houdini' | ||
| import { json, type RequestHandler } from '@sveltejs/kit' | ||
|
|
||
| const updatedToken = 'updated-Houdini-Token-0000' | ||
|
|
||
| export const POST: RequestHandler = (event) => { | ||
| event.cookies.set('houdini-session-token', updatedToken, { | ||
| path: '/', | ||
| httpOnly: true, | ||
| sameSite: 'lax', | ||
| }) | ||
| setSession(event, { user: { token: updatedToken } }) | ||
|
|
||
| return json({ token: updatedToken }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { Cache } from '../../../../houdini/src/runtime/cache/index.js' | ||
| import { createPluginHooks, DocumentStore, HoudiniClient } from 'houdini/runtime/client' | ||
| import { ArtifactKind, DataSource } from 'houdini/runtime/types' | ||
| import { beforeEach, expect, test, vi } from 'vitest' | ||
|
|
||
| import { testConfigFile } from '../../../../houdini/src/test/index.js' | ||
| import { setMockConfig } from '../../config.js' | ||
| import { query } from '../../plugins/query.js' | ||
|
|
||
| const config = testConfigFile() | ||
| beforeEach(async () => { | ||
| setMockConfig(config) | ||
| }) | ||
|
|
||
| test('refreshAll with a new session refetches active queries with that session', async () => { | ||
| const cache = new Cache() | ||
|
|
||
| const selection = { | ||
| fields: { | ||
| viewer: { | ||
| type: 'User', | ||
| visible: true, | ||
| keyRaw: 'viewer', | ||
| selection: { | ||
| fields: { | ||
| id: { type: 'ID', visible: true, keyRaw: 'id' }, | ||
| firstName: { type: 'String', visible: true, keyRaw: 'firstName' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| cache.write({ | ||
| selection, | ||
| data: { viewer: { id: '1', firstName: 'bob' } }, | ||
| }) | ||
|
|
||
| const fetchSpy = vi.fn() | ||
| const fakeFetch = () => ({ | ||
| network(ctx, { resolve }) { | ||
| fetchSpy(ctx) | ||
| resolve(ctx, { | ||
| data: { viewer: { id: '1', firstName: 'bob', __typename: 'User' } }, | ||
| errors: null, | ||
| fetching: false, | ||
| variables: null, | ||
| source: DataSource.Network, | ||
| partial: false, | ||
| stale: false, | ||
| }) | ||
| }, | ||
| }) | ||
|
|
||
| const artifact = { | ||
| kind: ArtifactKind.Query, | ||
| hash: '7777', | ||
| raw: 'RAW_TEXT', | ||
| name: 'TestArtifact', | ||
| rootType: 'Query', | ||
| pluginData: {}, | ||
| stripVariables: [], | ||
| selection, | ||
| } | ||
| const client = new HoudiniClient({ | ||
| config: () => config, | ||
| plugins: [query(cache), fakeFetch], | ||
| }) | ||
| const store = new DocumentStore({ | ||
| client, | ||
| plugins: createPluginHooks([query(cache), fakeFetch]), | ||
| artifact, | ||
| config, | ||
| }) | ||
|
|
||
| await store.send({ session: { token: 'old' }, variables: {} }) | ||
| fetchSpy.mockClear() | ||
|
|
||
| cache.refreshAll({ token: 'new' }) | ||
|
|
||
| await new Promise((r) => setTimeout(r, 0)) | ||
|
|
||
| expect(fetchSpy).toHaveBeenCalledOnce() | ||
| expect(fetchSpy.mock.calls[0][0].session).toEqual({ token: 'new' }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.