2. What is GraphQL
1m

What is GraphQL?

A language that enables us to do declarative data fetching, which means:

  • We can describe the data we want,
  • Bundle it all up in one request,
  • And get data back in a predictable shape.

Schema:

  • Describes the shape of the available data
  • A hierarchy of types and , populated by backend data stores

Note: You can think of the schema kind of like a menu that presents us with everything we're allowed to ask for!

Benefits of GraphQL

Which of the following pillars were discussed as benefits of GraphQL?

The journey of a GraphQL operation

Full journey of a GraphQL query

(Optional) Follow-along: Our GraphQL API so far

  1. Visit our API starting point: https://comet-cruises-api.herokuapp.com/

  2. Open up a terminal.

    On Mac, you can press Cmd + Space to open up Spotlight. Type "Terminal" and open it up.

  3. Paste in the POST request command from the landing page:

    curl --request POST \
    --header 'content-type: application/json' \
    --url 'https://comet-cruises-api.herokuapp.com/' \
    --data '{"query":"query { __typename }"}'
  4. We're getting data back!

    {"data":{"__typename":"Query"}}
  5. Let's try for locations data. Replace __typename with locations.

    curl --request POST \
    --header 'content-type: application/json' \
    --url 'https://comet-cruises-api.herokuapp.com/' \
    --data '{"query":"query { locations }"}'
  6. We get an error!

    {"errors":[{"message":"Field \"locations\" of type \"[Location!]!\" must have a selection of subfields. Did you mean \"locations { ... }\"?","locations":[{"line":1,"column":9}],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}}]}

    Looks like we need sub. But this is annoying and I don't know what subfields are available. It's really easy to make typos too!

    There must be a better way 🤔

Previous

Share your questions and comments about this lesson

Your feedback helps us improve! If you're stuck or confused, let us know and we'll help you out. All comments are public and must follow the Apollo Code of Conduct. Note that comments that have been resolved or addressed may be removed.

You'll need a GitHub account to post below. Don't have one? Post in our Odyssey forum instead.