99designs / gqlgen

go generate based graphql server library
https://gqlgen.com
MIT License
9.96k stars 1.17k forks source link

error while running go generate #2211

Closed shimish2 closed 2 years ago

shimish2 commented 2 years ago

What happened?

I am getting error while running go generate

ec._fieldMiddleware undefined (type *executionContext has no field or method _fieldMiddleware)

What did you expect?

go generate should run successfully. After downgrading gqlgen to v1.14.0 & gqlparser to v2.2.0, go generate was successful.

versions

frederikhors commented 2 years ago

Please create a reproduction repo.

shimish2 commented 2 years ago

Please checkout this changes https://github.com/project-zot/zot/tree/main/pkg/extensions/search and try to run go generate from search directory.

frederikhors commented 2 years ago

Can you try with 0.17.9, please?

shimish2 commented 2 years ago

tried with 0.17.9 and got same error.

ashishb commented 2 years ago

Same issue with the latest version and downgrading gqlgen to v0.14.0 works for me as well.

andaaron commented 2 years ago

Hi, Looks like sticking to the v0.14.0 workaround is not working with Go 1.18 because it doesn't have the fix for #1961 (works with 1.17). Do we have a long term solution?

manisharma commented 2 years ago

any updates here ? go run github.com/99designs/gqlgen generate still fails

$ go version
go version go1.18 darwin/arm64
andaaron commented 2 years ago

For our project we started from scratch with gqlgen init, replaced the schema with ours, and did some code refactoring to move our resolvers to match the new file/folder structure (before this we did not have a gqlgen.yml, or a schema.resolvers.go file - the methods in schema.resolvers.go were implemented in resolvers.go).

Currently it's working with latest gqlgen and go1.18.

frederikhors commented 2 years ago

This is not a problem anymore with gqlgen latest versions and Go 1.16.x, 1.17.x, 1.18x.

Maybe you can use gqlgen init from scratch in a new dir and notice the differences from your project.

Or you can re-check re-reading the config page.

Definitely this is not something wrong with latest versions, hence I'm closing this.

andrey-bobryshev-90poe commented 2 years ago

I found the cause of this issue. If you running generate with no gqlgen.yml file you will be getting this error because generated code will contain this piece of code:

    resTmp := ec._fieldMiddleware(ctx, obj, func(rctx context.Context) (interface{}, error) {
        ctx = rctx // use context from middleware stack in children
        return obj.ID, nil
    })

This is where it comes from https://github.com/99designs/gqlgen/blob/6855b7290cab62a1fc6a26a2b633e0b5bbf248da/codegen/field.gotpl#L19-L23 Once you add gqlgen.yml everything works well. Probably $.AllDirectives.LocationDirectives var initialises wrong without config file.