Repository navigation
mail search always fails: $orderby is sent together with $search (Graph rejects the combination) #11
Description
mail search always fails: $orderby is sent together with $search (Graph rejects the combination)
Summary
mog mail search <query> is unusable — every non-empty search returns:
error: SearchWithOrderBy: The query parameter '$orderBy' is not supported with '$search'.
Microsoft Graph does not allow $orderby and $search in the same request (results of $search are relevance-ranked and cannot be re-sorted). mail search sets both unconditionally, so any real query fails. Only mail search * / mail list work, because they skip $search.
Environment
- mog
v0.3.2-0.20260306171447-cfcbf708a438(built from HEADcfcbf708a438, currentmain) - macOS arm64, installed via
go install(~/go/bin/mog) - Microsoft 365 / Exchange Online mailbox
Steps to reproduce
$ mog mail search "invoice" --max 5
error: SearchWithOrderBy: The query parameter '$orderBy' is not supported with '$search'.
$ mog mail list --max 5 # works — no $search sent
... (messages) ...Root cause
internal/cli/mail.go, MailSearchCmd.Run sets $orderby for every request and then conditionally adds $search:
query.Set("$orderby", "receivedDateTime desc") // line 60 — always
query.Set("$select", "id,subject,from,receivedDateTime,isRead,hasAttachments")
if c.Query != "*" && c.Query != "" {
query.Set("$search", fmt.Sprintf(`"%s"`, c.Query)) // line 64 — combination is invalid
}When $search is present, $orderby must be omitted.
Suggested fix
Only set $orderby when not searching:
query.Set("$select", "id,subject,from,receivedDateTime,isRead,hasAttachments")
if c.Query != "*" && c.Query != "" {
query.Set("$search", fmt.Sprintf(`"%s"`, c.Query))
// NOTE: $search results are relevance-ranked; $orderby is not allowed with $search.
} else {
query.Set("$orderby", "receivedDateTime desc")
}Removing $orderby from the search path is sufficient to fix mail search. Two related observations:
contacts directorymay share a related problem.internal/cli/contacts.gohas three paths:contacts listsets$orderbybut no$search(safe);contacts searchuses$filter/contains()— not$search— so it is unaffected; butcontacts directory(line ~296) issues$searchon/users. That path does not set$orderby, but Graph$searchagainst directory objects (/users) requires theConsistencyLevel: eventualheader — see below.client.Getnever sendsConsistencyLevel: eventual.requestonly setsAuthorizationandContent-Type.$searchon/me/messageshappens to work without it, but$searchon/users(used bycontacts directory) does require it, so that header should be added for search requests.
(Note: internal/graph/client_test.go TestClient_QueryParams only verifies generic $top/$orderby query-param round-tripping; it does not exercise the $search + $orderby combination, so there is currently no regression test for this case.)
Impact
mail search is completely broken; users must fall back to mail list (capped at the page size) and grep, which cannot reach older messages.
Activity