Skip to main content
Version: 1.x

createAPIClient(...)

Qraft CLI generates a helper function createAPIClient(...), which creates a Qraft API client with the necessary context for React Hooks.

import { createAPIClient } from './api'

const qraft = createAPIClient({
context,
})

Arguments

  1. options?: QraftClientOptions - Optional options to be used by the Qraft API client
    • options.context?: Context<QraftContextValue> - Optional custom Context to be used by Qraft Hooks. If not provided, a default QraftContext will be used. The custom Context must be used when multiple Qraft API clients for the multiple OpenAPIs are used in the same application. In case if you are going to publish your API client as a library, it's highly recommended to use a custom Context.

Returns

  • qraft - The Qraft API client with the necessary context for Qraft Hooks. It contains all the methods to interact with the API grouped in services.

Examples

src/fetch-queries.ts
import { requestFn } from '@openapi-qraft/react';
import { createAPIClient } from './api';

import { QueryClient, dehydrate } from '@tanstack/react-query';

const queryClient = new QueryClient();

// ⬇︎ Create a Qraft API client
const qraft = createAPIClient()

const parameters = {
path: { petId: 1 },
};

// ⬇︎ Fetch the `getPetById` query with the `queryClient`
const pet = await qraft.pet.getPetById.fetchQuery(
{
parameters,
requestFn: requestFn,
baseUrl: 'https://api.sandbox.monite.com/v1',
},
queryClient
);

console.log(pet.name); // `pet` is the result of the `getPetById` request

dehydrate(queryClient); // Dehydrate the `queryClient` to be used for SSR
tip

You can call the createAPIClient(...) function multiple times, inside the Components, server side code, or create an external file to export the qraft object. All methods are valid ✔︎ as long as you provide the same options.