VerachadW / kraph

GraphQL request string builder written in Kotlin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fragments

eduardostuart opened this issue · comments

Is possible to build "fragments" with Kraph?

Example:

Fragment:

fragment something on SomethingXX {
   id
   name
}

Full query:

query queryname($input: QueryInput!) {
  queryname(input: $input) {
    message
    ... on SomeResult {
      somethinghere {
        ...something
      }
    }
  }
}

Well, you can do something like this.

Kraph {
        val userFragment: Kraph.FieldBuilder.() -> Unit = {
            field("name")
            field("avatar")
        }

        query {
            fieldObject("youtuber", builder = userFragment)
            fieldObject("subscriber", builder = userFragment)
        }
    }

Since Fragment is a set of fields, It would be best to declare as a variable and use in the query or fieldObject().

Thank you, @VerachadW .

And how can I build those inline fragments, with "... on" ?

It works! Thanks a lot @VerachadW .