Trending

World technology news

→
Fetching an email query and its details in one JMAP request
Software Engineering · 2019
Software Engineering2019

Fetching an email query and its details in one JMAP request

A walkthrough of the 2019 JMAP standards uses two dependent methods in one request, with a local Node.js run that demonstrates result references, missing records and two levels of failure.

Archive retrospective · Topic year 2019

Developer's JournalPublished: Dec 6, 20266 min read
Reading progress0%
In this article
  1. Two 2019 RFCs and one inbox view
  2. Discover capabilities and limits from the session
  3. Pass message IDs within a single HTTP request
  4. Read the HTTP status and each method result
  5. The result observed on Windows
  6. Keep the display ordered as the mailbox changes

Two 2019 RFCs and one inbox view

An operations worker opening the shift mailbox needs the subjects and arrival times of recent messages. The client first chooses the appropriate message IDs, then fetches the fields for those IDs. If the client waits for a search response before building a second HTTP request, the two operations incur separate network exchanges. When the second method takes its input directly from the first response on the server, a single request can describe both steps. RFC 8620, published in July 2019, defines the JMAP envelope, method responses and references to earlier results. RFC 8621 followed in August with the email data model and mail methods. Here `Email/query` chooses the messages and `Email/get` supplies their subjects and timestamp fields. Each method keeps its own response and error semantics. All messages in the walkthrough are synthetic shift notes.

Discover capabilities and limits from the session

A real client starts with the Session resource. It reads `apiUrl`, identifies an account that offers mail access and checks advertised capabilities. The Session state helps the client recognise when it needs to fetch that resource again. The mailbox ID is distinct from the account ID. In this example `ops` identifies a mailbox inside `demo-account`. A translated mailbox label from a user interface does not automatically identify the underlying object. The `using` array names the capabilities relied on by the batch. The core limits include `maxCallsInRequest`, `maxSizeRequest` and `maxObjectsInGet`. Together they constrain the call count, the envelope size and the number of objects in the fetch call. The example uses two calls and two records. A mailbox viewer that needs many rows can read them in bounded pages and render the first useful records while further pages load.

Service architectureConceptual sketch · summary of the article's technical flow

Pass message IDs within a single HTTP request

A method invocation contains a name, an argument object and a client-selected call ID. The `q` invocation searches the `ops` mailbox, sorts by `receivedAt` in descending order and asks for two IDs. The `g` invocation uses `#ids` to select the `/ids` array in the earlier `Email/query` response. `resultOf` identifies the call, `name` selects the response name and `path` picks the value inside that response. Once the reference resolves, `Email/get` receives a conventional `ids` array. Supplying `ids` and `#ids` together is an invalid argument. This viewer requests only `id`, `subject` and `receivedAt`. Details such as the message body can load when the worker opens a row. Restricting the projection keeps the first response focused even though the client batches two methods together. The envelope below is the request used in the local run.

Illustrative diagram of two method calls within one HTTP POST.
{
  "using": [
    "urn:ietf:params:jmap:core",
    "urn:ietf:params:jmap:mail"
  ],
  "methodCalls": [
    [
      "Email/query",
      {
        "accountId": "demo-account",
        "filter": {
          "inMailbox": "ops"
        },
        "sort": [
          {
            "property": "receivedAt",
            "isAscending": false
          }
        ],
        "limit": 2
      },
      "q"
    ],
    [
      "Email/get",
      {
        "accountId": "demo-account",
        "properties": [
          "id",
          "subject",
          "receivedAt"
        ],
        "#ids": {
          "resultOf": "q",
          "name": "Email/query",
          "path": "/ids"
        }
      },
      "g"
    ]
  ]
}

Read the HTTP status and each method result

The client first checks the HTTP response and its content type. In a successful JMAP envelope it correlates each invocation in `methodResponses` with the expected call ID and response name. Some methods can emit additional responses, so the client should use those identifiers when mapping results to user interface components. In the shift mailbox, `q` can succeed while `g` reports a method error in the same response. Changing the result reference to `missing` produces `invalidResultReference` for `g` in the example. The original query still returns its IDs and the envelope retains HTTP 200. The details component can then fail gracefully and retry the relevant operation. An unsupported identifier in `using` instead affects the request envelope. The local server returns HTTP 400 and `application/problem+json` for that case. Recording request failures and method errors with separate fields helps trace which part of the application needs attention.

The result observed on Windows

A Node.js 24.19.0 process on Windows listened on 127.0.0.1 and exposed a fixed set of three synthetic messages. The successful client path sent one POST. The server put the IDs `m17` and `m12` in the `Email/query` result and the subjects `Shift handover` and `Dispatch count` in the `Email/get` result inside the same response. Further runs checked the unresolved reference, the `gone` ID in `notFound` and the unsupported capability. All twelve assertions passed. The teaching fixture implements the two example methods, one result path and a fixed account view. The run did not use a 2019 server or connect to a real mailbox. It measured HTTP request count for the successful path. Latency, concurrent load, access control and production mail storage each call for their own tests. The receipt below preserves the actual console result.

{
  "node": "v24.19.0",
  "successfulBatchHttpPosts": 1,
  "assertions": 12,
  "ids": [
    "m17",
    "m12"
  ],
  "subjects": [
    "Shift handover",
    "Dispatch count"
  ],
  "methodError": {
    "status": 200,
    "invocation": [
      "error",
      {
        "type": "invalidResultReference"
      },
      "g"
    ]
  },
  "notFound": [
    "gone"
  ],
  "requestError": {
    "status": 400,
    "type": "urn:ietf:params:jmap:error:unknownCapability"
  }
}

Keep the display ordered as the mailbox changes

Build the visible rows using the order established by the query and use message IDs to match the fetched records. A deletion or visibility change can leave a requested ID in `notFound`. A short-lived view can query the page again. A long-running view keeps scope straight by treating `queryState` as the state of a query result, with the Email state tracking the object collection. When the server reports `canCalculateChanges`, `Email/queryChanges` can help update the visible query. Re-querying the page is the fallback when the change calculation is unavailable. A batch does not freeze activity from other clients. New messages between page reads can move the boundaries of a time-ordered list. Track the query state and page boundaries together as users scroll. Testing a normal page, an empty mailbox, a disappearing message and a single method error gives the first screen predictable behaviour during both full and partial success.

269 reads ✎ Write a comment

Reader discussion

Discussion prompt

Which bottleneck would you expect as this solution scales?

No comments yet. Be the first to contribute.

Write a comment

Comments are real visitor contributions. Repeated, link-heavy or abusive submissions may be limited.