第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

GqlGen - 在字段解析器中訪問查詢輸入?yún)?shù)

GqlGen - 在字段解析器中訪問查詢輸入?yún)?shù)

Go
慕蓋茨4494581 2023-02-21 13:20:55
使用 GqlGen 生成代碼后,已經(jīng)創(chuàng)建了一些字段解析器方法。我需要訪問字段解析器中的查詢輸入?yún)?shù),但我不確定如何訪問它。我需要從上下文中獲取這些值嗎?或者還有其他辦法嗎?查詢解析器:func (r *queryResolver) Main(ctx context.Context, device string) (*models.Main, error) {...}字段解析器:// Version is the resolver for the version field.func (r *mainResolver) Version(ctx context.Context, obj *models.Main) (*models.Version, error) {        // I NEED TO ACCESS device param here which is passed in Main method    panic(fmt.Errorf("not implemented: Version - version"))}謝謝,
查看完整描述

1 回答

?
斯蒂芬大帝

TA貢獻(xiàn)1827條經(jīng)驗 獲得超8個贊

FieldContext我認(rèn)為您可以在父解析器中找到參數(shù)。graphql.GetFieldContext你可以這樣得到它:


// Version is the resolver for the version field.

func (r *mainResolver) Version(ctx context.Context, obj *models.Main) (*models.Version, error) {

    device := graphql.GetFieldContext(ctx).Parent.Args["device"].(string)

    // ...

}

該字段Args是一個map[string]interface{},因此您可以按名稱訪問參數(shù),然后將它們鍵入斷言為它們應(yīng)該是什么。


如果解析器嵌套了多個級別,您可以編寫一個函數(shù)沿著上下文鏈向上遍歷,直到找到具有該值的祖先。使用 Go 1.18+ 泛型,該函數(shù)可以重用于任何值類型,使用類似于 json.Unmarshal 的模式:


func FindGqlArgument[T any](ctx context.Context, key string, dst *T) {

    if dst == nil {

        panic("nil destination value")

    }

    for fc := graphql.GetFieldContext(ctx); fc != nil; fc = fc.Parent {

        v, ok := fc.Args[key]

        if ok {

            *dst = v.(T)

        }

    }

    // optionally handle failure state here

}

并將其用作:


func (r *deeplyNestedResolver) Version(ctx context.Context, obj *models.Main) (*models.Version, error) {

    var device string 

    FindGqlArgument(ctx, "device", &device)

}

如果這不起作用,也可以嘗試 with graphql.GetOperationContext,這基本上沒有記錄......(歸功于@Shashank Sachan)


graphql.GetOperationContext(ctx).Variables["device"].(string)


查看完整回答
反對 回復(fù) 2023-02-21
  • 1 回答
  • 0 關(guān)注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號