我有这些实体:
- Golang结构:
type Player struct {
ID int
CreatedAt time.Time
City City
CityID int
Team *Team
TeamID *int
Score int
}
- GraphQL 架构:
type Player {
id: ID!
createdAt: Time!
City: City!
Team: Team
Score: Int!
}
如你看到的:
City
是强制性的Team
(TeamID
在 Golang 结构中)不是强制性的,所以我使用的是指针类型 (*
)
我现在不明白的是如何使用dataloaden
(一个 GraphQL 数据加载器)。
这里生成的CityById
加载器代码,生成go run github.com/vektah/dataloaden CityByIdLoader int *my_project/entities.City
:
CityById: CityByIdLoader{
maxBatch: 100,
wait: 1 * time.Millisecond,
fetch: func(keys []int) ([]*entities.City, []error) {
cities, err := myRepo.CitiesByKeys(keys)
// directly ordered in DB based on keys` order: it works
return cities, []error{err}
},
},
为什么[]*int
呢TeamID
?
TeamById
这是加载程序的生成代码,使用以下代码生成go run github.com/vektah/dataloaden TeamByIdLoader *int *my_project/entities.Team
::
TeamById: TeamByIdLoader{
maxBatch: 100,
wait: 1 * time.Millisecond,
fetch: func(keys []int) ([]*entities.Team, []error) {
teams, err := myRepo.TeamsByKeys(keys)
// I cannot order in DB because of `NULL` keys, right?
// How to order these results?
return teams, []error{err}
},
},
如果由于NULL
.keys