Rumah  >  Artikel  >  pembangunan bahagian belakang  >  Tidak dapat menjana generated_sort.go daripada gqlgen-sqlboiler

Tidak dapat menjana generated_sort.go daripada gqlgen-sqlboiler

PHPz
PHPzke hadapan
2024-02-14 12:00:12651semak imbas

无法从 gqlgen-sqlboiler 生成 generated_sort.go

Editor PHP Xinyi menyebut bahawa kadangkala apabila menggunakan penjana gqlgen-sqlboiler, anda mungkin menghadapi masalah bahawa fail generated_sort.go tidak boleh dijana. Masalah ini mungkin disebabkan oleh ralat konfigurasi atau pergantungan. Sebelum menyelesaikan masalah ini, kita perlu menyemak fail konfigurasi dan kebergantungan yang berkaitan dengan teliti untuk memastikan tiada peninggalan atau ralat. Pada masa yang sama, kami juga boleh cuba mengemas kini versi penjana dan perpustakaan berkaitan untuk menyelesaikan kemungkinan pepijat. Jika masalah berterusan, kami boleh meminta bantuan komuniti untuk melihat sama ada orang lain pernah menghadapi masalah yang sama dan menemui penyelesaian. Secara keseluruhannya, menyelesaikan masalah ini memerlukan kesabaran dan penjagaan, dan dengan menyelesaikan masalah dan mencuba penyelesaian yang berbeza, kami berharap dapat mencari penyelesaian yang berkesan.

Kandungan soalan

Saya guna versi sqlboiler: v4.14.2 versi gqlgen: v0.17.33 versi gqlgen-sqlboiler: v3.3.12 https://github.com/web-ridge/gqlgen-sqlboiler

Selepas mengikuti prasyarat readme.md, saya mendapat ralat berikut semasa menjalankan convert.go Yang berikut ialah 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")
        }

    }
}

Berikut ialah 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

Tolong bantu selesaikan masalah ini?

Penyelesaian

Menyelesaikan masalah dengan memetakan direktori yang betul. tukar.pergi sebelah Apabila mencipta struktur dalam convert.go

//output mewakili folder tempat fail akan dijana daripada gqlgen-sqlboiler

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

// backend ialah folder di mana semua model sqlboiler dijana

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

//! ! ! Bahagian hadapan yang penting ialah folder tempat gqlgen menjana semua fail graphql

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

Ralat pemetaan folder bahagian hadapan.

Atas ialah kandungan terperinci Tidak dapat menjana generated_sort.go daripada gqlgen-sqlboiler. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Artikel ini dikembalikan pada:stackoverflow.com. Jika ada pelanggaran, sila hubungi admin@php.cn Padam