亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

GraphQL 查詢在 React 中未成功獲取

GraphQL 查詢在 React 中未成功獲取

慕神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當我運行查詢時出現錯誤。我嘗試調用一個函數,我在后端解析器中定義了該函數,稱為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  }
查看完整描述

1 回答

?
慕村9548890

TA貢獻1884條經驗 獲得超4個贊

您的查詢字段中不存在用戶。在 Playground 中,您調用 userWithTasks 而不是 user。當您在 gql 中發出請求時,您必須首先使用鍵入的參數“復制”字段,然后使用參數值“復制”字段。


  export const GET_DASHBOARD_USER = gql`

        query userWithTasks($id: String!){

        userWithTasks(id: $id){

        id 

        username

        biography

        goal

        tasks {

         id

         description

         completedAt

         expirationDate

        }

        }

      }`


查看完整回答
反對 回復 2023-08-10
  • 1 回答
  • 0 關注
  • 107 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號