March 2018
Intermediate to advanced
304 pages
6h 59m
English
In the last section, you were introduced to fragments, using the inline variety to associate parts of GraphQL documents with specific types. Named fragments are just like inline fragments, but they’re reusable.
Let’s give that search query another look, this time breaking out the fields for menu items and categories:
| | query Search($term: String!) { |
| | search(matching: $term) { |
| | ... MenuItemFields |
| | ... CategoryFields |
| | } |
| | } |
| | |
| | fragment MenuItemFields on MenuItem { |
| | name |
| | } |
| | |
| | fragment CategoryFields on Category { |
| | name |
| | items { |
| | ... MenuItemFields |
| | } |
| | } |
Just as before, the ... fragment spread is used to insert an instance of a fragment. This time, however, we’re referencing a named fragment that’s defined ...
Read now
Unlock full access