mirror of
https://github.com/atulmy/gql-query-builder.git
synced 2026-04-25 23:55:55 +03:00
[GH-ISSUE #16] How to merge queries? #107
Labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/gql-query-builder#107
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @dChunikhin on GitHub (Jul 2, 2019).
Original GitHub issue: https://github.com/atulmy/gql-query-builder/issues/16
Let's assume:
i have query:
and one more:
Everything is nice now.
But what if i need to get both user and profile in one query? What should I do? Is there mechanism to merge these queries. Especially, due to the fact, that one of the best features of GraphQL (comparing with REST) is to combine queries into one.
@atulmy commented on GitHub (Jul 2, 2019):
Do you mean following case:
@dChunikhin commented on GitHub (Jul 2, 2019):
Yes, thanks, it helped me. But there is an issue with 'variables' object: all of your queries have to contain variables. If there's no, "Variable "$uri" is not defined." will be as a response. Even if one of your queries doesn't need this variable. I found way to avoid this: place queries that don't need variables in the beginning of the list, and it will work fine. But it smells like hardcode.
@atulmy commented on GitHub (Jul 2, 2019):
Thanks, I'll look into it.
@dChunikhin commented on GitHub (Jul 3, 2019):
In addition:
output:
Notice, that variables has only one variable - idx (latest). I suppose, that merging operation of all variables of all quieries have to be here. For instance, Object.assign method gotta be used.
@dChunikhin commented on GitHub (Jul 4, 2019):
Bro, I've sorted out this issue in your code. But due to the fact I am in a scrum team, and my time runs off, I can't fix your lib and create a pull request. So, just listen if you are interested in the solution.
You used State Machine pattern (something like this). Your default adapter takes queries from its arguments, destructing them into three fields: operation, fields, variables. Then produces some actions (operationTemplate, queryDataNameAndArgumentMap) on them, relying on what these fields contain of. This way, your adapter builds first query. Then, it do the same with next query in the queries list (IQueryBuilderOptions[]). Each next query overrides fields (operation, fields, variables) by his own properties and it works well, but... When all queries were processed, general wrapping operation (operationWrapperTemplate) is involved. And it relies on these fields too, for creating the argument-list (query {...}) but there's no just list of all arguments, that all queries provide, but only one (the last), so it makes decision, that there's only one variable in the query, so return the incorrect query-string (query ($idx: Int) { user (id: $id) { name } gallery (idx: $idx) { id } }). The same problem occurs in the operation of returning a list of used variables (Utils.queryVariablesMap), so it returns only variables for the last query in the provided list (IQueryBuilderOptions[]), not for all of them, as I expect working with your lib. So, replace state machine with more common way: this.variables: from { queryVariables } to this.variables: [{ queryVariables }, { queryVariables }] and rely on its content to build query params, variables used in queries and it will be more clear, i think. If smth isn't understandable, lemme know.
@atulmy commented on GitHub (Jul 4, 2019):
I see, can you post the code snippet which resolves this issue? I can commit and publish the fixes.
@dChunikhin commented on GitHub (Jul 4, 2019):
It needs good solution, but it takes too much time for me. You have to change the flow of data in your adapter. Just build the object with all of queries we pass, and then transform it to the query-string. Not one-by-one query, as you do now.
You have created good product, so I hope you'll maintain then.
@toadkicker commented on GitHub (Jul 8, 2019):
@atulmy when I was refactoring the query generation code into the default adapter I had similar thinking. I think @dChunikhin is suggesting the functions in there not be dependent on one-another (e.g. don't call a function from another internal function)
The idea then is that all the functions must return a string, and that string is concatenated together by a single builder and pushed into the state of the class.
It would be a pretty intrusive refactor to do but it would make maintaining the adapter(s) much more sane. Maybe make a new module for helper methods that was started with the Utils class.
@Devorein commented on GitHub (Jun 29, 2020):
I've fixed the multiple query variables issue that @dChunikhin was talking about in my PR.