Management API client
@picked/admin-api-client is a typed GraphQL client for the Management API. It gets the
type of each result and its variables from the API schema.
import { createAdminApiClient, graphql } from '@picked/admin-api-client'
const MeQuery = graphql(`
query Me {
user {
id
email
}
}
`)
const client = createAdminApiClient({
apiUrl: 'https://api.picked.app',
apiKey: process.env.PICKED_API_KEY,
})
const data = await client.request(MeQuery, {})Client options
createAdminApiClient(options) returns a client. The client sends each request to
<apiUrl>/api/graphql.
| Option | Type | Default | Description |
|---|---|---|---|
apiUrl | string | required | The origin of the API, for example https://api.picked.app. |
token | string | (() => string | Promise<string>) | none | A bearer token. A function gives a new token for each request. |
apiKey | string | none | An API key. The client sends it in the X-API-Key header. |
fetch | typeof fetch | the global fetch | The function that sends each request. |
See Authentication for the tokens and keys that the API accepts.
Requests
client.request(document, variables, options?): Promise<Result>| Parameter | Type | Description |
|---|---|---|
document | TadaDocumentNode | A document that graphql makes. |
variables | object | The variables of the document. Use {} for a document without variables. |
options.timeoutMs | number | The time limit of the request in milliseconds. The default is 30000. |
options.headers | Record<string, string> | Headers for this request only. They replace the token and apiKey headers that have the same name. |
The client does not check the shape of the response at runtime.
Errors
When the API refuses a request with a 429 status, the client waits about 1 second and
sends the request again. It does this 3 times at most. options.timeoutMs includes these
waits.
| Case | Error |
|---|---|
| The API returns GraphQL errors | AdminApiResponseError. Its codes holds the extensions.code of each error. |
The API refuses the request with a 429 status 4 times | AdminApiRateLimitError |
| A network failure, a timeout, or an HTTP error without GraphQL errors | Error |
See Errors for each error code.
Workspace documents
The package also gives the documents that move a workspace from one project to a different project:
| Document | Operation |
|---|---|
WorkspaceExportDocument | Reads the full contents of a workspace. |
ProjectLiveVersionDocument | Reads the live version of a project. |
WorkspaceImportDocument | Makes a workspace from those contents. |
AssetUploadInitDocument | Gives a URL that receives the file of an asset. |
AssetCommitDocument | Records an asset after its file is at that URL. |
AssetDeleteDocument | Deletes an asset. |
WorkspacePublishDocument | Publishes a workspace. |
The picked workspace export and picked workspace import commands use these documents.
See CLI.
App installation documents
The package also gives the documents that an app server sends as one installation. The
installation client of @picked/app-kit
sends them:
| Document | Operation |
|---|---|
AppInstallationLabelsDocument | Reads the labels of an installation. |
LabelsSetDocument | Makes or replaces labels. |
LabelsDeleteDocument | Removes labels. |
AppInstallationTokenCreateDocument | Makes an installation token. The request sends an app assertion in the picked-app-assertion header, through options.headers. |
