99designs / 99designs/gqlgen

Getters don't follow Go conventions

未关闭
#2,359 6 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Go
星标
10.8k
派生
1.3k
平均合并
2 天 36 分钟
30 天内合并 PR
26

描述

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`.

贡献指南

打开贡献指南

调研方向

查看 gqlgen 代码库中 GraphQL 接口的代码生成逻辑,可能位于代码生成包中。了解当前如何生成 GetID() 和 GetTitle() 方法。查看关联的 PR #2314 和 issue #1469,以了解相关背景。此更改涉及更新方法命名约定以移除 'Get' 前缀,并修改返回签名以包含 error 返回值。通过为示例 schema 生成代码并验证新的接口签名进行测试。

由索引模型根据 Issue 内容生成。

评估

技术栈
go
领域
backend-api-design
Issue 类型
重构
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
描述清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。