VerachadW / kraph

GraphQL request string builder written in Kotlin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use "variables" ?

eduardostuart opened this issue · comments

Hi,

How can I set "variables" in a query?

For example, if I have the query/mutation:

val args = mutableMapOf<String, Any>()
args.put("language", "en-us")

return Kraph {
    mutation("settings") {
        func("settings", args) {
            field("language")
        }
    }
}

This will result in:

{
   "query":"mutation settings {\nlanguage(language: \"en-us\")\n}",
   "variables":null,
   "operationName":"settings"
}

But, how do I put "variables"? Like this:

{
   "query":"mutation settings($settings: SettingsInput!) {\n  settings(input: $settings) {\n    language\n     }\n}\n",
   "variables":{
      "settings":{
         "language":"en-us"
      }
   },
   "operationName":"settings"
}

Kraph did not support to variables for now. I can add it to the plan but may not be in the near future since I want to migrate to Kotlin v1.1.1 first.

@eduardostuart Do you still need of variable? If yes, could you please specify which case do you really need it?

Great lib! Thanks for the work you've put in!

GraphQL supports the following: query, variables, and operationName as parameters passed in.
For example the following request string can be rewritten from:
query{ human(id: "1000") { name height } }

to the following with the usage of Variables section of the GraphQL request:
query($humanId: ID!) { human(id: $humanId) { ... } }
So as you are writing your queries they become more robust. This is especially useful when doing mutation queries with long JSON objects being passed in and makes debugging a lot easier, because in your variables section you will just say
{ $humanId: "some value" }

Now operationName allows devs to just write one query and in it specify which of the subqueries should be executed by the Graph engine as following:
query getHumanInfo($humanId: ID!) { ... } query getRobotInfo($robotId: ID!) { ... } query getDeathStarFloorInfo($floorNumber: ID!) { ...}

In the parameters section of the request I can specify that I only want the getRobotInfo to run

@arybin I see. Thanks for the info. From your comments, It seems we have 2 things to do.
Multiple subqueries and support for variables in Kraph. I will look more into this and come back with ideas later

@arybin @eduardostuart I open 2 issues for both topics. Feel free to continue discuss on those issues. I will close this one.