REST APIs can Rest! – GraphQL

  • What is GraphQL?

  • Why GraphQL? (vs REST)

  • How does GraphQL work?

  • GraphQL Dev Tools

  • Live Code!

What is GraphQL?

A query language for your API

query {
    book {
      title
      author
    }
}
{
  "book": {
      "title": "Norwegian Wood",
      "author": "Murakami",
  }
}
// schema.gql 

type Book {
  title: String
  author: String
  publisher: String
}

type Query {
  book: Book
}
  • Ask for what you need, get exactly that

  • Get many resources in a single request

  • Describe what's possible with a type system

  • Evolve your API without versions

Why GraphQL?

GET / book

GET / author

GET / publisher

👩🏻‍💼

🏛

REST API

📔

request 1

request 2

request 3

👩🏻‍💼

🏛

UNDER

FETCHING

📔

👩🏻‍💼

🏛

OVER

FETCHING

📔

👩🏻‍💼

🏛

GraphQL

📔

/ graphql

query {
   book {
    	title
        author
        publisher
    }
}
query {
   book {
    	title
    }
}

How does GraphQL work?

Object types

// schema.gql 

type Book {
  title: String
  publisher: String
  author: Author
}

type Author {
  name: String
  lastName: String
  year: Int
}

Query Type

// schema.gql 

type Book {
  title: String
  publisher: String
  author: Author
}

type Author {
  name: String
  lastName: String
  year: Int
}

type Query {
  book: Book
}
query {
   book {
    	title
        author {
           name
        }
        publisher
    }
}
// response.json

{
  "book": {
      "title": "Norwegian Wood",
      "author": {
        "name": "Murakami"
      },
    "publisher": "Vintage Books"
  }
}

Mutation Type

// schema.gql 

input BookInput {
  title: String
  author: Author
}

type Author {
  name: String
  lastName: String
  year: Int
}

type Mutation {
  addBook(data: BookInput): String
}
mutation {
   addBook (
      data: {
          title: "1Q84"
          author {
             name: "Murakami"
          }
       }
    )
}
// response.json

{
  "addBook": "Book added"
 
}
  

GraphQL Dev Tools

Playground & GraphiQL

Show me the code!

Rest APIs can Rest! –​GraphQL

By Rohini Senthil

Rest APIs can Rest! –​GraphQL

  • 119