Skip to content
Graffle is a work in progress. Learn more.

Abort

This example shows how to cancel requests using an AbortController signal.

ts
// Our website uses Vitepress+Twoslash. Twoslash does not discover the generated Graffle modules.
// Perhaps we can configure Twoslash to include them. Until we figure that out, we have to
// explicitly import them like this.
import './graffle/modules/global.js'
// ---cut---

import { Graffle } from 'graffle'

const abortController = new AbortController()
//    ^^^^^^^^^^^^^^^

const graffle = Graffle.create().transport({
  url: `http://localhost:3000/graphql`,
})

const resultPromise = graffle
  .transport({ raw: { signal: abortController.signal } })
  //                          ^^^^^^^^^^^^^^^
  .gql(`
    {
      pokemons {
        name
      }
    }
  `)
  .$send()

abortController.abort()
//              ^^^^^

const result = await resultPromise.catch((error: unknown) => (error as Error).message)

console.log(result)

Outputs

txt
This operation was aborted

Released under the MIT License.