Incorrect results when querying 1 type and 2 interfaces using different selection sets
- Ngôn ngữ chính
- Go
- Star
- 10.8k
- Fork
- 1.3k
- Merge trung bình
- 2 ngày 36 phút
- Pull request đã merge (30 ngày)
- 26
Mô tả
## Describe the bug
In a system with: 1 type (CompactPost) and 2 interfaces (PostInterface1, PostInterface2), when requesting different selections sets from the types/interfaces, the output differs depending on which selection set requests all fields or fewer fields.
## To Reproduce
A repository with a reproduction is here:
https://github.com/montykamath/gqlgen-bug-repro-1
1. Clone the repository.
2. Start the gql server by `cd b; make run`
3. Open the router sandbox url that shows in the terminal like http://127.0.0.1:8071/
4. Run the BadQuery.graphql or GoodQuery.graphql to see the differences in output
5. Place a breakpoint in 99designs/gqlgen@v0.17.70/graphql/handler/transport/util.go in writeJson to see the output before it is written. It has duplicate keys for awards before the response is written. After the response is written it takes the last of those duplicate keys.
## Expected behavior
Expect the output to be a deep merge of the selection sets into a single response list, rather than allowing the JSON writer to select the last of the duplicate keys.
## The working query
```
# This query fetches posts and their associated awards.
# It includes inline fragments to handle different post types/interfaces
# It only works because it requests the same fields in all fragments for awards
query GoodQuery {
posts {
__typename
awards {
__typename
id
name
}
... on CompactPost {
__typename
id
awards {
__typename
id
name
}
}
... on PostInterface2 {
__typename
id
awards {
__typename
name
id
}
}
}
}
```
#### Working query produces this output
```
{
"data": {
"posts": [
{
"__typename": "CompactPost",
"awards": [
{
"__typename": "Award",
"id": "1",
"name": "One"
},
{
"__typename": "Award",
"id": "2",
"name": "Two"
},
{
"__typename": "Award",
"id": "3",
"name": "Three"
}
],
"id": "1"
}
]
}
}
```
## The failing query
```
# This query is identical to the good query except that it comments out the name and id from the PostInterface2 fragment for Award
# This query fetches posts and their associated awards.
# It includes inline fragments to handle different post types/interfaces
# It does not work because it requests different fields in some of the fragments for awards
query BadQuery {
posts {
__typename
awards {
__typename
id
name
}
... on CompactPost {
__typename
id
awards {
__typename
id
name
}
}
... on PostInterface2 {
__typename
id
awards {
__typename
# name
# id
}
}
}
}
```
### Failing query produces this output
```
{
"data": {
"posts": [
{
"__typename": "CompactPost",
"awards": [
{
"__typename": "Award"
},
{
"__typename": "Award"
},
{
"__typename": "Award"
}
],
"id": "1"
}
]
}
}
```
Hướng dẫn đóng góp
Hướng nghiên cứu
Bug nằm trong logic hợp nhất response GraphQL khi các selection sets khác nhau được sử dụng giữa các fragment khác nhau. Hãy bắt đầu bằng cách kiểm tra repository tái hiện tại https://github.com/montykamath/gqlgen-bug-repro-1. Xem file handler/transport/util.go được đề cập, cụ thể là function writeJson, để thấy các key bị trùng. Hãy tìm hiểu cách gqlgen hợp nhất các selection sets cho interface và type. Bản sửa có thể liên quan đến việc thu thập field của execution layer hoặc định dạng response để thực hiện deep merge thay vì cho phép các key JSON bị trùng.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- go, graphql
- Lĩnh vực
- api, backend
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 45/100