Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin Apollo Client 4 GraphQL Tutorial

Kotlin Apollo Client 4 GraphQL Tutorial

Presentation video about GraphQL and the basics on how to use it. GraphQL can be an alternative to using REST and the Swagger OPEN API spec for our spring boot applications or other enterprise open source solutions we may find. With this document the idea is to make it clearer what are the possibilities.

Transcript

  1. What is GraphQL? • Query Language • Developed by Facebook

    2015 • Runtime Engine • Exact data requests • It is not REST • One single endpoint
  2. • 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
  3. What is Apollo Kotlin? • Complex Queries • Efficient Data

    Fetching • Client Flexibility • Evolving APIs • Spring GraphQL Integration
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. Setting up the guitar-client • In Gradle • apollo-runtime -

    Enable runtime to create object • com.apollographql.apollo - Configure apollo plugin
  13. 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
  14. 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