[GH-ISSUE #61] Attempting multiple queries with the same variable name does not work as expected #39

Closed
opened 2026-03-03 00:02:54 +03:00 by kerem · 1 comment
Owner

Originally created by @aijorgenson on GitHub (Sep 10, 2021).
Original GitHub issue: https://github.com/atulmy/gql-query-builder/issues/61

I'm attempting a fairly simple multi-query where I want to retrieve multiple counts from a single request with varying values. (i.e. get the count of fruit for several different statuses)

Given the example query below:

[
    {
        "operation": "statusId1: fruit",
        "variables": {
            "fruit_id": {
                "required": true,
                "value": 36
            },
            "where": {
                "type": "QueryFruitWhereConditions",
                "value": {
                    "AND": [
                        {
                            "column": "status_id",
                            "operator": "EQ",
                            "value": 1
                        }
                    ]
                }
            }
        },
        "fields": [
            {
                "meta": [
                    "total"
                ]
            }
        ]
    },
    {
        "operation": "statusId2: fruit",
        "variables": {
            "fruit_id": {
                "required": true,
                "value": 36
            },
            "where": {
                "type": "QueryFruitWhereConditions",
                "value": {
                    "AND": [
                        {
                            "column": "status_id",
                            "operator": "EQ",
                            "value": 2
                        }
                    ]
                }
            }
        },
        "fields": [
            {
                "meta": [
                    "total"
                ]
            }
        ]
    }
]

The following query will be built out:

{
    "query": "query ($fruit_id: Int!, $where: QueryFruitWhereConditions) { statusId1: fruit (fruit_id: $fruit_id, where: $where) { meta { total } } statusId2: fruit (fruit_id: $fruit_id, where: $where) { meta { total } } }",
    "variables": {
        "fruit_id": 36,
        "where": {
            "AND": [
                {
                    "column": "status_id",
                    "operator": "EQ",
                    "value": 2
                }
            ]
        }
    }
}

This is an issue because the where variable is intentionally different between queries; however, the last query's value for the variable is used for the entire query set. Is this a bug or is there some sort of workaround I could do?

Originally created by @aijorgenson on GitHub (Sep 10, 2021). Original GitHub issue: https://github.com/atulmy/gql-query-builder/issues/61 I'm attempting a fairly simple multi-query where I want to retrieve multiple counts from a single request with varying values. (i.e. get the count of fruit for several different statuses) Given the example query below: ```javascript [ { "operation": "statusId1: fruit", "variables": { "fruit_id": { "required": true, "value": 36 }, "where": { "type": "QueryFruitWhereConditions", "value": { "AND": [ { "column": "status_id", "operator": "EQ", "value": 1 } ] } } }, "fields": [ { "meta": [ "total" ] } ] }, { "operation": "statusId2: fruit", "variables": { "fruit_id": { "required": true, "value": 36 }, "where": { "type": "QueryFruitWhereConditions", "value": { "AND": [ { "column": "status_id", "operator": "EQ", "value": 2 } ] } } }, "fields": [ { "meta": [ "total" ] } ] } ] ``` The following query will be built out: ```javascript { "query": "query ($fruit_id: Int!, $where: QueryFruitWhereConditions) { statusId1: fruit (fruit_id: $fruit_id, where: $where) { meta { total } } statusId2: fruit (fruit_id: $fruit_id, where: $where) { meta { total } } }", "variables": { "fruit_id": 36, "where": { "AND": [ { "column": "status_id", "operator": "EQ", "value": 2 } ] } } } ``` This is an issue because the `where` variable is intentionally different between queries; however, the last query's value for the variable is used for the entire query set. Is this a bug or is there some sort of workaround I could do?
kerem closed this issue 2026-03-03 00:02:54 +03:00
Author
Owner

@aijorgenson commented on GitHub (Sep 10, 2021):

After struggling for a few I found the following solution in another issue:
https://github.com/atulmy/gql-query-builder/issues/48#issuecomment-762530333

The solution was to call each variable a unique name, i.e. where_1 and where_2, then inside of the variable define name: 'where'

<!-- gh-comment-id:917175674 --> @aijorgenson commented on GitHub (Sep 10, 2021): After struggling for a few I found the following solution in another issue: https://github.com/atulmy/gql-query-builder/issues/48#issuecomment-762530333 The solution was to call each variable a unique name, i.e. `where_1` and `where_2`, then inside of the variable define `name: 'where'`
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/gql-query-builder#39
No description provided.