atulmy/gql-query-builder

Attempting multiple queries with the same variable name does not work as expected

aijorgenson opened this issue · 1 comments

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?

After struggling for a few I found the following solution in another issue:
#48 (comment)

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'