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

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

Vue Apollo:變量沒有添加到 graphql 查詢,或者我沒有在后端正確接受它們

Vue Apollo:變量沒有添加到 graphql 查詢,或者我沒有在后端正確接受它們

Go
幕布斯7119047 2023-06-12 14:30:44
我似乎無法將使用 apollo 進行的 graphql 查詢中的變量放入查詢主體以被后端服務器接受。我有一個簡單的 vue 前端和后端。在 vue-component 我有以下查詢:apollo: {    entry: {      query: gql`        query variables($userID: Int){          entries(userID: $userID) {            id,            value,            timeStamp          }        }      `,      variables() {        return {          userID: 2        }      }      update: data => data,    }  }}在我的后端,我為所有 POST 請求設置了處理函數// GraphQL returns an http.HandlerFunc for our /graphql endpointfunc (s *Server) GraphQL() http.HandlerFunc {    return func(w http.ResponseWriter, r *http.Request) {        //Allow CORS here By * or specific origin        w.Header().Set("Access-Control-Allow-Origin", "*")        w.Header().Set("Access-Control-Allow-Headers", "Content-Type")        // Check to ensure query was provided in the request body        if r.Body == nil {            http.Error(w, "Must provide graphql query in request body", 400)            return        }        var rBody reqBody        // Decode the request body into rBody        err := json.NewDecoder(r.Body).Decode(&rBody)        if err != nil {            http.Error(w, "Error parsing JSON request body", 400)        }        fmt.Println(rBody.Query)        // Execute graphql query        result := gql.ExecuteQuery(rBody.Query, *s.GqlSchema)        // render.JSON comes from the chi/render package and handles        // marshalling to json, automatically escaping HTML and setting        // the Content-Type as application/json.        render.JSON(w, r, result)    }}當我運行查詢時,返回 {"entries" : null}。當我打印出發送到我的 go 服務器的查詢時,我得到以下信息:query variables($userID: Int) {  entries(userID: $userID) {    id    value    timeStamp    __typename  }}在第 2 行中,我希望看到條目(用戶 ID:2)。事實上,如果我從 vue 中的查詢中刪除變量,并在查詢中硬編碼 userID,一切正常。我檢查以確保以某種方式發送變量,如果我查看 POST 請求,則它在參數中具有以下內容:operationName   variablesquery   query variables($userID: Int) { entries(userID: $userID) { id value timeStamp __typename } }variables   {…}userID  2我是否沒有在后端正確接受數據,因為變量 userID 是在 POST 請求中發送的?
查看完整描述

1 回答

?
千萬里不及你

TA貢獻1784條經驗 獲得超9個贊

對于你的問題,你后臺接收的數據是正確的,查詢本身不包含查詢參數,查詢參數是通過variables鍵發送的。


響應問題是這一行result := gql.ExecuteQuery(rBody.Query, *s.GqlSchema)。 variables來自請求體也需要傳遞給ExecuteQuery. 使用函數的示例實現graphql-go/graphql Do可以是


func executeQuery(query string, schema graphql.Schema, variables map[string]interface{}) *graphql.Result {

    result := graphql.Do(graphql.Params{

        Schema:        schema,

        RequestString: query,

        VariableValues: variables

    })

    if len(result.Errors) > 0 {

        fmt.Printf("errors: %v", result.Errors)

    }

    return result

}

類型的一個例子reqBody可以是


type reqBody struct {

    Query string

    Variables map[string]interface{}

}

然后json.NewDecoder(r.Body).Decode(&rBody)會自動設置Query和Variables


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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