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

Custom Fetch

This examples shows how to leverage the extension system to override the "exchange" hook's default fetch implementation.

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 graffle = Graffle
  .create()
  .transport({ url: `http://localhost:3000/graphql` })
  .anyware(({ exchange }) =>
    exchange({
      using: {
        fetch: async () => {
          return new Response(JSON.stringify({ data: { pokemon: [{ name: `Pokemon Mocked!` }] } }))
        },
      },
    })
  )

const data = await graffle.gql('{ pokemons { name } }').$send()

console.log(data)

Outputs

json
{
  "pokemon": [
    {
      "name": "Pokemon Mocked!"
    }
  ]
}

Released under the MIT License.