What is GraphQL?
A query 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 fields, 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
The journey of a GraphQL operation
(Optional) Follow-along: Our GraphQL API so far
Visit our GraphQL API starting point: https://comet-cruises-api.herokuapp.com/
Open up a terminal.
On Mac, you can press
Cmd + Space
to open up Spotlight. Type "Terminal" and open it up.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 }"}'We're getting data back!
{"data":{"__typename":"Query"}}Let's try querying for locations data. Replace
__typename
withlocations
.curl --request POST \--header 'content-type: application/json' \--url 'https://comet-cruises-api.herokuapp.com/' \--data '{"query":"query { locations }"}'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 subfields. 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 🤔
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.