文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

无法从 gqlgen-sqlboiler 生成 generated_sort.go

2024-02-14 11:22

关注

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的详细内容,更多请关注编程网其它相关文章!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯