Skip to content

Maven GraphQL Scalar Mapping

dermakov edited this page Apr 29, 2023 · 4 revisions

To generate client DSL from our GraphQL schema, we have to provide mapping of scalars, defined in the schema, to Kotlin data types.

Default mapping

Kobby provides default mapping for some scalars:

GraphQL Scalar Kotlin Type
ID kotlin.Long
Int kotlin.Int
Long kotlin.Long
Float kotlin.Double
Double kotlin.Double
String kotlin.String
Boolean kotlin.Boolean

Custom mapping

You can configure your own mapping or override default mapping by means of kotlin.scalars section of plugin execution configuration.

The example below maps Date scalar to java.time.LocalDate class and JSON scalar to Map<String, Any?> class.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <build>
        <plugins>
            <plugin>
                <groupId>io.github.ermadmi78</groupId>
                <artifactId>kobby-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate-kotlin</goal>
                        </goals>
                        <configuration>
                            <kotlin>
                                <!-- Mapping GraphQL scalars to Kotlin classes -->
                                <scalars>
                                    <!-- GraphQL scalar name-->
                                    <Date>
                                        <!-- Kotlin package name-->
                                        <packageName>java.time</packageName>

                                        <!-- Kotlin class name-->
                                        <className>LocalDate</className>
                                    </Date>

                                    <!-- GraphQL scalar name-->
                                    <JSON>
                                        <!-- Kotlin package name-->
                                        <packageName>kotlin.collections</packageName>

                                        <!-- Kotlin class name-->
                                        <className>Map</className>

                                        <!-- List of Kotlin class generics -->
                                        <generics>
                                            <generic>
                                                <!-- Generic package name -->
                                                <packageName>kotlin</packageName>

                                                <!-- Generic class name -->
                                                <className>String</className>
                                            </generic>
                                            <generic>
                                                <!-- Generic package name -->
                                                <packageName>kotlin</packageName>

                                                <!-- Generic class name -->
                                                <className>Any</className>

                                                <!-- Is generic nullable. By default is `false` -->
                                                <nullable>true</nullable>
                                            </generic>
                                        </generics>

                                        <!-- Example of Kotlinx Serializer configuration -->
                                        <serializer>
                                            <!-- Serializer package name -->
                                            <packageName>my.serializer</packageName>

                                            <!-- Serializer class name -->
                                            <className>MySerializer</className>
                                        </serializer>
                                    </JSON>
                                </scalars>
                            </kotlin>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>