99designs / 99designs/gqlgen

gqlgen does not autobind to database models that use types like sql.NullString or sql.NullInt32.

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

描述

I am using gqlgen v.0.17.31 (the latest) as of this writing.

I am using gqlgen and also working with sqlc (mysql) to generate database models which is autobinded in the gqlgen.yaml. However, after using gqlgen generate command, the schema.resolvers.go generates unwanted resolvers for my fk_dma_name, call_letters, and channel_num fields which are just basic database columns of types Int and String. Then in the graphiql playground, running a basic GET query (getDetails) for ChannelDetail, I am only able to get back the sk_chnl_details_id field only. When I try to query the fk_dma_name or call_letters or channel_num, it returns a message: internal system error.

my gqlgen schema.graphqls file:
```
type ChannelDetail {
sk_chnl_details_id: Int!
fk_dma_name: String,
call_letters: String,
channel_num: Int,
}

type Query {
getDetails: [ChannelDetail!]!
}
```

my sqlc (mysql) auto-generated models.go file:
```
type ChannelDetail struct {
SkChnlDetailsID int32 `db:"sk_chnl_details_id" json:"sk_chnl_details_id"`
FkDmaName sql.NullString `db:"fk_dma_name" json:"fk_dma_name"`
CallLetters sql.NullString `db:"call_letters" json:"call_letters"`
ChannelNum sql.NullInt32 `db:"channel_num" json:"channel_num"`
}
```

my gqlgen schema.resolvers.go file after I run the gqlgen generate command:

```
// However, I noticed that there is no resolver function created for the sk_chnl_details_id field weirdly

// FkDmaName is the resolver for the fk_dma_name field.
func (r *channelDetailResolver) FkDmaName(ctx context.Context, obj *sqlc.ChannelDetail) (*string, error) {
panic(fmt.Errorf("not implemented: FkDmaName - fk_dma_name"))
}

// CallLetters is the resolver for the call_letters field
func (r *channelDetailResolver) CallLetters(ctx context.Context, obj *sqlc.ChannelDetail) (*string, error) {
panic(fmt.Errorf("not implemented: CallLetters - call_letters"))
}

// ChannelNum is the resolver for the channel_num field.
func (r *channelDetailResolver) ChannelNum(ctx context.Context, obj *sqlc.ChannelDetail) (*int, error) {
panic(fmt.Errorf("not implemented: ChannelNum - channel_num"))
}

// GetDetails is the resolver for the getDetails field for type Query
func (r *queryResolver) GetDetails(ctx context.Context) ([]sqlc.ChannelDetail, error) {
*custom code here
}

-- I included the below default code to paint the whole picture but not necessary to see the problem is the above resolver functions being created for basic database fields or what I think is a problem
// ChannelDetail returns ChannelDetailResolver implementation.
func (r *Resolver) ChannelDetail() ChannelDetailResolver { return &channelDetailResolver{r} }

// Mutation returns MutationResolver implementation.
func (r *Resolver) Mutation() MutationResolver { return &mutationResolver{r} }

// Query returns QueryResolver implementation.
func (r *Resolver) Query() QueryResolver { return &queryResolver{r} }

type channelDetailResolver struct{ *Resolver }
type mutationResolver struct{ *Resolver }
type queryResolver struct{ *Resolver }
```

I was under the impression that gqlgen resolvers functions in schema.resolvers.go only were created for type query or mutation methods or forced explicit fields. Not for basic fields that are just database columns of type String and Int. Is there something I can do on my end to fix this? I only pasted the relevant code, but can provide more details.

Solved: inside the sqlc's generated database model, I had to override manually all the types that were using sql.* types i.e. sql.NullString or sql.NullInt32 to string or int32 respectively. Can we update gqlgen so that it recognizes types like sql.NullString or sql.NullInt32 when turning on the autobind in gqlgen.yaml

贡献指南

打开贡献指南

调研方向

查看 gqlgen 的代码生成逻辑,尤其是 gqlgen.yaml 配置中的 autobinding 功能。问题在于,来自 database/sql 包的 sql.NullString 和 sql.NullInt32 类型没有被识别为 nullable GraphQL String/Int 字段。首先检查 gqlgen 代码库中的类型映射系统,可能位于 internal/code 等目录中。确认在生成过程中 Go 类型如何映射到 GraphQL 类型。修复将涉及更新类型映射,以适当地处理 sql.Null* 类型,将它们视为 nullable GraphQL 标量。通过修改类型映射逻辑,并针对使用这些类型的测试 schema 运行 gqlgen generate 进行测试。

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

评估

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

把新 issue 发到你的邮箱

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