Unable to generate generated_sort.go from gqlgen-sqlboiler

PHPz
Release: 2024-02-14 12:00:12
forward
658 people have browsed it

无法从 gqlgen-sqlboiler 生成 generated_sort.go

php editor Xinyi mentioned that sometimes when using the gqlgen-sqlboiler generator, you may encounter the problem that the generated_sort.go file cannot be generated. This problem may be caused by configuration or dependency errors. Before solving this problem, we need to carefully check the configuration file and related dependencies to ensure that there are no omissions or errors. At the same time, we can also try to update the versions of the generator and related libraries to solve possible bugs. If the problem persists, we can ask the community for help to see if anyone else has encountered the same problem and found a solution. All in all, solving this problem requires patience and care, and by troubleshooting and trying different solutions, we can hopefully find an effective solution.

Question content

I am using sqlboiler version: v4.14.2 gqlgen version: v0.17.33 gqlgen-sqlboiler version: v3.3.12 https://github.com/web-ridge/gqlgen-sqlboiler

After following the readme.md prerequisites, I get the following error when running convert.go The following is 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")
        }

    }
}
Copy after login

The following is the log

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
Copy after login

Please help to solve this problem?

Workaround

Solved the problem by mapping the correct directory. convert.go on the side When creating a structure in convert.go

//output represents the folder where the files will be generated from gqlgen-sqlboiler

output := structs.config{
        directory:   "helpers", // supports root or sub directories
        packagename: "helpers",
    }
Copy after login

//backend is the folder where all sqlboiler models are generated

backend := structs.config{
    directory:   "models",
    packagename: "models",
}
Copy after login

//! ! ! The important front end is the folder where gqlgen generates all graphql files

frontend := structs.Config{
    Directory:   "graph",
    PackageName: "graph",
}
Copy after login

Front-end folder mapping error.

The above is the detailed content of Unable to generate generated_sort.go from gqlgen-sqlboiler. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template