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

GraphQL with Spring Boot

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for Krzysztof Pawlowski Krzysztof Pawlowski
November 18, 2017
250

GraphQL with Spring Boot

Slides from my presentation @ CareerCon Kariera IT Gdańsk (18.11.2017)

Avatar for Krzysztof Pawlowski

Krzysztof Pawlowski

November 18, 2017
Tweet

Transcript

  1. About me • Senior Java Developer @ Merapar Technologies •

    Coder, Lecturer (PJATK), Java Trainer (iSA) • 7+ years of experience in Java Development • using GraphQL for a year now • E-mail: [email protected] • Twitter: krzychpawlowski
  2. 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
  3. 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]
 }
  4. 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
  5. 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]
 }
  6. 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
  7. 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!”
 }
  8. While in GraphQL… authors(filter : {id : 1 }) {


    name
 blogEntry {
 title
 comment {
 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
  9. What is GraphQL? “GraphQL is a query language for APIs

    and a runtime for fulfilling those queries with your existing data.” — http://graphql.org
  10. 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
  11. 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
  12. 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
  13. 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" } ] } ] }
  14. GraphQL query - filter authors(filter : {id : 1 })

    { name email blogEntries { title } }
  15. 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" } ] } ] } }
  16. 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]" }
  17. 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]" } } }
  18. 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 }

  19. 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 … }
  20. 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 }
  21. 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