Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Federation entity resolver generation #1454

Open
nhibberd opened this issue Feb 4, 2021 · 4 comments
Open

Federation entity resolver generation #1454

nhibberd opened this issue Feb 4, 2021 · 4 comments
Labels
accepted bug Something isn't working

Comments

@nhibberd
Copy link

nhibberd commented Feb 4, 2021

A combination of #1388 & #1429

What happened?

{Entity}Resolver's are not generated (example example/federation/reviews/graph/schema.resolvers.go) without specifying an explicit custom model (e.g. example/federation/reviews/graph/model/models.go)

Replication in the examples:

cd example/federation/reviews
rm graph/model/models.go
go generate server.go
validation failed: packages.Load: gqlgen/example/federation/reviews/graph/schema.resolvers.go:38:40: ProductResolver not declared by package generated
gqlgen/example/federation/reviews/graph/schema.resolvers.go:41:37: UserResolver not declared by package generated

exit status 3

What did you expect?

I expected that generated Entitys would also generate the EntityResolver's? Is this not the case by design?

Minimal graphql.schema and models to reproduce

type Review {
    body: String!
}

extend type User @key(fields: "id") {
    id: ID! @external
    reviews: [Review]
}

versions

  • gqlgen version? v0.13.0
  • go version? 1.14
  • dep or go modules?

Notes

Looking into the code a little more, I assume the issue is arising from codegen/config/config.go - Init()

@nhibberd
Copy link
Author

nhibberd commented Feb 5, 2021

Workaround for anyone else who comes across this:

Add a custom model for the type for the entity being extended, in this example User

package model

type User struct {
	ID string `json:"id"`
}

func (User) IsEntity() {}

And the model to the config - autobind

autobind: []
  - "mymodel/model"

@MichaelJCompton
Copy link
Contributor

Yep, looks like this doesn't quite work correctly without a custom model.

The work around (use custom models) is annoying, but looks like it generates the correct code and isn't functionally different.

@MichaelJCompton
Copy link
Contributor

Actually, looks like it's specifically to do with the fields generated in the model

extend type Product @key(fields: "upc") {
    upc: String! @external
    reviews: [Review]
}

Won't work with a model like

type Product struct {
	Upc        string    `json:"upc"`
	Reviews []*Review `json:"reviews"`
}

The Review field means that the ProductResolver doesn't get generated.

@genesor
Copy link

genesor commented Mar 30, 2021

Another solution to have the resolvers without a custom model is to force them in the gqlgen.yml file.

models:
  User:
    fields:
      reviews:
        resolver: true
  Product:
    fields:
      reviews:
        resolver: true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants