Practice
@connect directive parameters should you use to map the endpoint's specific JSON properties to your GraphQL schema?Use the REST API JSON response below to complete the code challenge.
{"id": "px-m012","name": "Mraza","mass": 6.42}
@connect(source: ""http: { GET: "" }selection: """# TODO""")
Add the @connect directive to the Query.popularPlanets field. Use the outerspace source and the API endpoint GET /planets?sort=popular. Refer to the JSON object above and the existing schema to define the selection.
Key takeaways
- The
@connectdirective describes how to get the data for a particular GraphQL field using a definedsource. - Each root field (the fields on the
QueryandMutationtypes) requires a@connectdirective. - We use
@connect'shttpparameter to define the HTTP method and URL parameters on thesourcethat should be used to retrieve data for a field. - We use
@connect'sselectionparameter to map the REST API's JSON response to our GraphQL fields.
Up next
We started small with just a couple of fields, but there's more to show! Let's add more fields and see what happens when our mapping doesn't quite match one-to-one.
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.