web-ridge / gqlgen-sqlboiler

This is a plugin for gqlgen to generate converts + filter queries and resolvers for sqlboiler
MIT License
75 stars 13 forks source link

Issue while running `go run convert_plugin.go` #3

Closed alichherawalla closed 4 years ago

alichherawalla commented 4 years ago

Thanks for this amazing library!

Currently when executing go run convert_plugin.go I'm getting the following error

panic: recursive or concurrent call to RenderToFile detected

my gqlgen.yml looks like this

schema:
  - schema.graphql
exec:
  filename: graphql/generated.go
  package: graphql
model:
  filename: graphql/models/generated_models.go
  package: gqlmodels
resolver:
  filename: graphql/resolver.go
  type: Resolver

any idea why this would be an issue?

RichardLindhout commented 4 years ago

Does this work

gqlgen.yml

schema:
  - ../schema.graphql
exec:
  filename: graphql_models/generated.go
  package: graphql_models
model:
  filename: graphql_models/generated_models.go
  package: graphql_models
resolver:
  filename: resolver.go
  type: Resolver
RichardLindhout commented 4 years ago

The directories needs aliasing so these are required till a next release| graphql_models for gqlgen models models for sqlboiler models

RichardLindhout commented 4 years ago

Do you have single or multiple schema.graphql?

alichherawalla commented 4 years ago

Hi, thanks for the swift response.

It doesn't work with the gqlgen.yml you posted, unfortunately.

I have a single schema.graphql

alichherawalla commented 4 years ago
[convert] get boiler models
[convert] get model with information
[convert] get extra's from schema
[convert] render preload.gotpl
renderError template.gotpl: template: template.gotpl:21:3: executing "template.gotpl" at <reserveImport $.BackendModelsPath>: error calling reserveImport: empty ambient import
[convert] render convert.gotpl
renderError template.gotpl: template: template.gotpl:21:3: executing "template.gotpl" at <reserveImport $.BackendModelsPath>: error calling reserveImport: empty ambient import
[convert] render convert_input.gotpl
renderError template.gotpl: template: template.gotpl:21:3: executing "template.gotpl" at <reserveImport $.BackendModelsPath>: error calling reserveImport: empty ambient import
[convert] render filter.gotpl
renderError template.gotpl: template: template.gotpl:22:3: executing "template.gotpl" at <reserveImport $.BackendModelsPath>: error calling reserveImport: empty ambient import
panic: recursive or concurrent call to RenderToFile detected

goroutine 1 [running]:
github.com/99designs/gqlgen/codegen/templates.Render(0xc0002f4820, 0xe, 0x0, 0x0, 0xc0000e5a40, 0x3f, 0x101, 0x0, 0x0, 0x0, ...)
        /Users/mac/GolandProjects/go-boiler/vendor/github.com/99designs/gqlgen/codegen/templates/templates.go:63 +0xfdf
github.com/99designs/gqlgen/codegen.GenerateCode(0xc000a1e880, 0xc000a1e880, 0x0)
        /Users/mac/GolandProjects/go-boiler/vendor/github.com/99designs/gqlgen/codegen/generate.go:8 +0xfb
github.com/99designs/gqlgen/api.Generate(0xc00031cdc0, 0xc0001f3f38, 0x2, 0x2, 0xc0003034d0, 0xc0001f3f18)
        /Users/mac/GolandProjects/go-boiler/vendor/github.com/99designs/gqlgen/api/generate.go:77 +0x378
github.com/wednesday-solution/go-boiler.Convert()
        /Users/mac/GolandProjects/go-boiler/convert_plugin.go:26 +0x180
main.main()
        /Users/mac/GolandProjects/go-boiler/cmd/convert/main.go:6 +0x20

Process finished with exit code 2

this is the full stack trace

alichherawalla commented 4 years ago

also btw i'm using postgres

RichardLindhout commented 4 years ago

Hmm strange, on what versions are you of gqlgen and sqlboiler? I'm on the latest versions of both.

RichardLindhout commented 4 years ago

Mine is:

// +build ignore

package main

import (
    "fmt"
    "os"

    "github.com/99designs/gqlgen/api"
    "github.com/99designs/gqlgen/codegen/config"
    convertsGenerator "github.com/web-ridge/gqlgen-sqlboiler/convert"
    resolverGenerator "github.com/web-ridge/gqlgen-sqlboiler/resolver"
)

func main() {
    cfg, err := config.LoadConfigFromDefaultLocations()
    if err != nil {
        fmt.Fprintln(os.Stderr, "failed to load config", err.Error())
        os.Exit(2)
    }

    convertHelpersDir := "helpers"
    sqlboilerDir := "models"
    gqlgenModelDir := "graphql_models"

    err = api.Generate(cfg,
        api.AddPlugin(convertsGenerator.New(
            convertHelpersDir, // directory where convert.go, convert_input.go and preload.go should live
            sqlboilerDir,      // directory where sqlboiler files are put
            gqlgenModelDir,    // directory where gqlgen models live
        )),
        api.AddPlugin(resolverGenerator.New(
            convertHelpersDir,
            sqlboilerDir,
            gqlgenModelDir,
            "gitlab.com/decicify/app/backend/auth",
        )),
    )
    if err != nil {
        fmt.Println("error!!")
        fmt.Fprintln(os.Stderr, err.Error())
        os.Exit(3)
    }
}
RichardLindhout commented 4 years ago

It crashes on this line in qqlgen code. https://github.com/99designs/gqlgen/blob/master/codegen/templates/templates.go#L68

Your convert_plugin.go file needs to live in the same folder as the project where you want to generate.

Schermafbeelding 2020-04-10 om 21 17 06
RichardLindhout commented 4 years ago

Did you generate your models for your database with https://github.com/volatiletech/sqlboiler?

alichherawalla commented 4 years ago

I'm on the latest versions of gqlgen and sqlboiler as well. yes i've generated the database models from sqlboiler. and yes the convert_plugin is in the root folder image

RichardLindhout commented 4 years ago

Interesting, to be honest I don't know if the output of sqlboiler is any different than with postgressql vs mysql.

What operating system do you use?

alichherawalla commented 4 years ago

macos

RichardLindhout commented 4 years ago

Me too :)

alichherawalla commented 4 years ago

i simplified the schema to have a just single type with a few scalar fields but it still doesn't work for some reason

RichardLindhout commented 4 years ago

Can you share your schema (maybe privately)

RichardLindhout commented 4 years ago

My email is on my profile

RichardLindhout commented 4 years ago

I'm generating my schema with: https://github.com/web-ridge/sqlboiler-graphql-schema a.t.m.

go run github.com/web-ridge/sqlboiler-graphql-schema --output=../schema.graphql --skip-input-fields=organizationId --directives=isAuthenticated
RichardLindhout commented 4 years ago

Maybe I made some assumptions about the schema.grapql which are not always true for everyone.

alichherawalla commented 4 years ago

I used that to generate my schema as well

alichherawalla commented 4 years ago

I tried it on a playground Blog/Comment/Post project as well, let me share that schema with you

RichardLindhout commented 4 years ago

I did have this issue too now I remember: the cause is here. https://github.com/99designs/gqlgen/blob/master/codegen/templates/templates.go#L62

This library generates multiple files instead of one, so we we needed to prevent this panic from happening.

That was fixed 6 days ago: https://github.com/web-ridge/gqlgen-sqlboiler/blob/master/convert/convert.go#L208

Somehow go.mod does not always understand how to update to the latest version

Maybe put this instead of the other web-ridge vendors in go.mod and run go mod tidy. And try regenerating.

    github.com/web-ridge/gqlgen-sqlboiler/convert 82c6f4a43f5e8052d109bed0ce72ec194f726147
    github.com/web-ridge/gqlgen-sqlboiler/helper 82c6f4a43f5e8052d109bed0ce72ec194f726147
    github.com/web-ridge/gqlgen-sqlboiler/resolver 82c6f4a43f5e8052d109bed0ce72ec194f726147
alichherawalla commented 4 years ago

that doesn't seem to work, could you please send me your go.mod?

RichardLindhout commented 4 years ago

I've added an example. https://github.com/web-ridge/gqlgen-sqlboiler/tree/master/examples/social-network

I do something wrong here with go-modules maybe I need to split up the project or something like that to keep everything on latest version

alichherawalla commented 4 years ago

what's the go version that you are on?

alichherawalla commented 4 years ago

i've added the project locally to make sure that there are no issues with the version for now but it still doesn't seem to work. I wasn't able to run your project either. Could it be something in the environment configuration?

RichardLindhout commented 4 years ago

➜ ~ go version go version go1.13.5 darwin/amd64

RichardLindhout commented 4 years ago

I don't know, it's strange!

RichardLindhout commented 4 years ago

I also don't have problems with latest version of golang go version go version go1.14.2 darwin/amd64

RichardLindhout commented 4 years ago

Ah!! I have the same problem now if I take it outside my golang path src directory

RichardLindhout commented 4 years ago

Schermafbeelding 2020-04-11 om 16 27 24

RichardLindhout commented 4 years ago

I think I need to rewrite this function or something like that. https://github.com/web-ridge/gqlgen-sqlboiler/blob/master/convert/convert.go#L134

Do you have the option available to move to your GOPATH folder?

alichherawalla commented 4 years ago

that did the trick! Thanks very much

RichardLindhout commented 4 years ago

In v2.1.1 it works outside of GOPATH!

@randallmlough added support for repositories outside of GOPATH based on where the go.mod file lives👌