Share
Embed
GraphQL with Spring Boot
Slides from the presentation at CareerCon Kariera IT in Cracow
Krzysztof Pawlowski
December 02, 2017
Transcript
32 slides
-
About me • Senior Java Developer @ Merapar Technologies •
Coder, Lecturer (PJATK), Java Trainer (infoShare Academy) • 7+ years of experience in Java Development • using GraphQL for a year now • E-mail: [email protected] • Twitter: krzychpawlowski -
REST not so restful? GET /author/:id <<class>> Author Long id
String name String email String bio List<BlogEntry> blogEntries <<class>> BlogEntry Long id String title List<Comment> comments <<class>> Comment Long id String author String comment -
REST not so restful? GET /author/:id <<class>> Author Long id
String name String email String bio List<BlogEntry> blogEntries <<class>> BlogEntry Long id String title List<Comment> comments <<class>> Comment Long id String author String comment { id: 1, name: “Krzysztof”, email:”[email protected]“, bio: “java dev”, blogEntriesIds: [1, 2] } -
REST not so restful? GET /author/:id GET /author/:id/blogEntry/:id <<class>> Author
Long id String name String email String bio List<BlogEntry> blogEntries <<class>> BlogEntry Long id String title List<Comment> comments <<class>> Comment Long id String author String comment -
REST not so restful? GET /author/:id GET /author/:id/blogEntry/:id <<class>> Author
Long id String name String email String bio List<BlogEntry> blogEntries <<class>> BlogEntry Long id String title List<Comment> comments <<class>> Comment Long id String author String comment { id: 1, title: “GraphQL”, authorId: 1, commentsIds: [1, 2] } -
REST not so restful? GET /author/:id/blogEntry/:id/comments GET /author/:id GET /author/:id/blogEntry/:id
<<class>> Author Long id String name String email String bio List<BlogEntry> blogEntries <<class>> BlogEntry Long id String title List<Comment> comments <<class>> Comment Long id String author String comment -
REST not so restful? GET /author/:id/blogEntry/:id/comments GET /author/:id GET /author/:id/blogEntry/:id
<<class>> Author Long id String name String email String bio List<BlogEntry> blogEntries <<class>> BlogEntry Long id String title List<Comment> comments <<class>> Comment Long id String author String comment { id: 1, author: “anonymous”, comment: “super!” } -
While in GraphQL… authors(filter : {id : 1 }) {
name blogEntries { title comments { comment } } } <<class>> Author Long id String name String email String bio List<BlogEntry> blogEntries <<class>> BlogEntry Long id String title List<Comment> comments <<class>> Comment Long id String author String comment -
What is GraphQL? “GraphQL is a query language for APIs
and a runtime for fulfilling those queries with your existing data.” — http://graphql.org -
What is GraphQL? “GraphQL is a query language for APIs
and a runtime for fulfilling those queries with your existing data.” — http://graphql.org Declarative Ask for what you need, get exactly that -
What is GraphQL? “GraphQL is a query language for APIs
and a runtime for fulfilling those queries with your existing data.” — http://graphql.org Declarative Ask for what you need, get exactly that Compositional Get many resources in a single request -
What is GraphQL? “GraphQL is a query language for APIs
and a runtime for fulfilling those queries with your existing data.” — http://graphql.org Declarative Ask for what you need, get exactly that Compositional Get many resources in a single request Strongly Typed Describe what’s possible with a type system -
GraphQL query authors { # queries can have comments! name
email blogEntries { title } } -
GraphQL query authors { # queries can have comments! name
email blogEntries { title } } { "data": { "authors": [ { "name": "author 1", "email": "Python dev", "blogEntries": [ { "title": "blog entry 1" }, { "title": "blog entry 3" } ] }, { "name": "author 2", "email": "Java dev", "blogEntries": [ { "title": "blog entry 2" }, { "title": "blog entry 4" } ] } ] } -
GraphQL query - filter authors(filter : {id : 1 })
{ name email blogEntries { title } } -
GraphQL query - filter authors(filter : {id : 1 })
{ name email blogEntries { title } } { "data": { "authors": [ { "name": "author 1", "email": "Python dev", "blogEntries": [ { "title": "blog entry 1" }, { "title": "blog entry 3" } ] } ] } } -
GraphQL mutation mutation addAuthorMutation($authorInfo: addAuthorInput!) { addAuthor(input : $authorInfo) {
id name bio email } } { "authorInfo": { "id": 1, “name" : "Krzysztof Pawlowski", "bio" : "java dev", "email" : "[email protected]" } -
GraphQL mutation mutation addAuthorMutation($authorInfo: addAuthorInput!) { addAuthor(input : $authorInfo) {
id name bio email } } { "authorInfo": { "id": 1, “name" : "Krzysztof Pawlowski", "bio" : "java dev", "email" : "[email protected]" } { "data": { "addAuthor": { "id": 1, "name": "Krzysztof Pawlowski", "bio": "java dev", "email": “[email protected]" } } } -
GraphQL schema type Author { id: Long! name: String bio:
String email: String blogEntries: [BlogEntry] } type BlogEntry { id: Long! title: String! comments: [Comment] } type Comment { id: Long! author: String comment: String } -
GraphQL schema type Author { id: Long! name: String bio:
String email: String blogEntries: [BlogEntry] } type BlogEntry { id: Long! title: String! comments: [Comment] } type Comment { id: Long! author: String comment: String } type Query { authors(id: Long!): Author blogEntries(id: Long!): BlogEntry comments(id: Long!): Comment } type Mutation { addAuthor(author: Author!): Author updateAuthor(author: Author!): Author deleteAuthor(author: Author!): Author … } -
GraphQL schema type Author { id: Long! name: String bio:
String email: String blogEntries: [BlogEntry] } type BlogEntry { id: Long! title: String! comments: [Comment] } type Comment { id: Long! author: String comment: String } type Query { authors(id: Long!): Author blogEntries(id: Long!): BlogEntry comments(id: Long!): Comment } type Mutation { addAuthor(author: Author!): Author updateAuthor(author: Author!): Author deleteAuthor(author: Author!): Author … } schema { query: Query mutation: Mutation } -
Other benefits of GraphQL ‣flexibility ‣one end-point ‣self-documenting (strong typing
and schema) ‣query validation ‣easy to implement consumer-driven contracts -
Spring Boot Starter for GraphQL ‣adding Maven dependency creates GraphQL
controller under /v1/graphql <dependency> <groupId>com.merapar</groupId> <artifactId>graphql-spring-boot-starter</artifactId> <version>1.0.2</version> </dependency> ‣adding new queries and mutation is a matter of implementing GraphQlFields interface -
Problems with GraphQL ‣HTTP caching ‣Unpredictable execution ‣No handling for
file upload ‣Authorisation and authentication