$30 off During Our Annual Pro Sale. View Details »

Spring for GraphQL

Spring for GraphQL

Spring for GraphQL presentation gave at Start of Java online meetup.

https://github.com/maciejwalkowiak/spring-for-graphql-talk

Maciej Walkowiak

September 28, 2022
Tweet

More Decks by Maciej Walkowiak

Other Decks in Programming

Transcript

  1. Spring GraphQL
    @maciejwalkowiak

    View Slide

  2. Web Applications < 2010

    View Slide

  3. Enterprise Applications < 2010

    View Slide

  4. Mobile Apps > 2007

    View Slide

  5. Angular & SPA > 2011

    View Slide

  6. Angular & SPA > 2011

    View Slide

  7. Different Clients - Different Needs
    browser displays more data and more types of data - payload size is not a huge issue
    mobile client displays small amount of data - small payload size is critical for UX
    Web: Mobile:
    GET /books

    [

    {

    "id": 122,

    "title": "Modern Java",

    "publicationYear": 2021,

    "coverUrl": "https://...",

    "authorId": 344,

    "description": "... long description text ..."

    },

    ...

    ]
    GET /books

    [

    {

    "id": 122,

    "title": "Modern Java",

    "publicationYear": 2021,

    "coverUrl": "https://...",

    "authorId": 344

    },

    ...

    ]

    View Slide

  8. Data Composition
    client is responsible for data composition
    multiple requests sent from client (potential N+1 problem)
    Fetch books: Fetch authors:
    GET /books

    [

    {

    "id": 122,

    "title": "Modern Java",

    "publicationYear": 2021,

    "coverUrl": "https://...",

    "authorId": 344,

    },

    {

    "id": 123,

    "title": "Spring in Action",

    "publicationYear": 2020,

    "coverUrl": "https://...",

    "authorId": 345,

    }

    ]
    GET /authors/344

    {

    "id": 344,

    "name": "John Smith",

    "avatarUrl": "https://..."

    }

    GET /authors/345

    {

    "id": 344,

    "name": "John Smith",

    "avatarUrl": "https://..."

    }

    View Slide

  9. Problem with REST(ish) APIs
    REST is an architecture style
    no spec - just principles
    difficult to get "right"
    overfetching
    expensive data composition
    no schema
    hypermedia isn’t what developers want

    View Slide

  10. This is not a REST critique
    Also, look at https://jsonapi.org/.

    View Slide

  11. Hello GraphQL
    Forget everything you know about REST
    Ignore HTTP methods, status codes, caching
    A query language for your API GraphQL is a query
    language for APIs and a runtime for fulfilling those
    queries with your existing data. GraphQL provides a
    complete and understandable description of the data in
    your API, gives clients the power to ask for exactly what
    they need and nothing more, makes it easier to evolve
    APIs over time, and enables powerful developer tools.

    View Slide

  12. GraphQL
    Schema: Query:
    Results:
    type Query {

    findAuthors: [Author]

    author(id: ID!): Author

    }

    type Author {

    id: ID!

    name: String!

    age: Int

    books: [Book]

    }

    type Book {

    id: ID!

    title: String!
    author: Author!

    publicationYear: Int

    }
    query {

    findAuthors {

    id

    name

    books {

    title

    }

    }

    }
    {

    "data": {

    "findAuthors": [

    {

    "id": "1",
    "name": "maciej",

    "books": [
    {

    "title": "book 1"

    },

    View Slide

  13. DEMO

    View Slide

  14. We have just scratched the surface

    View Slide

  15. Challenges
    microservies & single /graphql endpoint?
    caching?
    database transactions?
    ` `

    View Slide

  16. Continue Learning
    https://www.graphql-java.com/
    https://spring.io/projects/spring-graphql
    https://www.howtographql.com/
    https://youtube.com/springacademy <-- that’s me
    follow @maciejwalkowiak on twitter

    View Slide

  17. Thank you!

    View Slide