99designs / 99designs/gqlgen

Generation fails for structs with fields to slice pointer types

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

描述

### What happened?

#### Compilation error
Validation after generation fails. Marshal func has *v but object method has no pointer on the object.

```Error: validation failed: packages.Load: …/graphql/generated.go:2288:40: cannot use v (variable of type *domain.AppliedDiscounts) as domain.AppliedDiscounts value in argument to ec._AppliedDiscounts```

#### Observation
For the current master version:
This happens only for the field `Discounts *AppliedDiscounts` with an pointer to slice type.
Field `Test *string` works fine.

Generation of this case worked at least in v0.9.0.

### What did you expect?

Validation after generation succeeds.

### Minimal graphql.schema and models to reproduce

```
type (
Cart struct {
Discounts *AppliedDiscounts
Test *string
}

AppliedDiscounts []string
)

func (d *AppliedDiscounts) Get() string {
return "42"
}
```

```
type Cart {
discounts: AppliedDiscounts
test: String!
}

type AppliedDiscounts {
get: String!
}
````

### versions
- gqlgen version: `master`
- go version: `1.14.1`
- dep or go modules? `go modules`

贡献指南

打开贡献指南

调研方向

问题出在 GraphQL 类型的代码生成上:这些类型是字段为指向 slice 类型的指针的 Go struct。查看生成的文件 graphql/generated.go 中 2288 行附近验证错误发生的位置。检查 AppliedDiscounts 类型的 marshal 和 unmarshal 函数。对比 *string 之类的指针字段是如何处理的。修复可能需要调整代码生成器中的类型转换逻辑,可能是在负责将 Go 类型映射到 GraphQL 的 package 中。

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

评估

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

把新 issue 发到你的邮箱

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