Graphecule

Graphecule is a GraphQL client library which can auto generate client Kotlin code that matches the entire graph model represented by a GraphQL server schema.

The generated client Kotlin code can then be used like a normal Kotlin library performing all the query/mutation operations exposed by the original GraphQL API. All these operations can be done as if calling a normal Kotlin call, without having to know anything about underlying GraphQL or Networking.

Why Graphecule?

Here is an example of querying for user name from the auto generated client code for GitHub GraphQL API

        val gitHubQuery = Query.QueryRequest
            .Builder()
            //Fetch the user
            .fetchUser(
                User.UserRequest
                    .Builder()
                    .fetchName()      //Fetch name
                    .fetchLocation()  //Fetch location
                    .build(),
                "josesamuel"      //Arg for user query
            )
            
            .build()       //Builds the request
            .sendRequest() //Non blocking suspended call to send the request, that returns the parsed Query type instance

        //Directly access the fields requested
        println(gitHubQuery?.user?.name)

The auto generated code above shows that you can

Generating Client model

        val clientBuilder = GraphClientBuilder(
            graphServer,             // GraphQL server address
            outputLocation,          // Where to generate the client code
            clientLibraryPackageName // Package name under which to generate client
        )

        //Builds the client library
        clientBuilder.buildGraphClient()
        val clientBuilder = GraphClientBuilder(
            graphServer,             // GraphQL server address
            outputLocation,          // Where to generate the client code
            clientLibraryPackageName, // Package name under which to generate client
            //Specify any headers to be send.
            //Can also specify OkHttpClient to add interceptors
            HttpRequestAdapter(mapOf("authorization" to "Bearer $gitHubKey"))
        )

Perform Query or Set operations

Getting Graphecule

Gradle dependency

dependencies {
    implementation 'com.josesamuel:graphecule:1.0.0'
}

License

Copyright 2020 Joseph Samuel

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.