99designs / 99designs/gqlgen

Incorrect results when querying 1 type and 2 interfaces using different selection sets

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

描述

## 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"
}
]
}
}
```

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

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