packages/grafana-api-clients/README.md
@grafana/api-clients is currently in ALPHA.
This package provides reusable clients for Grafana APIs. The implementation is in ALPHA and the package is now published on npm as @grafana/api-clients.
The clients are auto-generated RTK Query API clients for Grafana's App Platform APIs.
yarn add @grafana/api-clients
This package uses subpath exports, which require moduleResolution to be set to "bundler" in your tsconfig.json:
{
"compilerOptions": {
"moduleResolution": "bundler"
}
}
If you use ts-node, add the following to your tsconfig.json:
{
"ts-node": {
"compilerOptions": {
"module": "es2020",
"moduleResolution": "Bundler"
}
}
}
Each API client is available as a separate subpath export, scoped by group and version:
import { generatedAPI } from '@grafana/api-clients/rtkq/<group>/<version>';
For example, to use the dashboard API or a hook:
import { generatedAPI, useListDashboardsQuery } from '@grafana/api-clients/rtkq/dashboard/v0alpha1';
If you're using this package in the context of the Grafana application, the middleware is already added for all API clients, so you don't need to add it manually.
If you're using this package for something else, outside of the core Grafana UI, you can add the middleware and reducers to your Redux store by importing the allMiddleware and allReducers exports.
import { allMiddleware, allReducers } from '@grafana/api-clients/rtkq';
const store = configureStore({
reducer: {
...allReducers,
},
middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(...allMiddleware),
});
grafana/grafana)This guide explains how to generate API clients for Grafana APIs using this package.
First, check if the group and the version are already present in openapi_test.go. If so, move on to the next step. If you need to add a new block, you can check for the right group and version in the backend API call that you want to replicate in the frontend.
{
Group: "dashboard.grafana.app",
Version: "v0alpha1",
}
Afterwards, you need to run the TestIntegrationOpenAPIs test with go test ./pkg/tests/apis -run TestIntegrationOpenAPIs. Note that it will fail the first time you run it. On the second run, it will generate the corresponding OpenAPI spec, which you can find in openapi_snapshots.
Note: You don't need to follow these two steps if the
groupyou're working with is already in theopenapi_test.gofile.
Run yarn generate:api-client and follow the prompts. See API Client Generator for details.
Use this flow when an existing API changes and you need to regenerate the API types, OpenAPI specs, and RTK Query endpoints.
Update the source API schema or types.
kinds/, run make gen-cue after changing the CUE definitions.pkg/apis/<group>/<version>/), update the types and then run ./hack/update-codegen.sh to regenerate zz_generated.deepcopy.go and zz_generated.openapi.go. This is also included in make gen-apps.Check that openapi_test.go includes the target Group and Version.
groups list if the API group/version is not present.EnableFeatureToggles so the API is registered during the test.Generate or update the OpenAPI snapshot:
go test ./pkg/tests/apis -run TestIntegrationOpenAPIs
This updates files in openapi_snapshots.
Regenerate the processed OpenAPI specs and RTK Query endpoints:
yarn generate-apis
This processes snapshots into packages/grafana-openapi/src/apis/*.json and regenerates endpoints.gen.ts files under packages/grafana-api-clients/src/clients/rtkq/.
Review the generated diff. A typical update includes:
pkg/tests/apis/openapi_snapshots/<group>.grafana.app-<version>.json.packages/grafana-openapi/src/apis/<group>.grafana.app-<version>.json.packages/grafana-api-clients/src/clients/rtkq/<group>/<version>/endpoints.gen.ts.You can also run yarn generate:api-client and select an existing API group/version.
The generator will offer to update the existing client by running the endpoint generation step without modifying the client config or Redux wiring.
Group, Version, and feature toggles in pkg/tests/apis/openapi_test.go.packages/grafana-openapi/src/apis/ does not change, verify that TestIntegrationOpenAPIs changed the snapshot first.endpoints.gen.ts does not change, verify that packages/grafana-api-clients/src/scripts/generate-rtk-apis.ts includes the API group/version.