Graphecule Common

Graphecule generates GraphQl client code for a given GraphQl server.

License

License

GroupId

GroupId

com.josesamuel
ArtifactId

ArtifactId

graphecule-common
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

Graphecule Common
Graphecule generates GraphQl client code for a given GraphQl server.
Project URL

Project URL

https://github.com/josesamuel/graphecule/
Source Code Management

Source Code Management

https://github.com/josesamuel/graphecule/

Download graphecule-common

How to add to project

<!-- https://jarcasting.com/artifacts/com.josesamuel/graphecule-common/ -->
<dependency>
    <groupId>com.josesamuel</groupId>
    <artifactId>graphecule-common</artifactId>
    <version>1.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.josesamuel/graphecule-common/
implementation 'com.josesamuel:graphecule-common:1.0.1'
// https://jarcasting.com/artifacts/com.josesamuel/graphecule-common/
implementation ("com.josesamuel:graphecule-common:1.0.1")
'com.josesamuel:graphecule-common:jar:1.0.1'
<dependency org="com.josesamuel" name="graphecule-common" rev="1.0.1">
  <artifact name="graphecule-common" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.josesamuel', module='graphecule-common', version='1.0.1')
)
libraryDependencies += "com.josesamuel" % "graphecule-common" % "1.0.1"
[com.josesamuel/graphecule-common "1.0.1"]

Dependencies

compile (2)

Group / Artifact Type Version
com.squareup.okhttp3 : okhttp jar 4.5.0
com.google.code.gson : gson jar 2.8.6

runtime (2)

Group / Artifact Type Version
org.jetbrains.kotlin : kotlin-stdlib-jdk7 jar 1.4.0
org.jetbrains.kotlinx : kotlinx-coroutines-core jar 1.3.3

Project Modules

There are no modules declared in this project.

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?

  • Auto generates entire GraphQL client model in Kotlin
  • Perform any Query or Mutation using auto generated Kotlin classes
  • Hides all complexities of GraphQL and networking. Use the auto generated classes like any plain Kotlin library.
  • Just like GraphQL, the auto generated Kotlin client library will allow you to specify what exactly needs to be fetched.

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

  • Specify exactly what all fields needs to be "fetched"
  • Build the request that exactly match what the GraphQL schema required, by providing all the arguments
  • Call the API without having to know anything about GraphQL
  • Client library internally takes care of GraphQL and network requests, sending the correct requests, and parsing the reply and giving the parsed result back as Kotlin Object instance

Generating Client model

  • Using the Jar
  • Programmatically
    • Include the dependencies for library in gradle (as given below)
    • Build the client code using "GraphClientBuilder"
        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()
  • For authentication, optionally specify "HttpRequestAdapter"
        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

  • Use the inner XXXRequest.Builder class to generate the query or set requests.
    • For Query class, the builder exposes "fetch" methods
    • For Mutation class, the builder exposes "invoke" methods, or use the direct invoke methods on the generated class that match the Mutation type

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.

Versions

Version
1.0.1
1.0.0