反应 CCM 图QL 客户端( WIP)
图QL - > GQL 语言
类型脚本 - > TS 翻译
图QL + TypeScript - > 图克l 编码
此软件包将Harness CCM GraphQL API 显示为已键入 @tanstack/react-query 钩子,由使用 CCM GraphQL 方案生成 @graphql-codegen/cli。 。 。 .
这是一个 伴侣到 @harnessio/react-ccm-service-client ——该套件涵盖REST端点;此套件涵盖GraphQL端点(/ccm/api/graphql) (中文(简体) ).
软件包结构
packages/ccm-graphql-client/
├── schema.graphql # CCM GraphQL schema (source of truth — update when schema changes)
├── queries/ # .gql query/mutation files (one per operation)
├── codegen.ts # graphql-codegen config
├── package.json
├── tsconfig.json
└── src/
├── index.ts # Exports CCMGraphQLClient class + all generated hooks
├── fetcher.ts # Custom fetch implementation wired to CCMGraphQLClient config
└── services/
└── index.ts # Auto-generated — DO NOT edit manually
如何生成和构建
从 schema + 查询中生成钩子 :
yarn generate
完整构建( 基因+编译) :
yarn build
src/services/index.ts是自动生成的。 永远不要手工编辑它——更新schema.graphql或者说.gql文件并重运行yarn generate。 。 。 .
初始化客户端
在使用任何钩之前, 初始化 CCMGraphQLClient 应用程序启动时一次 :
import { CCMGraphQLClient } from '@harnessio/react-ccm-graphql-client'
new CCMGraphQLClient({
urlInterceptor: (url) => window.getApiBaseUrl(`/ccm/api/${url}`),
getRequestHeaders: () => ({
'harness-account': accountId
}),
responseInterceptor: (response) => {
if (response.status === 401) {
on401Handler()
}
return response
}
})
CCMGraphQLClientCallbacks
| 外地 | 类型 | 需求 | 说明 |
|---|---|---|---|
urlInterceptor | (url: string) => string | 对 | 接收 "graphql" 并返回完整端点 URL,包括账户查询参数 |
getRequestHeaders | () => Record<string, string> | 对 | 在每个请求中添加返回页眉( 自动、 账户等) |
responseInterceptor | (response: Response) => Response | 没有 | 每次答复后都打电话——用于401次处理 |
使用钩子
产生的钩子跟随 @tanstack/react-query 样式 :
import { useFetchAllPerspectivesQuery } from '@harnessio/react-ccm-graphql-client'
const { data, isLoading, error } = useFetchAllPerspectivesQuery({
folderId: props.defaultFolderId,
pageNo: 0,
pageSize: 100
})
所有可用的钩子由包根导出.
UI平台整合计划
平台UI (apps/cacm) 已经使用 @harnessio/react-ccm-service-client。这些是添加 GraphQL 支持的步骤.
第1步——通过yalc(反应-api-客户端)构建并发布
cd packages/ccm-graphql-client
yarn install
yarn build
yalc publish
步骤2 — 添加到平台 UI cacm app
cd /path/to/platformUI
yalc add @harnessio/react-ccm-graphql-client
pnpm install
pnpm add graphql @tanstack/react-query # if not already in cacm/package.json
步骤3——同时初始化客户端 CCMServiceAPIClient
单位 apps/cacm/src/init-client.ts,将 GraphQL 客户端添加到 RIST 客户端旁边:
import { CCMServiceAPIClient } from '@harnessio/react-ccm-service-client'
import { CCMGraphQLClient } from '@harnessio/react-ccm-graphql-client'
export const useOpenApiClients = (accountId: string) => {
if (!accountId) return
new CCMServiceAPIClient({
urlInterceptor(url) {
return window.getApiBaseUrl(`/ccm/api${url}`)
},
requestInterceptor: getRequestInterceptor(accountId),
responseInterceptor: handle401Response
})
new CCMGraphQLClient({
urlInterceptor: (url) =>
window.getApiBaseUrl(`/ccm/api/${url}?accountIdentifier=${accountId}&routingId=${accountId}`),
getRequestHeaders: () => ({ 'harness-account': accountId }),
responseInterceptor: handle401Response
})
}
步骤4——在组件中使用钩子
import { useFetchAllPerspectivesQuery } from '@harnessio/react-ccm-graphql-client'
export const PerspectiveList = () => {
const { data, isLoading } = useFetchAllPerspectivesQuery({
folderId: '',
pageNo: 0,
pageSize: 20,
filters: [],
sortCriteria: { sortOrder: 'ASCENDING', sortType: 'NAME' },
searchKey: '',
viewIds: []
})
if (isLoading) return <span>Loading...</span>
return <pre>{JSON.stringify(data, null, 2)}</pre>
}
步骤5——重建后同步
修改软件包时 :
# in packages/ccm-graphql-client:
yarn build && yalc push
步骤 6 – 在发布到 npm 时去除 yalc 链接
cd /path/to/platformUI
yalc remove @harnessio/react-ccm-graphql-client
pnpm install
# add the real npm version to cacm/package.json
更新 Schema 或查询
- 替换
schema.graphql和CCM后端团队的最新消息 - 添加/编辑
.gql文件在queries/ - 运行
yarn generate重新生成src/services/index.ts - 运行
yarn build要编译 - 运行
yalc push同步到本地测试的 UI 平台
版本
版本的由计划变化和查询添加所驱动. 不手动编辑 src/services/index.ts。 。 。 .