Slide 1

Slide 1 text

João Esperancinha Apollo 4 Kotlin Made me GraphQL 2024/11/12

Slide 2

Slide 2 text

Gnothi seauton Know thyself What components make up this talk?

Slide 3

Slide 3 text

What is GraphQL? ● Query Language ● Developed by Facebook 2015 ● Runtime Engine ● Exact data requests ● It is not REST ● One single endpoint

Slide 4

Slide 4 text

● Overfetching and underfetching of data What does GraphQL solves? ● Multiple endpoints in general but particularly for the same resource ● Requirement Changes ● Lack of typed contracts ● Inconsistent and redundant API Documentation

Slide 5

Slide 5 text

What is Apollo Kotlin? ● Complex Queries ● Efficient Data Fetching ● Client Flexibility ● Evolving APIs ● Spring GraphQL Integration

Slide 6

Slide 6 text

What does Apollo Kotlin solve? ● Code generation ● Type-Safe Queries and Responses ● Optimized Caching and Offline Support ● Coroutines and Asynchronous Operations ● Flexible Networking Integration ● Error Handling ● Server-Side Usage ● Integration with Other Apollo Tools ● Efficient communication with GraphQL APIs

Slide 7

Slide 7 text

What is Spring GraphQL? ● Efficient Data Fetching with Batch Loading ● Unified API endpoint ● Data Fetching Flexibility ● GraphQL Annotations ● Schema driven development ● GraphiQL and Testing ● Security and Access Control

Slide 8

Slide 8 text

What does Spring GraphQL solve? ● Overfetching and underfetching of data ● Consolidates all APIs into a single endpoint ● Improving Data Fetching Efficiency ● Enforcing a Clear Schema for API Interactions ● Reducing Redundant API Calls

Slide 9

Slide 9 text

What does Spring GraphQL solve? ● Real-Time Data with Subscriptions ● Enhanced Error Management and Validation ● Fine-Grained Access Control ● Developer Productivity with Tooling ● Smooth Integration with the Spring Ecosystem

Slide 10

Slide 10 text

Kairos Know the right moment How does the system look like?

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Meden agan Nothing in excess How to program it?

Slide 13

Slide 13 text

Setting up the guitar-service ● spring-boot-starter-webflux - Make it reactive ● spring-boot-starter-graphql - Enable GraphQL ● kotlin-stdlib - Enable Kotlin ● kotlin-reflect - To enable necessary reflection features ● spring-data-commons - Required for the data structures ● kotlinx-coroutines-core - Required coroutine libraries ● kotlinx-coroutines-reactor - Reactive services engine

Slide 14

Slide 14 text

Creating the guitar service type Query { guitarById(id: ID): GuitarDto ownerById(id: ID): OwnerDto } type GuitarDto { id: ID brand: String model: String year: Int owner: OwnerDto } type OwnerDto { id: ID firstName: String lastName: String } schema.graphqls - By default, this file is kept in the resources folder. Spring GraphQL knows to find it there

Slide 15

Slide 15 text

Creating the guitar-service spring.graphql.graphiql.enabled=true spring.main.web-application-type=reactive application.properties @Controller class OwnerController( val ownerService: OwnerService ) { @QueryMapping suspend fun ownerById(@Argument("id") id: Long): OwnerDto? = ownerService.getByIdOrNull(id) } @Controller @QueryMapping @Argument GraphQL still follows the MVC pattern QueryMapping maps the method to the definition Argument maps the input argument of the method

Slide 16

Slide 16 text

Creating the guitar-service spring.graphql.graphiql.enabled=true spring.main.web-application-type=reactive @Controller class GuitarController( val guitarService: GuitarService, val ownerService: OwnerService ) { @QueryMapping suspend fun guitarById(@Argument("id") id: Long) = guitarService.getByIdOrNull(id) @SchemaMapping suspend fun owner(guitarDto: GuitarDto): OwnerDto? = ownerService.getByIdOrNull(guitarDto.ownerId) } @SchemaMapping application.properties SchemaMapping maps parts of the parent object with its child objects

Slide 17

Slide 17 text

GraphiQL - Interface http://localhost:8080/graphiql?path=/graphql

Slide 18

Slide 18 text

Setting up the guitar-client ● In Gradle ● apollo-runtime - Enable runtime to create object ● com.apollographql.apollo - Configure apollo plugin

Slide 19

Slide 19 text

apollo { service("service") { packageName.set("org.jesperancinha.guitar.gui") introspection { endpointUrl.set("http://localhost:8080/graphql") schemaFile.set(file("src/main/graphql/schema.graphqls")) } } } Setting up the guitar-client in Gradle packageName is the package name of the created classes endpointUrl is the address where we can find our graphql definition schemaFile is where we want the fetched definition to be saved to schemaFile packageName endpointUrl

Slide 20

Slide 20 text

DEMO Time!

Slide 21

Slide 21 text

Questions? I am an inquisitive cat

Slide 22

Slide 22 text

Resources ● https://spring.io/guides/gs/graphql-server ● https://www.apollographql.com/docs/kotlin/tutorial/04-execute-the-query ● https://plugins.jetbrains.com/plugin/8097-graphql

Slide 23

Slide 23 text

Source code https://github.com/jesperancinha/guitar-shop

Slide 24

Slide 24 text

About me ● Homepage - https://joaofilipesabinoesperancinha.nl ● Threads - https://www.threads.net/@joaofisaes ● LinkedIn - https://www.linkedin.com/in/joaoesperancinha/ ● YouTube - https://www.youtube.com/@jesprotech ● Bluesky - https://bsky.app/profile/jesperancinha.bsky.social ● Mastodon - https://masto.ai/@jesperancinha ● GitHub - https://github.com/jesperancinha ● Hackernoon - https://hackernoon.com/u/jesperancinha ● DevTO - https://dev.to/jofisaes ● Medium - https://medium.com/@jofisaes

Slide 25

Slide 25 text

Thank you! See you next time!