/**
|
* GraphQL客户端工具
|
*/
|
|
import { getToken } from './auth'
|
|
// GraphQL端点
|
const GRAPHQL_ENDPOINT = '/api/graphql'
|
|
export interface GraphQLResponse<T = any> {
|
data?: T
|
errors?: Array<{
|
message: string
|
locations?: Array<{
|
line: number
|
column: number
|
}>
|
path?: string[]
|
}>
|
}
|
|
/**
|
* 发送GraphQL请求
|
*/
|
export async function graphqlRequest<T = any>(
|
query: string,
|
variables?: Record<string, any>
|
): Promise<T> {
|
const token = getToken()
|
|
const headers: Record<string, string> = {
|
'Content-Type': 'application/json',
|
}
|
|
// 如果有token,添加到请求头
|
if (token) {
|
headers['Authorization'] = `Bearer ${token}`
|
}
|
|
const response = await fetch(GRAPHQL_ENDPOINT, {
|
method: 'POST',
|
headers,
|
body: JSON.stringify({
|
query,
|
variables
|
})
|
})
|
|
if (!response.ok) {
|
throw new Error(`HTTP error! status: ${response.status}`)
|
}
|
|
const result: GraphQLResponse<T> = await response.json()
|
|
if (result.errors && result.errors.length > 0) {
|
throw new Error(result.errors[0].message)
|
}
|
|
if (!result.data) {
|
throw new Error('No data returned from GraphQL')
|
}
|
|
return result.data
|
}
|
|
/**
|
* 登录API
|
*/
|
export interface LoginRequest {
|
phone: string
|
password: string
|
}
|
|
export interface LoginResponse {
|
webLogin: {
|
token: string
|
userInfo: {
|
userId: string
|
name: string
|
phone: string
|
userType: string
|
employee?: {
|
id: string
|
name: string
|
roleId: string
|
description?: string
|
}
|
judge?: {
|
id: string
|
name: string
|
title?: string
|
company?: string
|
description?: string
|
}
|
player?: {
|
id: string
|
name: string
|
phone?: string
|
description?: string
|
auditState?: number
|
}
|
}
|
}
|
}
|
|
export async function loginApi(request: LoginRequest): Promise<LoginResponse['webLogin']> {
|
const query = `
|
mutation WebLogin($input: LoginRequest!) {
|
webLogin(input: $input) {
|
token
|
userInfo {
|
userId
|
name
|
phone
|
userType
|
employee {
|
id
|
name
|
roleId
|
description
|
}
|
judge {
|
id
|
name
|
title
|
company
|
description
|
}
|
player {
|
id
|
name
|
phone
|
description
|
auditState
|
}
|
}
|
}
|
}
|
`
|
|
const response = await graphqlRequest<LoginResponse>(query, { input: request })
|
return response.webLogin
|
}
|