swaggertype-providersfable-f#

Using type providers (specifically SwaggerProvider) in a Fable application


I am trying to use SwaggerProvider with Fable (is this even possible?)

//.fsproj
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <Compile Include="App.fs" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Fable.Browser.Dom" Version="2.2.0" />
    <PackageReference Include="Fable.Core" Version="3.2.3" />
    <PackageReference Include="SwaggerProvider" Version="1.0.1" />
  </ItemGroup>
</Project>
//App.fs, code from https://fsprojects.github.io/SwaggerProvider/#/#new-project
module App

open Browser.Dom
open SwaggerProvider

let [<Literal>] Schema = "https://petstore.swagger.io/v2/swagger.json"
type PetStore = OpenApiClientProvider<Schema>

// Get a reference to our button and cast the Element to an HTMLButtonElement
let myButton = document.querySelector(".my-button") :?> Browser.Types.HTMLButtonElement

// Register our listener
myButton.onclick <- fun _ ->
    let client = PetStore.Client()

    let s = 
        client.GetInventory()
        |> Async.AwaitTask
        |> Async.RunSynchronously
        |> sprintf "%O"

    myButton.innerText <- s

Visual Studio Code Intellisense works fine, which means that everything is ok at design time.

However, when I try to start the project with npm start I get the following exceptions

C:/Root/Work/Horth/Work/Yatp/Client/Fable/src/App.fs(7,17): (7,46) error FSHARP: The type provider 'SwaggerProvider.OpenApiClientTypeProvider' reported an error: Could not load file or assembly 'System.Text.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. Could not find or load a specific file. (0x80131621) (code 3033)
C:/Root/Work/Horth/Work/Yatp/Client/Fable/src/App.fs(17,27): (17,33) error FSHARP: The type 'PetStore' does not define the field, constructor or member 'Client'. (code 39)

What am I doing wrong?

If Fable doesn't support type providers (or this type provider), what is the recommended way to consume an OpenApi/Swagger rest api from a Fable application?


Solution

  • I report here the answer from SwaggerProvider maintainer.

    SwaggerProvider does not support Fable, we do not emit code compatible with browsers.

    I recommend to try another project - https://github.com/Zaid-Ajaj/Hawaii Hawaii pretty flexible and configurable CLI tool that can generate F# clients based on Swagger/OpenApi schema compatible with Fable.