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

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

gqlgen go,通過添加一個解析器減少數(shù)據(jù)庫調(diào)用

gqlgen go,通過添加一個解析器減少數(shù)據(jù)庫調(diào)用

Go
烙印99 2022-12-05 17:17:56
我在解決導(dǎo)致性能下降的特定情況時遇到了一些麻煩。我很確定這是可以做到的,但我不知道該怎么做。這是暴露問題的示例模式:type Answer{    answerId: String!    text: String!    topic: Topic!}type Topic {    topicId: String!    name: String!    level: Int!}extend type Query {    answer(answerId: String!): Answer!    answers: [Answer!]! }我遵循了文檔,特別是這部分https://gqlgen.com/getting-started/#dont-eagerly-fetch-the-user 從我的架構(gòu)中,它生成以下解析器:func (r *queryResolver) Answer(ctx context.Context, answerId string) (*models.Answer, error) {...#Single Query which retrives single record of Answer from DB.#Fills a model Answer with the Id and the text#Proceeds by calling the Topic resolver...}func (r *queryResolver) Answers(ctx context.Context) ([]*models.Answer, error) {...#Single Query which retrives list of Answers from DB#Fills a list of model Answer with the Id and the text-->#For each element of that list, it calls the Topic resolver...}func (r *answerResolver) Topic(ctx context.Context, obj *models.Answer) (*models.Topic, error) {...#Single Query which retrives single record of Topic from DB#Return a model Topic with id, name and level...}當(dāng)使用參數(shù)answer調(diào)用查詢時,將觸發(fā)解析器,它解析屬性并調(diào)用解析器。解析器按預(yù)期工作,檢索并將其合并到并返回。answerIdanswertextTopicTopicTopicAnswer當(dāng)answers查詢在沒有answerId參數(shù)的情況下被調(diào)用時,answer解析器被觸發(fā),它answers通過單個查詢檢索列表。然后,對于該列表的每個元素,它調(diào)用Topic解析器。Topic 檢索 a并將其Topic合并到單 例中Answer并返回。結(jié)果在這兩種情況下都可以,但是answers如果我要求很多答案,查詢就會成為性能問題。對于每個答案,Topic解析器都會被觸發(fā)并執(zhí)行查詢以檢索單個記錄。前任。如果我有 2 個答案 --> 1 個查詢[Answer0, Answer1],然后 1 個查詢 Topic0和 1 個Topic1前任。10 個答案 --> 1 個[Answer0, ..., Answer9],然后每個 10 個TopicN我想獲得一些topic數(shù)組解析器,例如func (r *answersResolver) Topics(ctx context.Context, obj *[]models.Answer) (*[]models.Topic, error) {...#Single Query which retrives list of Topics from DB#Return a list of model Topic with id, name and level...}我希望返回數(shù)組的每個元素都與數(shù)組的相應(yīng)元素合并Answers。有可能嗎?我在哪里可以找到這種方法的例子?謝謝
查看完整描述

1 回答

?
慕蓋茨4494581

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

可以使用數(shù)據(jù)加載器(文檔)解決問題


我必須為以下數(shù)據(jù)源實現(xiàn)Topics:


package dataloader


import (

    "github.com/graph-gophers/dataloader"

)


type ctxKey string


const (

    loadersKey = ctxKey("dataloaders")

)



type TopicReader struct {

    conn *sql.DB

}


func (t *TopicReader) GetTopics(ctx context.Context, keys dataloader.Keys) []*dataloader.Result {

    topicIDs := make([]string, len(keys))

    for ix, key := range keys {

        topicIDs[ix] = key.String()

    }

    res := u.db.Exec(

        r.Conn,

        "SELECT id, name, level

        FROM topics

        WHERE id IN (?" + strings.Repeat(",?", len(topicIDs-1)) + ")",

        topicIDs...,

    )

    defer res.Close()


    output := make([]*dataloader.Result, len(keys))

    for index, _ := range keys {

            output[index] = &dataloader.Result{Data: res[index], Error: nil}

    }

    return output

}


type Loaders struct {

    TopicLoader *dataloader.Loader

}



func NewLoaders(conn *sql.DB) *Loaders {

    topicReader := &TopicReader{conn: conn}

    loaders := &Loaders{

        TopicLoader: dataloader.NewBatchedLoader(t.GetTopics),

    }

    return loaders

}


func Middleware(loaders *Loaders, next http.Handler) http.Handler {

    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

        nextCtx := context.WithValue(r.Context(), loadersKey, loaders)

        r = r.WithContext(nextCtx)

        next.ServeHTTP(w, r)

    })

}


func For(ctx context.Context) *Loaders {

    return ctx.Value(loadersKey).(*Loaders)

}


func GetTopic(ctx context.Context, topicID string) (*model.Topic, error) {

    loaders := For(ctx)

    thunk := loaders.TopicLoader.Load(ctx, dataloader.StringKey(topicID))

    result, err := thunk()

    if err != nil {

        return nil, err

    }

    return result.(*model.Topic), nil

}



查看完整回答
反對 回復(fù) 2022-12-05
  • 1 回答
  • 0 關(guān)注
  • 137 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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