Skip to content

Using variables in a query on a sub graph causes an error #7937

@maartenkools

Description

@maartenkools

Product

Hot Chocolate

Version

14.3.0

Link to minimal reproduction

https://github.com/maartenkools/hc-fusion-duplicate-vars/tree/main

Steps to reproduce

Start the Server and Gateway projects from the sample repo, and execute the following query:

query getItems($groupId: String! = "groupId") {
  items(groupId: $groupId) {
    values(groupId: $groupId)
  }
}

The server has the following schema:

type Query {
    items(groupId: String): [Item!]!
}

type Item {
    values(groupId: String!): [String!]!
}

Key point is that groupId is optional for the items query, while it's mandatory for values.

What is expected?

The query executes successfully (in case of the repo, an empty array is returned)

What is actually happening?

An error occurs:

Using HC 13.9.7:

{
  "errors": [
    {
      "message": "An item with the same key has already been added. Key: groupId",
      "extensions": {
        "code": "HC0012"
      }
    }
  ]
}

Using HC 14.3.0 and up:

{
  "errors": [
    {
      "message": "Cannot return null for non-nullable field.",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "items"
      ],
      "extensions": {
        "code": "HC0018"
      }
    },
    {
      "message": "An item with the same key has already been added. Key: groupId",
      "extensions": {
        "code": "HC0012"
      }
    }
  ]
}

In both cases the following query plan is generated:

{
    "queryPlan": {
        "document": "query getItems($groupId: String! = \"groupId\") { items(groupId: $groupId) { values(groupId: $groupId) } }",
        "operation": "getItems",
        "rootNode": {
            "type": "Sequence",
            "nodes": [{
                    "type": "Resolve",
                    "subgraph": "Subgraph",
                    "document": "query getItems_1($groupId: String! = \"groupId\", $groupId: String = \"groupId\") { items(groupId: $groupId) { values(groupId: $groupId) } }",
                    "selectionSetId": 0,
                    "forwardedVariables": [{
                            "variable": "groupId"
                        }, {
                            "variable": "groupId"
                        }
                    ]
                }, {
                    "type": "Compose",
                    "selectionSetIds": [
                        0
                    ]
                }
            ]
        }
    }
}

Relevant log output

Additional context

Possibly related to #7365?

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions