S
SolidJS3w ago
Cone

Throwing a redirect inside a query

When throwing a redirect from a query, the throw gets caught and returns undefined instead. The inferred return type is wrong in this case, but when throwing other values the throw is not caught. Is the query function typing wrong, or am I using query wrong?
import { query, redirect } from "@solidjs/router"

const queryThatThrows = query(async () => {
"use server"
const condition = true
if (condition) {
throw redirect("/foo")
}
return true
}, "query-that-throws")

const otherQuery = query(async () => {
"use server"
const v = await queryThatThrows()
type V = typeof v // boolean
// this still runs because the throw gets caught
console.log(v) // undefined
}, "other-query")
import { query, redirect } from "@solidjs/router"

const queryThatThrows = query(async () => {
"use server"
const condition = true
if (condition) {
throw redirect("/foo")
}
return true
}, "query-that-throws")

const otherQuery = query(async () => {
"use server"
const v = await queryThatThrows()
type V = typeof v // boolean
// this still runs because the throw gets caught
console.log(v) // undefined
}, "other-query")
2 Replies
Brendonovich
Brendonovich3w ago
I don't think calling a query inside another query or action is supported, i'd suggest moving the server functions out and using them directly.
// server.ts
export const queryThatThrows = async () => {
"use server"
const condition = true
if (condition) {
throw redirect("/foo")
}
return true
}

export const otherQuery = async () => {
"use server"
const v = await queryThatThrows()
...
}

// queries.ts
import * as server from "./server";
import { query, redirect } from "@solidjs/router"

const queryThatThrows = query(
server.queryThatThrows,
"query-that-throws"
)

const otherQuery = query(
server.otherQuery,
"other-query"
)
// server.ts
export const queryThatThrows = async () => {
"use server"
const condition = true
if (condition) {
throw redirect("/foo")
}
return true
}

export const otherQuery = async () => {
"use server"
const v = await queryThatThrows()
...
}

// queries.ts
import * as server from "./server";
import { query, redirect } from "@solidjs/router"

const queryThatThrows = query(
server.queryThatThrows,
"query-that-throws"
)

const otherQuery = query(
server.otherQuery,
"other-query"
)
Cone
ConeOP3w ago
Thanks. I had searched for nested query both here and in the docs and I didn't find anything. If it isn't documented somewhere yet, could it be? My instinct was to nest queries because queryThatThrows is an expensive operation that is used in multiple other queries and caching it could help my application performance
Want results from more Discord servers?
Add your server