首頁 > 後端開發 > Golang > 主體

無法從 gqlgen-sqlboiler 產生 generated_sort.go

PHPz
發布: 2024-02-14 12:00:12
轉載
660 人瀏覽過

无法从 gqlgen-sqlboiler 生成 generated_sort.go

php小編新一提到,有時在使用gqlgen-sqlboiler產生器時,可能會遇到無法產生generated_sort.go檔的問題。這個問題可能是由於配置或依賴關係的錯誤所導致的。在解決這個問題之前,我們需要仔細檢查設定檔和相關依賴,確保沒有遺漏或錯誤。同時,我們也可以嘗試更新生成器和相關函式庫的版本,以解決可能存在的bug。如果問題仍然存在,我們可以尋求社區的幫助,看看是否有其他人遇到了相同的問題,並且找到了解決方案。總之,解決這個問題需要耐心和細心,透過追蹤和嘗試不同的解決方案,我們有望找到有效的解決方法。

問題內容

我正在使用 sqlboiler版本:v4.14.2 gqlgen版本:v0.17.33 gqlgen-sqlboiler 版本:v3.3.12 https://github.com/web-ridge/gqlgen-sqlboiler

遵循 readme.md 先決條件後,在執行 convert.go 時出現以下錯誤 以下是convert.go

package main

import (
    "github.com/99designs/gqlgen/codegen/config"
    "github.com/rs/zerolog/log"
    gbgen "github.com/web-ridge/gqlgen-sqlboiler/v3"
    "github.com/web-ridge/gqlgen-sqlboiler/v3/cache"
    "github.com/web-ridge/gqlgen-sqlboiler/v3/structs"
    "os"
    "os/exec"
    "strings"
)

func main() {
    // change working directory to parent directory where all configs are located
    newdir, _ := os.getwd()
    os.chdir(strings.trimsuffix(newdir, "/convert"))

    enablesoftdeletes := true
    boilerargs := []string{"mysql", "--no-back-referencing", "--wipe", "-d"}
    if enablesoftdeletes {
        boilerargs = append(boilerargs, "--add-soft-deletes")
    }
    cmd := exec.command("sqlboiler", boilerargs...)

    err := cmd.run()
    if err != nil {
        log.fatal().err(err).str("command", cmd.string()).msg("error generating dm models running sql-boiler")
    }

    output := structs.config{
        directory:   "helpers", // supports root or sub directories
        packagename: "helpers",
    }
    backend := structs.config{
        directory:   "models",
        packagename: "models",
    }
    frontend := structs.config{
        directory:   "models",
        packagename: "models",
    }

    boilercache := cache.initializeboilercache(backend)

    generateschema := true
    generatedschema := !generateschema
    if generateschema {
        if err := gbgen.schemawrite(
            gbgen.schemaconfig{
                boilercache:         boilercache,
                directives:          []string{"isauthenticated"},
                skipinputfields:     []string{"createdat", "updatedat", "deletedat"},
                generatemutations:   true,
                generatebatchcreate: false,
                generatebatchdelete: false,
                generatebatchupdate: false,
                hookshouldaddmodel: func(model gbgen.schemamodel) bool {
                    if model.name == "config" {
                        return false
                    }
                    return true
                },
                hookchangefields: func(model *gbgen.schemamodel, fields []*gbgen.schemafield, parentype gbgen.parenttype) []*gbgen.schemafield {
                    //profile: userpayload! @isauthenticated

                    return fields
                },
                hookchangefield: func(model *gbgen.schemamodel, field *gbgen.schemafield) {
                    //"userid", "userorganizationid",
                    if field.name == "userid" && model.name != "useruserorganization" {
                        field.skipinput = true
                    }
                    if field.name == "userorganizationid" && model.name != "useruserorganization" {
                        field.skipinput = true
                    }
                },
            },
            "../schema.graphql",
            gbgen.schemagenerateconfig{
                mergeschema: false,
            },
        ); err != nil {
            log.fatal().err(err).msg("error generating schema")
        }
        generatedschema = true
    }
    if generatedschema {

        cfg, err := config.loadconfigfromdefaultlocations()
        if err != nil {
            log.fatal().err(err).msg("error loading config")
        }

        data, err := gbgen.newmodelplugin().generatecode(cfg)
        if err != nil {
            log.fatal().err(err).msg("error generating graphql models using gqlgen")
        }

        modelcache := cache.initializemodelcache(cfg, boilercache, output, backend, frontend)

        if err := gbgen.newconvertplugin(
            modelcache,
            gbgen.convertpluginconfig{
                databasedriver: gbgen.mysql,
                //searchable: {
                //  company: {
                //      column: dm.companycolumns.name
                //  },
                //},
            },
        ).generatecode(); err != nil {
            log.fatal().err(err).msg("error while generating convert/filters")
        }

        if err := gbgen.newresolverplugin(
            config.resolverconfig{
                filename: "resolvers/all_generated_resolvers.go",
                package:  "resolvers",
                type:     "resolver",
            },
            output,
            boilercache,
            modelcache,
            gbgen.resolverpluginconfig{

                enablesoftdeletes: enablesoftdeletes,
                // authorization scopes can be used to override e.g. userid, organizationid, tenantid
                // this will be resolved used the provided scoperesolvername if the result of the addtrigger=true
                // you would need this if you don't want to require these fields in your schema but you want to add them
                // to the db model.
                // if you do have these fields in your schema but want them authorized you could use a gqlgen directive
                authorizationscopes: []*gbgen.authorizationscope{},
                //  {
                //      importpath:        "github.com/my-repo/app/backend/auth",
                //      importalias:       "auth",
                //      scoperesolvername: "useridfromcontext", // function which is called with the context of the resolver
                //      boilercolumnname:  "userid",
                //      addhook: func(model *gbgen.boilermodel, resolver *gbgen.resolver, templatekey string) bool {
                //          // fmt.println(model.name)
                //          // fmt.println(templatekey)
                //          // templatekey contains a unique where the resolver tries to add something
                //          // e.g.
                //          // most of the time you can ignore this

                //          // we want the delete call to work for every object we don't want to take in account te user-id here
                //          if resolver.isdelete {
                //              return false
                //          }

                //          var addresolver bool
                //          for _, field := range model.fields {
                //              if field.name == "userid" {
                //                  addresolver = true
                //              }
                //          }
                //          return addresolver
                //      },
                //  },
                //  {
                //      importpath:        "github.com/my-repo/app/backend/auth",
                //      importalias:       "auth",
                //      scoperesolvername: "userorganizationidfromcontext", // function which is called with the context of the resolver
                //      boilercolumnname:  "userorganizationid",

                //      addhook: func(model *gbgen.boilermodel, resolver *gbgen.resolver, templatekey string) bool {
                //          // fmt.println(model.name)
                //          // fmt.println(templatekey)
                //          // templatekey contains a unique where the resolver tries to add something
                //          // e.g.
                //          // most of the time you can ignore this
                //          var addresolver bool
                //          for _, field := range model.fields {
                //              if field.name == "userorganizationid" {
                //                  addresolver = true
                //              }
                //          }
                //          return addresolver
                //      },
                //  },
                // },
            },
        ).generatecode(data); err != nil {
            log.fatal().err(err).msg("error while generating resolvers")
        }

    }
}
登入後複製

以下是日誌

go run convert/convert.go
4:10PM DBG [boiler-cache] building cache
4:10PM DBG [boiler-cache] built cache!
4:10PM DBG write GraphQL schema to disk bytes=3918
file=../schema.graphql
4:10PM DBG formatting GraphQL schema
../schema.graphql 71ms 4:10PM DBG formatted GraphQL schema
4:10PM DBG [model-cache] get structs
4:10PM DBG [model-cache] get extra's from schema
4:10PM DBG [model-cache] enhance structs with information
4:10PM DBG [model-cache] built cache!
4:10PM DBG [convert] render generated_convert.gotpl
4:10PM DBG [convert] rendered generated_convert.gotpl
4:10PM DBG [convert] render generated_convert_batch.gotpl
4:10PM DBG [convert] rendered generated_convert_batch.gotpl
4:10PM DBG [convert] render generated_convert_input.gotpl
4:10PM DBG [convert] rendered generated_convert_input.gotpl
4:10PM DBG [convert] render generated_filter.gotpl
4:10PM DBG [convert] rendered generated_filter.gotpl
4:10PM DBG [convert] render generated_preload.gotpl
4:10PM DBG [convert] rendered generated_preload.gotpl
4:10PM DBG [convert] render generated_sort.gotpl
4:10PM ERR could not parse golang file error="src.go:1:1: expected
'package', found 'EOF'"
4:10PM ERR error while rendering generated_sort.gotpl error="errors
while writing template to helpers/generated_sort.go writeError: ,
contentError: execute: template: :44:17: executing "" at
<$field.Enum.Name>: nil pointer evaluating *structs.Enum.Name,
importError: helpers/generated_sort.go:1:1: expected 'package', found
'EOF'"
4:10PM DBG [convert] rendered generated_sort.gotpl
4:10PM ERR could not parse golang file error="src.go:1:1: expected
'package', found 'EOF'"
4:10PM ERR could not close file error="invalid argument"
fileName=resolvers/all_generated_resolvers.go
4:10PM FTL error while generating resolvers error="errors while
printing template to resolvers/all_generated_resolvers.go  invalid
argument"
exit status 1
登入後複製

請幫忙解決這個問題?

解決方法

透過映射正確的目錄解決了問題。在側面convert.go 在convert.go中建立結構時

//output 代表將從 gqlgen-sqlboiler 產生檔案的資料夾

output := structs.config{
        directory:   "helpers", // supports root or sub directories
        packagename: "helpers",
    }
登入後複製

//backend是產生所有sqlboiler模型的資料夾

backend := structs.config{
    directory:   "models",
    packagename: "models",
}
登入後複製

//! ! !重要的前端是gqlgen產生所有graphql檔案的資料夾

#
frontend := structs.Config{
    Directory:   "graph",
    PackageName: "graph",
}
登入後複製

前端資料夾映射錯誤。

以上是無法從 gqlgen-sqlboiler 產生 generated_sort.go的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:stackoverflow.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板