我嘗試在 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當我運行查詢時出現錯誤。我嘗試調用一個函數,我在后端解析器中定義了該函數,稱為userWithTask在操場上,我在沒有query關鍵字的情況下使用了它,效果很好:{ userWithTasks(id: "1"){ id username biography goal tasks { id description completedAt expirationDate } }}語法應該是正確的,請問是什么問題?更新:這是我收到的錯誤消息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 }
GraphQL 查詢在 React 中未成功獲取
慕神8447489
2023-08-10 15:59:09