Spring GraphQL

VMware Tanzu
VMware TanzuVMware Tanzu
Andreas Marek
Rossen Stoyanchev
Spring GraphQL
Copyright © 2021 VMware, Inc. or its affiliates.
Who are we
Andreas Marek
● GraphQL Java creator and maintainer
● GraphQL contributor and Technical Steering
Committee member
● Spring GraphQL co-creator
● Working at Atlassian, Sydney
● @andimarek on twitter and github
Rossen Stoyanchev
● Spring Framework committer - Spring MVC,
WebFlux, web messaging, RSocket
● RSocket Java committer
● Spring GraphQL co-creator
● Working at VMware, Cambridge, UK
● @rstoya05 on twitter
Cover w/ Image
Agenda
● What is GraphQL?
● GraphQL Java
● Spring GraphQL
What is GraphQL?
What is GraphQL?
GraphQL is a technology for client server data exchange
● A clients wants to access data on a server (across a network)
● Originally developed by Facebook for their iOS app in 2012
● Open sourced in 2015, governed by a non profit foundation today
● Two pillars: statically typed API + query language
● Sweet spots are Single Page Apps and native clients
● An alternative to REST(ish) APIs
● My favourite argument for GraphQL: the developer experience
GraphiQL demo
GraphQL Java
Data Fetching Layer
HTTP
Web iOS Android
Cache Microservices DB
The two pillars of GraphQL
GraphQL Schema
● Describes your API
● Defined on the server
● Based on a simple static types system
● Schema Definition Language (SDL) is used to
describe a Schema
GraphQL Query Language
● Custom query language
● Clients define the query based on their needs
● Every field needs to be requested explicitly
GraphQL Schema
Example
Spring GraphQL
GraphQL Query
Example
The GraphQL ecosystem
GraphQL ecosystem is based on a specification
● The GraphQL specification defines how GraphQL queries should be executed
● It defines the GraphQL schema + query language
● First there was the spec + reference implementation in JS (GraphQL.JS)
● Next it was implemented in every major language
GraphQL Java
GraphQL Java
GraphQL Java is the GraphQL implementation for Java
● It is an implementation for the server side GraphQL execution (also called execution
engine)
● Started mid 2015
● Pure engine: no HTTP or IO. No high level abstracts.
● Used word wide and empowers a whole ecosystem of libraries build on top
● https://graphql-java.com/
How to think in GraphQL Java
Schema first and DataFetchers
● Start designing by putting the Schema first
● Use case and client oriented
● Define the schema in SDL (textual format, preferred) or programmatically
● Schema is made out of types with fields
● Fundamental rule: every field has a DataFetcher associated with
● DataFetcher fetches the data for one field
● If you don’t specify a DataFetcher a default DataFetcher is provided
Spring GraphQL
PropertyDataFetcher
PropertyDataFetcher
DataFetcher
Calling the employee service
DataFetcher
Calling the department service
DataFetcher
Calling the employee service
1 Query.allEmployees
2a Employee.id
2b Employee.name
2c Employee.department
3a Department.name
3b Department.employees
4 Employee.id
Request Execution: DataFetchers sequence
From GraphQL Java to Spring GraphQL
Spring GraphQL is the missing gap in the developer story
● GraphQL Java is “limited” on purpose
● GraphQL Java lets you do everything, but not everything is as simple and convenient
as possible
● The Spring and GraphQL Java teams came together to fix that
● Spring GraphQL is focused on comprehensive and first level support
● It aims to be a fundamental building block build directly on GraphQL Java
Spring GraphQL
Transport ?
GraphQL Java
Engine
Data Fetchers
Database Message
Broker
Microservice
GraphQL Java
GraphQL Java
HTTP,
WebSocket
GraphQL Java
Engine
Data Fetchers
Database Message
Broker
Microservice
Spring MVC
Spring WebFlux
HTTP,
WebSocket
GraphQL Java
Engine
Data Fetchers
Database Message
Broker
Microservice
Initialization
Context propagation
Security
Exception resolution
Spring MVC
Spring WebFlux
GraphQL Java
Engine
Data Fetchers
Database Message
Broker
Microservice
@Controller
Querydsl
HTTP,
WebSocket
Spring MVC
Spring WebFlux
Initialization
Context propagation
Security
Exception resolution
GraphQL Java
Engine
Data Fetchers
Database Message
Broker
Microservice
HTTP,
WebSocket
Testing
@Controller
Querydsl repositories
Spring MVC
Spring WebFlux
Initialization
Context propagation
Security
Exception resolution
GraphQL Java
Engine
Data Fetchers
Database Message
Broker
Microservice
Boot Starter
Testing
@Controller
Querydsl repositories
Spring MVC
Spring WebFlux
Initialization
Context propagation
Security
Exception resolution
HTTP,
WebSocket
Customize ExecutionInput
Inspect ExecutionResult
Web Transports
WebMvc + WebFlux
Functional HTTP
Handlers
WebSocket
Handlers
WebInterceptor WebInterceptor GraphQlService graphql.GraphQL
Functional HTTP
Handlers
WebSocket
Handlers
WebInterceptor WebInterceptor GraphQlService graphql.GraphQL
Reactor API
Async Execution
Mono from CompletableFuture
Web Transports
WebMvc + WebFlux
Spring GraphQL
GraphQL Java
Engine
Data
Fetchers
Service Service Service
HTTP, WebSocket
Handlers
Spring Security
Filter Chain
@PreAuthorize
@Secured
Security
Security and Context Propagation
Spring MVC
ThreadLocal context propagation from
Servlet container thread
Need to register ThreadLocalAccessor
Built-in accessor for Spring Security context
Spring WebFlux
Reactor context propagation from web layer
Spring Security context propagated
GraphQL Java
Engine
Data
Fetchers
Service Service Service
HTTP, WebSocket
Handlers
Spring Security
Filter Chain
@PreAuthorize
@Secured
Security
Context Propagation
Data Layer
The DataFetcher Contract
DataFetcher<T>
DataFetcher<T>
DataFetcher<T>
DataFetcher Wiring to Schema Fields
Spring GraphQL
Spring GraphQL
Spring GraphQL
type: Query, field: allEmployees
type = Department, field = employees
type = Mutation, field = updateSalary
Exception Handling
GraphQL Java allows registering a single DataFetcherExceptionHandler
Spring GraphQL enables a DataFetcherExceptionResolver chain
DataFetcherExceptionResolver
ExceptionResolversExceptionHandler
DataFetcherExceptionResolver
DataFetcherExceptionResolver
Spring GraphQL
Querydsl
Typesafe way to express queries in Java that works across multiple data stores
Spring Data has support for Querydsl
Spring GraphQL and Querydsl
QuerydslDataFetcher
Adapts a Spring Data repository to DataFetcher
Translates GraphQL query parameters to Querydsl Predicate
Adapt the Repository to
a DataFetcher
Match based on query return type for
top-level queries
Automatic Registration
More on Querydsl
Customize how GraphQL request parameters are mapped to Querydsl Predicate
Transform the resulting Objects via interface and DTO projections
GraphQlTester
Workflow for testing GraphQL
Automatic checks to verify no errors in response
Use JsonPath to specify data to inspect
Data decoding
WebGraphQlTester
Extension for GraphQL tests over web transports
Specify HTTP specific inputs
Uses WebTestClient
Spring GraphQL
Spring Boot Starter
Dependencies
Autoconfig
Properties
Metrics
GraphiQL UI and Schema pages
Spring Boot Starter
Currently In Spring GraphQL repository, group id ‘org.springframework.experimental’
Due to move to Spring Boot, after version 2.6 is released
Collaboration with Netflix DGS
Optional, alternative starter to run DGS on the spring-graphql core
DGS programming model + spring-graphql WebMvc / WebFlux foundation
spring-graphql starter to become the main starter eventually
Netflix DGS Features
Annotation based registration of data fetchers, data loaders, scalars, etc.
Code generation for GraphQL Schema -> Java/Kotlin
GraphQL client for Java/Kotlin
Federation
Roadmap Timeline
M3: mid-October
Starter integrated into Spring Boot, after 2.6 release
RC phase: early 2022
GA: May 2022 with Spring Boot 2.7
Roadmap Features
Evolve controller programming model, #63 (batch loaders), #110 (bean validation)
Automated registration of Spring Data repositories, #99
Query by Example (QBE) support as an alternative to Querydsl, #115
GraphQL client, #10
More...
Roadmap Features
Your feedback
Thank you
Andreas Marek @andimarek
Rossen Stoyanchev @rstoya05
© 2021 Spring. A VMware-backed project.
1 of 59

More Related Content

What's hot(20)

Introduction to GraphQLIntroduction to GraphQL
Introduction to GraphQL
Bhargav Anadkat791 views
GraphQL FundamentalsGraphQL Fundamentals
GraphQL Fundamentals
Virbhadra Ankalkote126 views
Introduction to Spring BootIntroduction to Spring Boot
Introduction to Spring Boot
Purbarun Chakrabarti1K views
REST vs gRPC: Battle of API'sREST vs gRPC: Battle of API's
REST vs gRPC: Battle of API's
Luram Archanjo669 views
GraalVMGraalVM
GraalVM
NexThoughts Technologies916 views
What is gRPC introduction gRPC ExplainedWhat is gRPC introduction gRPC Explained
What is gRPC introduction gRPC Explained
jeetendra mandal42 views
REST API Design & DevelopmentREST API Design & Development
REST API Design & Development
Ashok Pundit1.2K views
React js programming conceptReact js programming concept
React js programming concept
Tariqul islam1.4K views
React & GraphQLReact & GraphQL
React & GraphQL
Nikolas Burk908 views
REST APIs with SpringREST APIs with Spring
REST APIs with Spring
Joshua Long25.6K views
gRPC OverviewgRPC Overview
gRPC Overview
Varun Talwar13K views
REST API and CRUDREST API and CRUD
REST API and CRUD
Prem Sanil155.7K views
Spring BootSpring Boot
Spring Boot
Jiayun Zhou2.5K views
Spring Native and Spring AOTSpring Native and Spring AOT
Spring Native and Spring AOT
VMware Tanzu1.6K views
GraphQL vs RESTGraphQL vs REST
GraphQL vs REST
GreeceJS3K views

Similar to Spring GraphQL(20)

 GraphQL-ify your APIs - Devoxx UK 2021 GraphQL-ify your APIs - Devoxx UK 2021
GraphQL-ify your APIs - Devoxx UK 2021
Soham Dasgupta102 views
GraphQL-ify your APIsGraphQL-ify your APIs
GraphQL-ify your APIs
Soham Dasgupta45 views
Boost your API with GraphQLBoost your API with GraphQL
Boost your API with GraphQL
Jean-Francois James133 views
GraphQL_devoxx_2023.pptxGraphQL_devoxx_2023.pptx
GraphQL_devoxx_2023.pptx
Soham Dasgupta133 views
Create GraphQL server with apolloJSCreate GraphQL server with apolloJS
Create GraphQL server with apolloJS
Jonathan Jalouzot343 views
GraphQL-ify your APIsGraphQL-ify your APIs
GraphQL-ify your APIs
Soham Dasgupta94 views
Boost your APIs with GraphQL 1.0Boost your APIs with GraphQL 1.0
Boost your APIs with GraphQL 1.0
Otávio Santana682 views
Boost your APIs with GraphQLBoost your APIs with GraphQL
Boost your APIs with GraphQL
Jean-Francois James95 views
GraphQL-ify your API - JFall 2022GraphQL-ify your API - JFall 2022
GraphQL-ify your API - JFall 2022
Soham Dasgupta38 views
Kotlin REST & GraphQL APIKotlin REST & GraphQL API
Kotlin REST & GraphQL API
Sean O'Brien62 views
GraphQL over REST at Reactathon 2018GraphQL over REST at Reactathon 2018
GraphQL over REST at Reactathon 2018
Sashko Stubailo1.4K views
The Apollo and GraphQL StackThe Apollo and GraphQL Stack
The Apollo and GraphQL Stack
Sashko Stubailo2.5K views
Spring Update | July 2023Spring Update | July 2023
Spring Update | July 2023
VMware Tanzu68 views
Graph ql subscriptions on the jvmGraph ql subscriptions on the jvm
Graph ql subscriptions on the jvm
Gerard Klijs61 views
GraphQL API Crafts presentationGraphQL API Crafts presentation
GraphQL API Crafts presentation
Sudheer J372 views
Spring Boot 3 And BeyondSpring Boot 3 And Beyond
Spring Boot 3 And Beyond
VMware Tanzu130 views

More from VMware Tanzu(20)

Building Cloud Ready AppsBuilding Cloud Ready Apps
Building Cloud Ready Apps
VMware Tanzu54 views
tanzu_developer_connect.pptxtanzu_developer_connect.pptx
tanzu_developer_connect.pptx
VMware Tanzu153 views
Tanzu Developer Connect - FrenchTanzu Developer Connect - French
Tanzu Developer Connect - French
VMware Tanzu13 views

Recently uploaded(20)

AutoMailX PremiumAutoMailX Premium
AutoMailX Premium
GhouseMohiddin1614 views
New Groundbreaking AI App.pdfNew Groundbreaking AI App.pdf
New Groundbreaking AI App.pdf
Anwar78Talukder7 views
esqlfunctions.pdfesqlfunctions.pdf
esqlfunctions.pdf
Votarikari Shravan147 views
Green Cloud - Measure cloud emissionsGreen Cloud - Measure cloud emissions
Green Cloud - Measure cloud emissions
Green Software Development28 views
Java-ML-lego-j-fallJava-ML-lego-j-fall
Java-ML-lego-j-fall
Jago de Vreede46 views

Spring GraphQL

  • 1. Andreas Marek Rossen Stoyanchev Spring GraphQL Copyright © 2021 VMware, Inc. or its affiliates.
  • 2. Who are we Andreas Marek ● GraphQL Java creator and maintainer ● GraphQL contributor and Technical Steering Committee member ● Spring GraphQL co-creator ● Working at Atlassian, Sydney ● @andimarek on twitter and github Rossen Stoyanchev ● Spring Framework committer - Spring MVC, WebFlux, web messaging, RSocket ● RSocket Java committer ● Spring GraphQL co-creator ● Working at VMware, Cambridge, UK ● @rstoya05 on twitter
  • 3. Cover w/ Image Agenda ● What is GraphQL? ● GraphQL Java ● Spring GraphQL
  • 4. What is GraphQL?
  • 5. What is GraphQL? GraphQL is a technology for client server data exchange ● A clients wants to access data on a server (across a network) ● Originally developed by Facebook for their iOS app in 2012 ● Open sourced in 2015, governed by a non profit foundation today ● Two pillars: statically typed API + query language ● Sweet spots are Single Page Apps and native clients ● An alternative to REST(ish) APIs ● My favourite argument for GraphQL: the developer experience
  • 6. GraphiQL demo
  • 7. GraphQL Java Data Fetching Layer HTTP Web iOS Android Cache Microservices DB
  • 8. The two pillars of GraphQL GraphQL Schema ● Describes your API ● Defined on the server ● Based on a simple static types system ● Schema Definition Language (SDL) is used to describe a Schema GraphQL Query Language ● Custom query language ● Clients define the query based on their needs ● Every field needs to be requested explicitly
  • 9. GraphQL Schema Example
  • 11. GraphQL Query Example
  • 12. The GraphQL ecosystem GraphQL ecosystem is based on a specification ● The GraphQL specification defines how GraphQL queries should be executed ● It defines the GraphQL schema + query language ● First there was the spec + reference implementation in JS (GraphQL.JS) ● Next it was implemented in every major language
  • 13. GraphQL Java
  • 14. GraphQL Java GraphQL Java is the GraphQL implementation for Java ● It is an implementation for the server side GraphQL execution (also called execution engine) ● Started mid 2015 ● Pure engine: no HTTP or IO. No high level abstracts. ● Used word wide and empowers a whole ecosystem of libraries build on top ● https://graphql-java.com/
  • 15. How to think in GraphQL Java Schema first and DataFetchers ● Start designing by putting the Schema first ● Use case and client oriented ● Define the schema in SDL (textual format, preferred) or programmatically ● Schema is made out of types with fields ● Fundamental rule: every field has a DataFetcher associated with ● DataFetcher fetches the data for one field ● If you don’t specify a DataFetcher a default DataFetcher is provided
  • 17. PropertyDataFetcher PropertyDataFetcher DataFetcher Calling the employee service DataFetcher Calling the department service DataFetcher Calling the employee service
  • 18. 1 Query.allEmployees 2a Employee.id 2b Employee.name 2c Employee.department 3a Department.name 3b Department.employees 4 Employee.id Request Execution: DataFetchers sequence
  • 19. From GraphQL Java to Spring GraphQL Spring GraphQL is the missing gap in the developer story ● GraphQL Java is “limited” on purpose ● GraphQL Java lets you do everything, but not everything is as simple and convenient as possible ● The Spring and GraphQL Java teams came together to fix that ● Spring GraphQL is focused on comprehensive and first level support ● It aims to be a fundamental building block build directly on GraphQL Java
  • 20. Spring GraphQL
  • 21. Transport ? GraphQL Java Engine Data Fetchers Database Message Broker Microservice GraphQL Java
  • 22. GraphQL Java HTTP, WebSocket GraphQL Java Engine Data Fetchers Database Message Broker Microservice Spring MVC Spring WebFlux
  • 23. HTTP, WebSocket GraphQL Java Engine Data Fetchers Database Message Broker Microservice Initialization Context propagation Security Exception resolution Spring MVC Spring WebFlux
  • 24. GraphQL Java Engine Data Fetchers Database Message Broker Microservice @Controller Querydsl HTTP, WebSocket Spring MVC Spring WebFlux Initialization Context propagation Security Exception resolution
  • 25. GraphQL Java Engine Data Fetchers Database Message Broker Microservice HTTP, WebSocket Testing @Controller Querydsl repositories Spring MVC Spring WebFlux Initialization Context propagation Security Exception resolution
  • 26. GraphQL Java Engine Data Fetchers Database Message Broker Microservice Boot Starter Testing @Controller Querydsl repositories Spring MVC Spring WebFlux Initialization Context propagation Security Exception resolution HTTP, WebSocket
  • 27. Customize ExecutionInput Inspect ExecutionResult Web Transports WebMvc + WebFlux Functional HTTP Handlers WebSocket Handlers WebInterceptor WebInterceptor GraphQlService graphql.GraphQL
  • 28. Functional HTTP Handlers WebSocket Handlers WebInterceptor WebInterceptor GraphQlService graphql.GraphQL Reactor API Async Execution Mono from CompletableFuture Web Transports WebMvc + WebFlux
  • 30. GraphQL Java Engine Data Fetchers Service Service Service HTTP, WebSocket Handlers Spring Security Filter Chain @PreAuthorize @Secured Security
  • 31. Security and Context Propagation Spring MVC ThreadLocal context propagation from Servlet container thread Need to register ThreadLocalAccessor Built-in accessor for Spring Security context Spring WebFlux Reactor context propagation from web layer Spring Security context propagated
  • 32. GraphQL Java Engine Data Fetchers Service Service Service HTTP, WebSocket Handlers Spring Security Filter Chain @PreAuthorize @Secured Security Context Propagation
  • 33. Data Layer
  • 34. The DataFetcher Contract
  • 35. DataFetcher<T> DataFetcher<T> DataFetcher<T> DataFetcher Wiring to Schema Fields
  • 39. type: Query, field: allEmployees
  • 40. type = Department, field = employees
  • 41. type = Mutation, field = updateSalary
  • 42. Exception Handling GraphQL Java allows registering a single DataFetcherExceptionHandler Spring GraphQL enables a DataFetcherExceptionResolver chain DataFetcherExceptionResolver ExceptionResolversExceptionHandler DataFetcherExceptionResolver DataFetcherExceptionResolver
  • 44. Querydsl Typesafe way to express queries in Java that works across multiple data stores Spring Data has support for Querydsl
  • 45. Spring GraphQL and Querydsl QuerydslDataFetcher Adapts a Spring Data repository to DataFetcher Translates GraphQL query parameters to Querydsl Predicate
  • 46. Adapt the Repository to a DataFetcher
  • 47. Match based on query return type for top-level queries Automatic Registration
  • 48. More on Querydsl Customize how GraphQL request parameters are mapped to Querydsl Predicate Transform the resulting Objects via interface and DTO projections
  • 49. GraphQlTester Workflow for testing GraphQL Automatic checks to verify no errors in response Use JsonPath to specify data to inspect Data decoding
  • 50. WebGraphQlTester Extension for GraphQL tests over web transports Specify HTTP specific inputs Uses WebTestClient
  • 52. Spring Boot Starter Dependencies Autoconfig Properties Metrics GraphiQL UI and Schema pages
  • 53. Spring Boot Starter Currently In Spring GraphQL repository, group id ‘org.springframework.experimental’ Due to move to Spring Boot, after version 2.6 is released
  • 54. Collaboration with Netflix DGS Optional, alternative starter to run DGS on the spring-graphql core DGS programming model + spring-graphql WebMvc / WebFlux foundation spring-graphql starter to become the main starter eventually
  • 55. Netflix DGS Features Annotation based registration of data fetchers, data loaders, scalars, etc. Code generation for GraphQL Schema -> Java/Kotlin GraphQL client for Java/Kotlin Federation
  • 56. Roadmap Timeline M3: mid-October Starter integrated into Spring Boot, after 2.6 release RC phase: early 2022 GA: May 2022 with Spring Boot 2.7
  • 57. Roadmap Features Evolve controller programming model, #63 (batch loaders), #110 (bean validation) Automated registration of Spring Data repositories, #99 Query by Example (QBE) support as an alternative to Querydsl, #115 GraphQL client, #10 More...
  • 58. Roadmap Features Your feedback
  • 59. Thank you Andreas Marek @andimarek Rossen Stoyanchev @rstoya05 © 2021 Spring. A VMware-backed project.