Skip to main content
Version: 4.xx.xx

2. Create a data provider with swizzle

What is swizzle?โ€‹

In some cases, refine's built-in data providers may not fully meet your API needs, and you may want to edit the existing data provider logic. If that is the case, you should use swizzle.

The swizzle is a command in refine-cli that allows you to customize refineโ€™s supported components and data providers by letting you eject selected files from the refine packages and modify them depending on your needs.

This also allows you to use the ejected file code logic as a starting point for your modifications instead of starting from scratch.

How do you use swizzle to create an data provider?โ€‹

  1. Run the swizzle command in the project directory:

    npm run refine swizzle
  2. Select the data provider package of your choice from the list. We are using @refinedev/simple-rest in this tutorial so we will choose that.

    ? Which package do you want to swizzle?
    Data Provider
    โฏ @refinedev/simple-rest
    UI Framework

    swizzle will copy the necessary files for this package to the src/rest-data-provider folder

    Successfully swizzled Data Provider

    Files created:
    - src/rest-data-provider/index.ts
    - src/rest-data-provider/utils/axios.ts
    - src/rest-data-provider/utils/generateFilter.ts
    - src/rest-data-provider/utils/generateSort.ts
    - src/rest-data-provider/utils/mapOperator.ts
    - src/rest-data-provider/utils/index.ts

    You will also need to add axios to your project dependencies.


    โ•ญ App.tsx โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
    โ”‚ โ”‚
    โ”‚ import { dataProvider } from "./rest-data-provider"; โ”‚
    โ”‚ โ”‚
    โ”‚ const App = () => { โ”‚
    โ”‚ return ( โ”‚
    โ”‚ <Refine โ”‚
    โ”‚ dataProvider={dataProvider} โ”‚
    โ”‚ /* ... */ โ”‚
    โ”‚ /> โ”‚
    โ”‚ ); โ”‚
    โ”‚ } โ”‚
    โ”‚ โ”‚
  3. To use the generated data provider, we need to import it in the App.tsx file and give it as a dataProvider prop to the Refine component.

And with that, you are now able to modify and use the ejected data provider however you want. Amount of time saved using swizzle instead of creating a data provider from scratch is quite substantial.