# Poll a waiting Journey on its retry hint

This page shows what a client does when a Journey is waiting: the host's
execution is busy with something the visitor cannot speed up, and there is no
screen to show yet. It uses patron registration: the patron has submitted
their details and the library is printing their card.

It assumes the package's fields are in your schema, as in [Add Journeys to
the schema you already have](add-journeys-to-your-schema.md).

## Read the waiting answer

A `journey` query, and the `answer` of any mutation payload, is a
`JourneyAnswer`: a `JourneyScreen`, a `JourneyWaiting` or a `JourneyDone`.
Select all three:

```graphql
query Where($id: ID!) {
  journey(id: $id) {
    __typename
    ... on JourneyScreen { screen { key title } elements moves { outcomes back } }
    ... on JourneyWaiting { reason retryAfterMs }
    ... on JourneyDone { result }
  }
}
```

A waiting Journey answers:

```json
{
  "data": {
    "journey": {
      "__typename": "JourneyWaiting",
      "reason": "card_printing",
      "retryAfterMs": 5000
    }
  }
}
```

`reason` is why the execution waits, as the host said it, and
`retryAfterMs` is how long, in milliseconds, the host suggests the client
waits before it asks again. Either is null where the host supplied none.

## Ask again after the hint

There is no subscription: the client learns that the Journey has moved by
asking `journey` again. Wait `retryAfterMs` milliseconds, then send the same
query, and keep doing so until the answer is a `JourneyScreen`, which the
client draws, or a `JourneyDone`, which ends the Journey. Where
`retryAfterMs` is null, the host has suggested no wait, and how long to wait
is the client's choice.

Each ask is a full read: the access check runs every time, so a visitor
whose access the host withdraws while the Journey waits is answered the
refusal on the next ask, as for any other read.

A sketch in a client's own language, here TypeScript:

```typescript
async function untilMoved(id: string): Promise<JourneyAnswer> {
  for (;;) {
    const answer = await askJourney(id);
    if (answer.__typename !== "JourneyWaiting") return answer;
    await sleep(answer.retryAfterMs ?? clientDefaultWaitMs);
  }
}
```

`askJourney`, `sleep` and `clientDefaultWaitMs` are the client's own.
Stop polling when the visitor leaves the screen that shows the wait.

## Where a wait comes from

A wait is the host's to give. A host's `present/1`, `submit/2` or `back/2`
answers what `Riddler.Journey.present/3` answers for a `{:waiting, info}`
status, and `info`'s `"reason"` and `"retry_after_ms"` are what reach the
client. A mutation can answer a wait too: a `submitJourney` whose host
starts slow work answers a payload whose `answer` is a `JourneyWaiting`, and
the client polls `journey` from there.
