sveltejs / sveltejs/kit

Remote Functions: Keyed Refresh

Open
#14,367 7 comments 18 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

needs-decision
Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the problem

With remote functions it's possible to force a refresh of a query in a command by running the refresh action on the query eg getLikes(id).refresh() which is fine when you know the parameters of the other queries; but if you are using multiple of the same query, or a query with multiple parameters that you don't have access to then running the refresh is difficult.

Describe the proposed solution

Tanstack has a good approach where keys are defined on the query, then it's possible to invalidate all queries with a matching key eg:

const todoListQuery = useQuery({
  queryKey: ['todos'],
  queryFn: fetchTodoList,
})
const todoListQuery = useQuery({
  queryKey: ['todos', { page: 1 }],
  queryFn: fetchTodoList,
})

queryClient.invalidateQueries({ queryKey: ['todos'] })

A configuration could be added to the remote function query:

export const getToDos = query(
    z.number(),
    async(page) => {
        fetchPagedTodos(page)
    },
    {
          queryKey: (page) => ["todo", page] 
     }

)

  export const updateTodo = command(
      z.string(),
      async (id) => {
          await updateTodo(id);
          invalidateQueries([
              ["todo"],           
              ["user", "stats"]
          ])
      }
  )

Alternatives considered

No response

Importance

would make my life easier

Additional Information

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how remote function queries expose their refresh action and how commands currently trigger refreshes. Compare that behavior with the proposed queryKey and invalidateQueries examples, then determine the matching-key semantics and API shape. Done means related remote function queries can be invalidated without knowing every query parameter.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.