Getters don't follow Go conventions
- Dominant language
- Go
- Stars
- 10.8k
- Forks
- 1.3k
- Avg merge
- 2d 36m
- Merged PRs (30d)
- 26
Description
https://github.com/99designs/gqlgen/issues/1469 was closed by https://github.com/99designs/gqlgen/pull/2314, but while the implementation is a good step forward I don't think it utilizes golang to its fullest extent.
Following the change above, a schema defined as...
```graphql
interface Resource {
id: ID!
title: String!
}
```
is generated as...
```go
type Resource interface {
IsResource()
GetID() string
GetTitle() string
}
```
However, this has two problems I see:
1. Getters shouldn't have a `Get` prefix (see [here](https://go.dev/doc/effective_go#Getters))
2. Methods should leverage go's multiple return values to return errors whenever applicable (see [here](https://go.dev/doc/effective_go#multiple-returns))
It'd be better if the go type was generated as
```go
type Resource interface {
IsResource()
ID() (string, error)
Title() (string, error)
}
```
Naming convention is minor (but good practice), but the errors are more important IMO. For example as https://github.com/99designs/gqlgen/issues/2331#issuecomment-1221510135 points out there are many situations (eg. data loaders) where you can't get the value for some reason, and in the current implementation you'd either need to not report the error (bad) or panic (really bad). For methods that don't use a loader or have no reason to report any error, they can simply always return `nil`.
Contributor guide
Assessment
This issue has not been assessed yet.