N05
Error
Requests
N05 — An invalidates naming what the contract does not have
A write's invalidates [list, byId(id)] names the reads a success makes stale, each a get of the same group, each argument one of the write's own parameters. A name the group does not have would make nothing stale.
What the compiler said
error[N05]: `Backend.todos.create` invalidates `all`, which `Backend.todos` has no read of --> src/App.wf:9:18 | 9 | invalidates [all] | ^^^ = help: It may name `list` = docs: https://webfluent.monzeromer.dev/errors/N05
The fix
Broken
type Todo { id: String, title: String }
type NewTodo { title: String }
api Backend(base: "/api") { }
api Backend.todos {
get list() at "todos" -> [Todo]!
post create(body: NewTodo) at "todos" -> Todo!
invalidates [all]
}
page P(path: "/", title: "T", description: "D") {
resource todos = Backend.todos.list()
Heading("Todos").h1
match todos {
.loaded(list) { Text("{list.length} todos") }
else { Spinner }
}
}
Fixed
type Todo { id: String, title: String }
type NewTodo { title: String }
api Backend(base: "/api") { }
api Backend.todos {
get list() at "todos" -> [Todo]!
post create(body: NewTodo) at "todos" -> Todo!
invalidates [list]
}
page P(path: "/", title: "T", description: "D") {
resource todos = Backend.todos.list()
Heading("Todos").h1
match todos {
.loaded(list) { Text("{list.length} todos") }
else { Spinner }
}
}
Name a read the group declares, with arguments the write takes.
Each example is a whole program: it is all of src/App.wf in a project with no other .wf file (trying an example).
Why this rule exists
Error.
A write's invalidates [list, byId(id)] names the reads that a success
makes stale: every resource on the page over one of them asks again. Each
name is a get of the same group, and each argument is one of the write's
own parameters, so that byId(id) refreshes the item the write changed. A
name the group does not have would make nothing stale, and the page would
keep showing what the write changed.
A read changes nothing, so an invalidates on a get is refused too.
lints cannot lower it: it would ship a broken page.