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

Suggestion: Generate resolver for fields missing from go model #63

Closed
andrewmunro opened this issue Mar 23, 2018 · 4 comments
Closed

Suggestion: Generate resolver for fields missing from go model #63

andrewmunro opened this issue Mar 23, 2018 · 4 comments

Comments

@andrewmunro
Copy link

andrewmunro commented Mar 23, 2018

When we first started using gqlgen, if your go model did not have a field publicly accessible that was specified on your schema, the generator would generate a resolver for you to manually handle it.

// schema.graphql
schema {
	query: Query
}

type Query {
	cars: [Car]!
}

type Car {
	name: String
	model: String
	created:Time
	new: Boolean
}
// model.go
package model

type Car struct {
	Name    string    `db:"name"`
	Model   string    `db:"model"`
	Created time.Time `db:"created"`
}
// types.json
{
    "Car": "path/to/model.Car"
}
// generated.go
type Resolvers interface {
	Query_cars(ctx context.Context) ([]model.Car, error)
	Field_car_new(ctx context.Context, car model.Car) (bool, error) // This is missing
}

Unfortunately, this functionality disappeared when you introduced the model generator.
This was really nice feature if you have any fields on your graphql model that are generated by business logic. For the example above, something like:

// resolver.go
func (r *QueryResolver) Field_car_new(ctx context.Context, car model.Car) (bool, error) {
	weekAgo := time.Now().AddDate(0, -7, 0)
	return car.Created.After(weekAgo)
}

I can understand the answer is probably to separate db go models from graphql go models, but for an example like this where most of the fields are duplicated, I don't see it worth it for the added overhead and complexity.

@vektah
Copy link
Collaborator

vektah commented Mar 25, 2018

This should still work the way you describe. Model generation only happens if you don't add the model to types.json.

eg, the starwars example is still using it for the friends connection

Whats probably happening is types.json isnt being searched for by default any more, you need to add a -typemap types.json if you want to map to your own types.

@andrewmunro
Copy link
Author

andrewmunro commented Mar 26, 2018

@vektah No it is searching fine, we now receive an error:

unable to bind Car.new to anything, path/to/model.Car has no suitable fields or methods

If you add a New field to the Car model it fixes it.

Generating with the following:

//go:generate gorunpkg github.com/vektah/gqlgen -package schema -schema schema.graphql -typemap types.json -out generated.go

It works for custom type connections (hence the star wars example still working), but not scalar values, e.g string

@vektah vektah closed this as completed in 652c567 Mar 26, 2018
@vektah
Copy link
Collaborator

vektah commented Mar 26, 2018

that was just a warning, but I've removed it to avoid future confusion.

Scalars has an example:
https://github.com/vektah/gqlgen/blob/master/example/scalars/resolvers.go#L49-L55

@Sparx2
Copy link

Sparx2 commented Jan 15, 2022

😶

cgxxv pushed a commit to cgxxv/gqlgen that referenced this issue Mar 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants