In GraphQL, fragments are repeatable sets of fields that can be plugged into queries. Fragments are useful for reusability and composability and are used widely in GraphQL.
- [Voiceover] In the last video we created aliases…to query repositories with the same owner.…Let's say I want to query the same fields…for the GraphQL and react projects.…So I'm going to do this manually.…I'm going to type homepageURL…and now we're requesting the same three fields…from each of our reposts.…Now this is a lot of repetition.…As an alternative I can create a fragment…to query the data repeatedly.…Fragments are reusable sets of fields that can be included…in queries as needed.…
So what I'll do here on line 14…is I'm going to create a fragment.…I will give it a name.…I'll call it repofields.…So I'm going to look for the repository object here.…Now there are many different choices.…You can use fragments on any of your objects.…So the fields that I want to use repeatedly…are id, description,…and homepageURL.…
Cool.…So now that I've created my fragment, I need to use it.…So we're going to replace our fields here on line three…with three dots and then the name of the fragment.…Same goes for our line six here.…
In this course, learn how to get started with GraphQL. Eve Porcello walks through the basics of this query language, diving into the data fetching specification, and helping you get up to speed with the syntax and vocabulary of GraphQL. Eve explains how to create basic queries, including how to work with arguments and multiple fields. She also goes into complex techniques for handling data, including working with aliases, fragments, and pagination. Plus, she covers how to edit and transform data with mutations.
- Learning about the origins of GraphQL
- Learning about how GraphQL makes data fetching and updating easier
- Creating basic queries
- Using multiple fields
- Understanding schemas
- Handling data
- Drilling down into nested fields with a query
- Adding variables for argument values
- Creating mutations
- Adding a reaction mutation