The Pragmatic Studio

What Can GraphQL Do?

February 20, 2019

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

What can GraphQL do? In the previous video we saw how GraphQL is a query language for your API. But not just any API. If 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. A GraphQL API, by comparison, supports three types of operations: queries, mutations, and subscriptions.

In this short video we see GraphQL in action using a concrete example that highlights all three operations.

In summary, 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 course. No need to piece together solutions yourself. Use this application as a springboard for creating your own GraphQL apps!

Full-Stack GraphQL Course