99designs / 99designs/gqlgen

Getters don't follow Go conventions

Open
#2,359 6 comments 0 reactions 0 assignees View on GitHub
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.