All Projects → ermadmi78 → kobby

ermadmi78 / kobby

Licence: Apache-2.0 license
Kobby is a codegen plugin of Kotlin DSL Client by GraphQL schema. The generated DSL supports execution of complex GraphQL queries, mutation and subscriptions in Kotlin with syntax similar to native GraphQL syntax.

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to kobby

native-build-tools
Native-image plugins for various build tools
Stars: ✭ 168 (+223.08%)
Mutual labels:  maven, maven-plugin, gradle-plugin
dmn-check
A tool which performs static analyses on Decision Model Notation (DMN) files to detect bugs
Stars: ✭ 34 (-34.62%)
Mutual labels:  maven, maven-plugin, gradle-plugin
gradle-git-versioning-plugin
This extension will set project version, based on current Git branch or tag.
Stars: ✭ 44 (-15.38%)
Mutual labels:  maven, maven-plugin, gradle-plugin
Javapackager
📦 Gradle/Maven plugin to package Java applications as native Windows, Mac OS X, or GNU/Linux executables and create installers for them.
Stars: ✭ 285 (+448.08%)
Mutual labels:  maven, maven-plugin, gradle-plugin
Jib
🏗 Build container images for your Java applications.
Stars: ✭ 11,370 (+21765.38%)
Mutual labels:  maven, maven-plugin, gradle-plugin
36 Graphql Concepts
📜 36 concepts every GraphQL developer should know.
Stars: ✭ 209 (+301.92%)
Mutual labels:  graphql-client, graphql-schema, graphql-subscriptions
Maven Git Versioning Extension
This extension will virtually set project versions, based on current git branch or tag.
Stars: ✭ 178 (+242.31%)
Mutual labels:  maven, maven-plugin, gradle-plugin
RapidMavenPushPlugin
A Gradle plugin : Upload Artifacts to Multi Maven Repository
Stars: ✭ 21 (-59.62%)
Mutual labels:  maven, maven-plugin, gradle-plugin
mvn-jlink
Maven plugin to provide way to work with jlink tool in maven projects
Stars: ✭ 34 (-34.62%)
Mutual labels:  maven, maven-plugin
plexus-compiler
Plexus compiler a layer on top of compilers and used by maven-compiler-plugin
Stars: ✭ 24 (-53.85%)
Mutual labels:  maven, maven-plugin
dependency-update-maven-plugin
A Maven plugin that creates merge requests for dependency updates.
Stars: ✭ 23 (-55.77%)
Mutual labels:  maven, maven-plugin
gatling-maven-plugin
Gatling Plugin for Maven
Stars: ✭ 30 (-42.31%)
Mutual labels:  maven, maven-plugin
snyk-maven-plugin
Test and monitor your projects for vulnerabilities with Maven. This plugin is officially maintained by Snyk.
Stars: ✭ 64 (+23.08%)
Mutual labels:  maven, maven-plugin
macosappbundler-maven-plugin
Maven plugin for creating a native macOS bundle containing all dependencies required by a Maven project
Stars: ✭ 35 (-32.69%)
Mutual labels:  maven, maven-plugin
jcabi-mysql-maven-plugin
MySQL Maven Plugin: starts MySQL server on pre-integration phase and shuts it down on post-integration phase
Stars: ✭ 34 (-34.62%)
Mutual labels:  maven, maven-plugin
boost
Boost Maven and Gradle plugins for MicroProfile development
Stars: ✭ 27 (-48.08%)
Mutual labels:  maven-plugin, gradle-plugin
mvn scalafmt
Scalafmt plugin for maven
Stars: ✭ 14 (-73.08%)
Mutual labels:  maven, maven-plugin
ktlint-maven-plugin
Maven plugin for ktlint the Kotlin linter
Stars: ✭ 42 (-19.23%)
Mutual labels:  maven, maven-plugin
mosec-maven-plugin
用于检测maven项目的第三方依赖组件是否存在安全漏洞。
Stars: ✭ 85 (+63.46%)
Mutual labels:  maven, maven-plugin
restdocs-spec
A maven plugin for generating Open API and Postman Collection specifications using Spring Restdocs.
Stars: ✭ 43 (-17.31%)
Mutual labels:  maven, maven-plugin

License Plugin Portal Maven Central Discussions

alt text

Kobby is a codegen plugin of Kotlin DSL Client by GraphQL schema. The generated DSL supports execution of complex GraphQL queries, mutations and subscriptions in Kotlin with syntax similar to native GraphQL syntax. Moreover, you can customize generated DSL by means of GraphQL schema directives and Kotlin extension functions.

Documentation

Tutorials

Requirements

  • Gradle at least version 7.0 is required.
  • Maven at least version 3.6.3 is required.
  • Kotlin at least version 1.5 is required to compile generated client DSL.
  • 1.5.0 <= Ktor version < 2.0.0 is required to generate default adapters.

Contributing

Please see CONTRIBUTING.md

Gradle

plugins {
    id("io.github.ermadmi78.kobby") version "1.6.0"
}

Maven

<build>
    <plugins>
        <plugin>
            <groupId>io.github.ermadmi78</groupId>
            <artifactId>kobby-maven-plugin</artifactId>
            <version>1.6.0</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>generate-kotlin</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Define your GraphQL schema

Put your GraphQL schema file in the project resources with graphqls extension. For example, let define cinema.graphqls schema file and put it in src/main/resources/io/github/ermadmi78/kobby/cinema/api/

Configure Kobby Gradle plugin

  • Add Kobby plugin to your build.gradle.kts, to generate Kotlin DSL.
  • Configure Kotlin data types for scalars, defined in the GraphQL schema (more details about the scalar mapping see here).
  • Add Jackson dependency to generate Jackson annotations for DTO classes.
  • Add Kotlin plugin to your build.gradle.kts to compile generated DSL.
import io.github.ermadmi78.kobby.kobby

description = "Cinema API Example"

plugins {
    kotlin("jvm")
    `java-library`
    id("io.github.ermadmi78.kobby")
}

kobby {
    kotlin {
        scalars = mapOf(
            "Date" to typeOf("java.time", "LocalDate"),
            "JSON" to typeMap.parameterize(typeString, typeAny.nullable())
        )
    }
}

val jacksonVersion: String by project
dependencies {
    compileOnly("com.fasterxml.jackson.core:jackson-annotations:$jacksonVersion")
}

Generate Kotlin DSL Client by your GraphQL schema

Execute gradle build command to generate Kotlin DSL Client. The entry point of the DSL will be placed in the cinema.kt file (name of the DSL entry point file is the same as the name of GraphQL schema file):

alt text

More details about the entry point configuration see here.

Instantiate DSL Context

The cinema.kt file will contain the cinemaContextOf builder function, which creates instance of the CinemaContext interface - the entry point of the generated DSL. Note that the prefixes of the builder function, adapter and context interfaces are the same as the name of the GraphQL schema file.

fun cinemaContextOf(adapter: CinemaAdapter): CinemaContext = CinemaContextImpl(adapter)

More details about the DSL context see here

We have to pass instance of the CinemaAdapter interface to the cinemaContextOf function to create instance of the CinemaContext interface. By default, Kobby does not generate any adapter implementations. But we can ask Kobby to generate default Ktor implementations of the CinemaAdapter interface.

To do this just add io.ktor:ktor-client-cio dependency to you project:

val jacksonVersion: String by project
val ktorVersion: String by project
dependencies {
    // Add this dependency to enable Jackson annotation generation in DTO classes by Kobby
    compileOnly("com.fasterxml.jackson.core:jackson-annotations:$jacksonVersion")

    // Add this dependency to enable default Ktor adapters generation
    compileOnly("io.ktor:ktor-client-cio:$ktorVersion")
}

Rebuild the project, and Kobby will generate classes CinemaSimpleKtorAdapter and CinemaCompositeKtorAdapter in subpackage adapter.ktor. The prefix <Cinema> by default is the schema name.

The CinemaSimpleKtorAdapter is simple to configure, but it does not support GraphQL subscriptions - so we can use only queries and mutations:

val client = HttpClient {
    install(JsonFeature) {
        serializer = JacksonSerializer {
            registerModule(ParameterNamesModule(JsonCreator.Mode.PROPERTIES))
            registerModule(JavaTimeModule())
            // Force Jackson to serialize dates as String
            disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
        }
    }
}

val context = cinemaContextOf(CinemaSimpleKtorAdapter(client, "http://localhost:8080/graphql"))

The CinemaCompositeKtorAdapter is more difficult to configure, but it supports all types of GraphQL operations, including subscriptions:

val client = HttpClient {
    install(WebSockets)
}

val mapper = jacksonObjectMapper()
    .registerModule(ParameterNamesModule(JsonCreator.Mode.PROPERTIES))
    .registerModule(JavaTimeModule())
    // Force Jackson to serialize dates as String
    .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)

val context = cinemaContextOf(
    CinemaCompositeKtorAdapter(
        client,
        "http://localhost:8080/graphql",
        "ws://localhost:8080/subscriptions",
        object : CinemaMapper {
            override fun serialize(value: Any): String =
                mapper.writeValueAsString(value)

            override fun <T : Any> deserialize(content: String, contentType: KClass<T>): T =
                mapper.readValue(content, contentType.java)
        }
    )
)

See here full example of adapters configuration. Note that you are not required to use the default adapters. You can write your own adapter implementation. For example, see here adapter implementation for Spring Boot integration tests.

Ok, we are ready to execute GraphQL queries by means of generated Kotlin DSL Client

Simple query

alt text

You can see more details about GraphQL queries support here

Simple mutation

alt text

Simple subscription

alt text

You can see more details about GraphQL subscriptions support here

We can download a complex graph of objects by means of our Kotlin DSL

alt text

GraphQL's unions and interfaces are supported too

alt text

You can see more details about GraphQL abstract data types support here

License

Apache License Version 2.0

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].