慕神8447489
2023-08-10 15:59:09
我嘗試在 React 中使用 GraphQLuseQuery像這樣的API: const {loading, data, error} = useQuery<BaseUser, BaseUserVars>( GET_DASHBOARD_USER, {variables: {id: "1"}} )在我的 API 文件夾中,我保存了所需的接口和查詢,如下所示:export interface BaseUser { id: string username: string age: number goal: Goal tasks: [{ id: string, description: string, completedAt: string | null, expirationDate: string }]}export interface BaseUserVars { id: string}export const GET_DASHBOARD_USER = gql` query userWithTasks($id: String!){ user(id: $id){ id username biography goal tasks { id description completedAt expirationDate } } }`400 bad response當(dāng)我運(yùn)行查詢時(shí)出現(xiàn)錯(cuò)誤。我嘗試調(diào)用一個(gè)函數(shù),我在后端解析器中定義了該函數(shù),稱為userWithTask在操場(chǎng)上,我在沒有query關(guān)鍵字的情況下使用了它,效果很好:{ userWithTasks(id: "1"){ id username biography goal tasks { id description completedAt expirationDate } }}語法應(yīng)該是正確的,請(qǐng)問是什么問題?更新:這是我收到的錯(cuò)誤消息Cannot query field "user" on type "Query". Did you mean "users"?這是我的后端解析器供參考: @Query(() => User) async userWithTasks(@Args("id") id: string): Promise<User> { const user = await this.userService.getUserByIdWithTasks(id); console.log(user) return user }
1 回答

慕村9548890
TA貢獻(xiàn)1884條經(jīng)驗(yàn) 獲得超4個(gè)贊
您的查詢字段中不存在用戶。在 Playground 中,您調(diào)用 userWithTasks 而不是 user。當(dāng)您在 gql 中發(fā)出請(qǐng)求時(shí),您必須首先使用鍵入的參數(shù)“復(fù)制”字段,然后使用參數(shù)值“復(fù)制”字段。
export const GET_DASHBOARD_USER = gql`
query userWithTasks($id: String!){
userWithTasks(id: $id){
id
username
biography
goal
tasks {
id
description
completedAt
expirationDate
}
}
}`
添加回答
舉報(bào)
0/150
提交
取消