Using the AWESOME GraphQL Kotlin libraries ❤️
$ git clone https://github.com/rdbasu/demo.git
$ cd demo/
$ mvn clean install
$ mvn spring-boot:run
$ open http://localhost:8080/playground
☝️ will launch the GraphQL Playground
And you can start playing with your new Sample GraphQL Kotlin server 😍
++src
++main
++kotlin
++com.graphqlsummit.demo (this goes in application.properties, the packages which needs to be scanned)
++contexts
++enums (contains the enums)
++interfaces (contains the interfaces)
++models (contains the object types, input types, etc)
++mutations (contains the mutations)
++queries (contains the queries)
++DemoApplication.kt
++resources
++application.properties
In pom.xml (To use the graphql-kotlin-spring-server)
<dependency>
<groupId>com.expediagroup</groupId>
<artifactId>graphql-kotlin-spring-server</artifactId>
<version>1.2.3-SNAPSHOT</version>
</dependency>
graphql.packages=com.graphqlsummit.demo
An object type example code
data class Speaker(val name: String, val title: Title,
override val type: Type) : Attendee {
}
An ENUM example code
enum class Type {
SPEAKER,
GUEST,
ORGANIZER
}
A QUERY example code
Implement the MARKER INTERFACE Query
class SpeakerQuery : Query {
// A deprecated API, deprecated using Annotation: @Deprecated
@Deprecated(message = "Forget this one", replaceWith = ReplaceWith("latestAndGreatest"))
// Annotation @GraphQLDescription used to add description
@GraphQLDescription("Sample Query for the GraphQL Summit 2019")
fun getSpeaker(name: String): Speaker {
return Speaker("Rohit Basu", Title.ARCHITECT, Type.SPEAKER)
}
@GraphQLDescription("The Latest & The Greatest")
// Annotation @GraphQLName used to rename the API
@GraphQLName("SomeRandomName")
// Annotation @GraphQLIgnore used to ignore this API from the SCHEMA
@GraphQLIgnore
fun latestAndGreatest(name: String): Speaker {
return Speaker("New & Improved Rohit", Title.ARCHITECT, Type.GUEST)
}
// A List Example
@GraphQLName("Speakers")
fun listSpeakers(): List<Speaker> {
return listOf(Speaker("rohit basu", Title.ARCHITECT, Type.ORGANIZER),
Speaker("rohit not so basu", Title.SLACKER, Type.GUEST),
Speaker("rohit again basu", Title.SOFTWAREENGINEER, Type.SPEAKER))
}
// An example to show the usage of Custom Context
@GraphQLName("RandomCustomContext")
fun getCustomContext(@GraphQLContext context: CustomContext): Speaker? {
println(context.custom)
return null
}
}
A DEPRECATED QUERY example code
Annotation used @Deprecated
// A deprecated API, deprecated using Annotation: @Deprecated
@Deprecated(message = "Forget this one", replaceWith = ReplaceWith("latestAndGreatest"))
// Annotation @GraphQLDescription used to add description
@GraphQLDescription("Sample Query for the GraphQL Summit 2019")
fun getSpeaker(name: String): Speaker {
return Speaker("Rohit Basu", Title.ARCHITECT, Type.SPEAKER)
}
An example of Description code
Annotation used @GraphQLDescription
@GraphQLDescription("Sample Query for the GraphQL Summit 2019")
An example to IGNORE/exclude something from the SCHEMA code
Annotation used @GraphQLIgnore
@GraphQLIgnore
fun latestAndGreatest(name: String): Speaker {
return Speaker("New & Improved Rohit", Title.ARCHITECT, Type.GUEST)
}
An example to rename something code
Annotation used @GraphQLName
@GraphQLName("RandomCustomContext")
fun getCustomContext(@GraphQLContext context: CustomContext): Speaker? {
println(context.custom)
return null
}
Create Custom Context code
- Custom Context
data class CustomContext(val custom: String)
- Custom Context Factory
@Component
class CustomGraphQLContextFactory : GraphQLContextFactory<CustomContext> {
override suspend fun generateContext(request: ServerHttpRequest, response: ServerHttpResponse): CustomContext {
return CustomContext("Just some Random Context")
}
}
- Using the Custom Context
@GraphQLName("RandomCustomContext")
fun getCustomContext(@GraphQLContext context: CustomContext): Speaker? {
println(context.custom)
return null
}
A MUTATION example code
Implement the MARKER INTERFACE Mutation
class SpeakerMutation : Mutation {
@GraphQLDescription("A Sample Mutation")
fun addSpeaker(name: String, title: Title): Speaker {
println("the name: $name")
println("the title: $title")
return Speaker(name, title, Type.GUEST)
}
@GraphQLDescription("Adding Speaker as an Input Type")
fun addSpeakerAsInputType(speaker: Speaker): Speaker {
return Speaker(speaker.name, speaker.title, speaker.type)
}
}
This documentation (WIP) will be made better soon. ⛅