@8base/api-client 中文文档教程

发布于 3年前 浏览 28 项目主页 更新于 3年前

8base SDK - Client Module

8base SDK 提供了一种初始化 API 客户端以开始对工作区进行 GraphQL 调用的便捷方式。

其他 8base 服务包使用此客户端库向 8base API 发出请求。 您也可以独立使用它来向 API 发出自定义请求。

Usage

Client 模块公开了许多可用于配置自身的不同方法。 下面列出了这些函数及其相关说明。

在最基本的情况下,客户端可用于从给定工作区查询公共资源。

/* Import client module */
import { Client } from '@8base/api-client';

/* Instantiate new instance with workspace endpoint */
const client = new Client('https://api.8base.com/cjz1n2qrk00f901jt2utcc3m0');

/* Run a query with a callback handler */
client.request(`
  query {
    __schema {
      types {
        name
      }
    }
  }
`).then(console.log);

如果需要设置 idTokenapiToken,可以使用 client.setIdToken(tk) 方法。 在幕后,这将在后续请求中将提供的值设置为 Bearer 令牌标头。

/* Set the Token */
client.setIdToken('MY_API_TOKEN')

/* Run a query with a callback handler */
client.request(`
  query {
    privateResourceList {
      items {
        id
      }
    }
  }
`).then(console.log);

Client Methods

setIdToken(token: String!)

更新 id 令牌。

setRefreshToken(token: String!)

更新刷新令牌。

setEmail(email: String!)

更新用户邮箱。

setWorkspaceId(id: String!)

更新工作区标识符。

request(query: GraphqlString!, variables: Object)

使用执行查询时将使用的变量向 API 发送请求。 . 退货承诺予以解决。

/* Set variables */
const variables = {
    search: "ste"
}

/* Set query */
const query = /* GraphQL */`
  query($search: String!) {
    resourceList(filter: {
        name: {
            contains: $search
        }
    }) {
      items {
        id
      }
    }
  }
`

/* Run a query with a callback handler */
client.request(query, variables).then(console.log);

Alternatives

开发人员可以通过多种方式连接到他们的工作区并开始执行查询。 Client 模块只是其中之一! 如果您对创建客户端的替代方法感到好奇,请观看以下视频。 但是,请记住,所有 GraphQL 调用都只是 HTTP post 请求——连接到您的 8base 工作区也不例外!

连接到API

8base SDK - Client Module

The 8base SDK provides a convient way of initializing an API client to start making GraphQL calls to a workspace.

This client library is used by the other 8base service packages to make requests to the 8base API. You can also use it independently to make custom requests to the API.

Usage

The Client module exposes a number of different methods that can be used to configure itself. Those functions are listed below with relevant descriptions.

In the most basic case, the client can be used to query public resources from a given workspace.

/* Import client module */
import { Client } from '@8base/api-client';

/* Instantiate new instance with workspace endpoint */
const client = new Client('https://api.8base.com/cjz1n2qrk00f901jt2utcc3m0');

/* Run a query with a callback handler */
client.request(`
  query {
    __schema {
      types {
        name
      }
    }
  }
`).then(console.log);

Should an idToken or apiToken need to get set, the client.setIdToken(tk) method can be used. Under the hood, this will set the supplied value as a Bearer token header on subsequent requests.

/* Set the Token */
client.setIdToken('MY_API_TOKEN')

/* Run a query with a callback handler */
client.request(`
  query {
    privateResourceList {
      items {
        id
      }
    }
  }
`).then(console.log);

Client Methods

setIdToken(token: String!)

Update the id token.

setRefreshToken(token: String!)

Update the refresh token.

setEmail(email: String!)

Update the user email.

setWorkspaceId(id: String!)

Update the workspace identifier.

request(query: GraphqlString!, variables: Object)

Send request to the API with variables that will be used when executing the query. . Returns promise to be resolved.

/* Set variables */
const variables = {
    search: "ste"
}

/* Set query */
const query = /* GraphQL */`
  query($search: String!) {
    resourceList(filter: {
        name: {
            contains: $search
        }
    }) {
      items {
        id
      }
    }
  }
`

/* Run a query with a callback handler */
client.request(query, variables).then(console.log);

Alternatives

There any a number of ways developers can connect to their workspace and begin executing queries. The Client module is only one of them! If you're curious about alternatives for how you can create a client, check out the following video. However, remember that all GraphQL calls are only HTTP post requests – and connecting to your 8base workspace is no different!

Connecting to the API

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文