https://kotlinlang.org logo
#kgraphql
Title
# kgraphql
a

Andres

03/14/2023, 4:44 PM
Potential silly question here.. Is there a way to determine what fields a user is requesting for their response? I am thinking I should be able to optimize some database queries if I know they only want specific fields in the response.
j

jeggy

03/14/2023, 9:58 PM
There is currently a hidden feature that allows this. You can provide
Execution.Node
to your arguments. Just like how
Context
is a special argument,
Execution.Node
as an argument will not be exposed to your schema, but KGraphQL will provide you with the node instance. You can take a look at this example: https://github.com/aPureBase/KGraphQL/blob/main/kgraphql/src/test/kotlin/com/apurebase/kgraphql/integration/QueryTest.kt#L314
n

Nahuel Ourthe

03/14/2023, 10:09 PM
Hi Andres, I care a lot about efficiency too... I resolved that in my domain with somethiing like this:
Copy code
class MyType
    query("myType") { resolver { -> MyType() } }
    type<MyType> {
        property("field1") {
            resolver { _ -> 
                "response to field1 from data base"
            }
        }
        property("field2") {
            resolver { _ ->
                "response to field2 from data base"
            }
        }
    }
So each resolver is executed only if the field is present in the query:
Copy code
myType {
  field1
}

myType {
  field2
}

myType {
  field1
  field2
}
This is more readable and easy, but with the secret feature that Jógvan Olsen says , you can do just one database request!
7 Views