Skip to content

feat(lit-query): add render method to query controllers#10791

Open
EskiMojo14 wants to merge 10 commits into
TanStack:mainfrom
EskiMojo14:query-render
Open

feat(lit-query): add render method to query controllers#10791
EskiMojo14 wants to merge 10 commits into
TanStack:mainfrom
EskiMojo14:query-render

Conversation

@EskiMojo14

@EskiMojo14 EskiMojo14 commented May 25, 2026

Copy link
Copy Markdown

🎯 Changes

see #10711 - adds a render method based on the query status, based on the Tasks API

  render() {
    return this.repo.render({
      pending: () => html`Loading...`
      error: (query) => html`Error: ${query.error.message}`
      success: (query) => html`
        <h1>${query.data.name}</h1>
        <p>${query.data.description}</p>
        <strong>${query.data.stargazers_count} stars</strong>
      `
    })
  }

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm run test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • New Features
    • Added a typed render(...) method to query, mutation, and infinite-query controllers for rendering based on pending/error/success states.
  • Documentation
    • Updated the Lit Query guides with new “Rendering” sections and examples for queries, mutations, and infinite queries.
  • Exports
    • Exposed a shared renderResult helper to support consistent, type-safe result rendering.
  • Tests
    • Added coverage for render(...) selection across states, including fallback behavior when no renderer is provided.
  • Chores
    • Added a minor release changeset entry.

@coderabbitai

coderabbitai Bot commented May 25, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Adds a typed rendering utility and a render convenience method to query, mutation, and infinite query controllers; delegates rendering through renderResult; updates tests and documentation; and adds a minor-release changeset.

Changes

Render Method Feature

Layer / File(s) Summary
Core rendering infrastructure and types
packages/lit-query/src/render.ts, packages/lit-query/src/tests/render.test.ts
Adds status-keyed renderer types, inferred return types, and renderResult, with tests for narrowing and unmatched statuses.
Query controller render method
packages/lit-query/src/createQueryController.ts, packages/lit-query/src/index.ts, packages/lit-query/src/tests/query-controller.test.ts
Adds the typed query accessor method, delegates to renderResult, re-exports it, and tests query state transitions.
Mutation controller render method
packages/lit-query/src/createMutationController.ts, packages/lit-query/src/tests/mutation-controller.test.ts
Adds mutation rendering and tests idle, success, error, pending, and missing-renderer states.
Infinite query controller render method
packages/lit-query/src/createInfiniteQueryController.ts, packages/lit-query/src/tests/infinite-and-options.test.ts
Adds infinite-query rendering and tests page data, error, and missing-renderer states.
Documentation and release notes
docs/framework/lit/guides/*.md, .changeset/crisp-sloths-sneeze.md
Documents Task API-style rendering examples and declares a minor package release.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant LitTemplate
  participant QueryController
  participant renderResult
  participant StatusRenderer
  LitTemplate->>QueryController: call render(renderers)
  QueryController->>renderResult: pass controller.current and renderers
  renderResult->>StatusRenderer: invoke renderer matching result.status
  StatusRenderer-->>LitTemplate: return rendered output or undefined
Loading

Suggested reviewers: TkDodo

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 57.14% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: adding a render method to lit-query controllers.
Description check ✅ Passed The description matches the template and includes changes, checklist items, and release impact with a changeset.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
docs/framework/lit/guides/mutations.md (1)

172-185: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Import nothing in the example snippet.

This example uses nothing (Line 179) but the imports shown in this guide only include html/LitElement, so copy-pasting the snippet can fail.

Proposed doc fix
-import { LitElement, html } from 'lit'
+import { LitElement, html, nothing } from 'lit'
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/framework/lit/guides/mutations.md` around lines 172 - 185, The example
uses the special sentinel nothing in the render() example but the shown imports
only include html/LitElement; update the snippet imports to also import nothing
(e.g., from 'lit') so copy-pasting works and the pending template can return
nothing; ensure the updated example shows nothing alongside html/LitElement
imports and keep addTodo.render(...) and the pending/error/success handlers
unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/lit-query/src/createMutationController.ts`:
- Line 72: Update the JSDoc in createMutationController.ts: replace the phrase
"query result" with "mutation result" in the comment for the accessor inside
createMutationController (the JSDoc that begins "/** Renders the query result
... */") so the description correctly refers to mutation results.

In `@packages/lit-query/src/createQueryController.ts`:
- Line 19: The import in createQueryController.ts incorrectly treats
RendererResult and ResultRenderers as runtime values; change the import to use
type-only qualifiers for RendererResult and ResultRenderers (e.g., import type {
RendererResult, ResultRenderers } from './render.js') while keeping renderResult
as a normal import, and apply the same type-only import change in
createMutationController.ts and createInfiniteQueryController.ts so all three
modules import those two symbols as types only.

---

Outside diff comments:
In `@docs/framework/lit/guides/mutations.md`:
- Around line 172-185: The example uses the special sentinel nothing in the
render() example but the shown imports only include html/LitElement; update the
snippet imports to also import nothing (e.g., from 'lit') so copy-pasting works
and the pending template can return nothing; ensure the updated example shows
nothing alongside html/LitElement imports and keep addTodo.render(...) and the
pending/error/success handlers unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: bf4f14c7-08aa-4d27-a0e7-1ce5aa4b29b2

📥 Commits

Reviewing files that changed from the base of the PR and between 514c132 and 4d2c97e.

📒 Files selected for processing (13)
  • .changeset/crisp-sloths-sneeze.md
  • docs/framework/lit/guides/infinite-queries.md
  • docs/framework/lit/guides/mutations.md
  • docs/framework/lit/guides/queries.md
  • packages/lit-query/src/createInfiniteQueryController.ts
  • packages/lit-query/src/createMutationController.ts
  • packages/lit-query/src/createQueryController.ts
  • packages/lit-query/src/index.ts
  • packages/lit-query/src/render.ts
  • packages/lit-query/src/tests/infinite-and-options.test.ts
  • packages/lit-query/src/tests/mutation-controller.test.ts
  • packages/lit-query/src/tests/query-controller.test.ts
  • packages/lit-query/src/tests/render.test.ts

Comment thread packages/lit-query/src/createMutationController.ts Outdated
Comment thread packages/lit-query/src/createQueryController.ts Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/lit-query/src/tests/infinite-and-options.test.ts`:
- Around line 510-551: Wrap the bodies of the infinite.render tests, including
the additionally referenced tests, in try/finally blocks so controller cleanup
always runs. Keep setup and assertions in the try block, and move each
infinite.destroy() call into the finally block; use the test’s existing infinite
controller variable and preserve all current assertions.

In `@packages/lit-query/src/tests/query-controller.test.ts`:
- Around line 1516-1555: Wrap the bodies of all three render tests, including
“renders by current query status via query.render” and the two following tests,
in try/finally blocks. Keep setup and assertions in the try block, and move each
query.destroy() call into its corresponding finally block to guarantee
controller cleanup when assertions fail.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: a79e1635-c1b1-40e4-b419-7cd4d017be7d

📥 Commits

Reviewing files that changed from the base of the PR and between 65cc829 and 9c816fd.

📒 Files selected for processing (13)
  • .changeset/crisp-sloths-sneeze.md
  • docs/framework/lit/guides/infinite-queries.md
  • docs/framework/lit/guides/mutations.md
  • docs/framework/lit/guides/queries.md
  • packages/lit-query/src/createInfiniteQueryController.ts
  • packages/lit-query/src/createMutationController.ts
  • packages/lit-query/src/createQueryController.ts
  • packages/lit-query/src/index.ts
  • packages/lit-query/src/render.ts
  • packages/lit-query/src/tests/infinite-and-options.test.ts
  • packages/lit-query/src/tests/mutation-controller.test.ts
  • packages/lit-query/src/tests/query-controller.test.ts
  • packages/lit-query/src/tests/render.test.ts
✅ Files skipped from review due to trivial changes (4)
  • docs/framework/lit/guides/mutations.md
  • .changeset/crisp-sloths-sneeze.md
  • packages/lit-query/src/tests/mutation-controller.test.ts
  • docs/framework/lit/guides/queries.md
🚧 Files skipped from review as they are similar to previous changes (7)
  • packages/lit-query/src/tests/render.test.ts
  • packages/lit-query/src/index.ts
  • packages/lit-query/src/createQueryController.ts
  • docs/framework/lit/guides/infinite-queries.md
  • packages/lit-query/src/createInfiniteQueryController.ts
  • packages/lit-query/src/createMutationController.ts
  • packages/lit-query/src/render.ts

Comment thread packages/lit-query/src/tests/infinite-and-options.test.ts
Comment on lines +1516 to +1555
it('renders by current query status via query.render', async () => {
const client = new QueryClient({
defaultOptions: {
queries: {
retry: false,
},
},
})

const host = new TestControllerHost()

const query = createQueryController(
host,
{
queryKey: ['query-controller', 'render-01'],
queryFn: async () => 'ok',
},
client,
)

const pendingUi = query.render({
pending: () => 'pending-ui',
success: () => 'success-ui',
error: () => 'error-ui',
})
expect(pendingUi).toBe('pending-ui')

host.connect()
host.update()
await waitFor(() => query().isSuccess)

const successUi = query.render({
pending: () => 'pending-ui',
success: (result) => `success-${result.data}`,
error: () => 'error-ui',
})
expect(successUi).toBe('success-ok')

query.destroy()
})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Use try/finally for controller cleanup to match existing test patterns.

The new render tests call query.destroy() directly at the end, but if an expect throws, the controller won't be cleaned up. Existing tests in this file (e.g., lines 189–208, 237–263, 288–309) wrap the body in try/finally to guarantee cleanup. Adopt the same pattern for consistency and to prevent potential test pollution from lingering observers.

♻️ Proposed fix for the first test (apply similarly to the other two)
     const query = createQueryController(
       host,
       {
         queryKey: ['query-controller', 'render-01'],
         queryFn: async () => 'ok',
       },
       client,
     )

-    const pendingUi = query.render({
-      pending: () => 'pending-ui',
-      success: () => 'success-ui',
-      error: () => 'error-ui',
-    })
-    expect(pendingUi).toBe('pending-ui')
-
-    host.connect()
-    host.update()
-    await waitFor(() => query().isSuccess)
-
-    const successUi = query.render({
-      pending: () => 'pending-ui',
-      success: (result) => `success-${result.data}`,
-      error: () => 'error-ui',
-    })
-    expect(successUi).toBe('success-ok')
-
-    query.destroy()
+    try {
+      const pendingUi = query.render({
+        pending: () => 'pending-ui',
+        success: () => 'success-ui',
+        error: () => 'error-ui',
+      })
+      expect(pendingUi).toBe('pending-ui')
+
+      host.connect()
+      host.update()
+      await waitFor(() => query().isSuccess)
+
+      const successUi = query.render({
+        pending: () => 'pending-ui',
+        success: (result) => `success-${result.data}`,
+        error: () => 'error-ui',
+      })
+      expect(successUi).toBe('success-ok')
+    } finally {
+      query.destroy()
+    }

Also applies to: 1557-1591, 1593-1631

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/lit-query/src/tests/query-controller.test.ts` around lines 1516 -
1555, Wrap the bodies of all three render tests, including “renders by current
query status via query.render” and the two following tests, in try/finally
blocks. Keep setup and assertions in the try block, and move each
query.destroy() call into its corresponding finally block to guarantee
controller cleanup when assertions fail.

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

Successfully merging this pull request may close these issues.

1 participant