(This is Part 2 of a series: Part 1, Part 3, Part 4)

What can GraphQL do? In the previous video we said it’s a query language for your API. But not just any ‘ol API…

When you send a GraphQL query to a REST API it doesn’t know what to do with it. A REST API expects GET, POST, PUT, and DELETE operations on specific resources.

But a GraphQL API knows how to handle three types of operations:

  • queries that fetch data

    query {
      places(matching: "castle") {
        name
        location
        image
        bookings {
          startDate
          endDate
        }
      }
    }
    
  • mutations that change data (create, update, or delete data)

    mutation {
      createBooking(placeId: 3,
                    startDate: "2019-06-15",
                    endDate: "2019-06-23") {
        startDate
        endDate
        state
        totalPrice
      }
    }
    
  • subscriptions that deliver near real-time data updates

    subscription {
      bookingChange(placeId: 3) {
        startDate
        endDate
        state
        user {
          email
        }
      }
    }
    

So then, how do you know what queries, mutations, and subscriptions a GraphQL API supports? Do you have to guess?

Thankfully, no! A GraphQL API tells you what’s possible by way of its schema, which we explore in the next video.

Unpack a Full-Stack GraphQL App Layer-By-Layer

Learn what it takes to put together a full-stack GraphQL app using Absinthe, Phoenix, and React in our Unpacked: Full-Stack GraphQL video course. No need to piece together solutions yourself. Use this application as a springboard for creating your own GraphQL apps!