[GraphQL] Reuse GraphQL Selection Sets with Fragments
with GraphQL Selection reuse Sets
2023-09-14 09:00:48 时间
Fragments are selection sets that can be used across multiple queries. They allow you to refactor redundant selection sets, and they are essential when querying unions or interface types. In this lesson, we will improve our query logic by creating a fragment for the activity selection set.
To follow along with these queries, go to the Pet Library GraphQL Playground.
query Pet { petById(id:"S-2") { name, weight, photo { thumb }, status, inCareOf { name } } allPets(category:RABBIT){ name, weight, photo { thumb }, status, inCareOf { name, username } } }
We can reuse part of query with fragement:
query Pet { petById(id:"S-2") { ...PetDetail, inCareOf { ...CustomerDetail } } allPets(category:RABBIT){ ...PetDetail, inCareOf { ...CustomerDetail } } } fragment CustomerDetail on Customer { name, username } fragment PetDetail on Pet { name, weight, photo { thumb }, status, }
相关文章
- [GraphQL] Query a GraphQL API with graphql-request
- [React] Make Controlled React Components with Control Props
- [MODx] 6. Cache '!' with login package
- [GraphQL] Reuse Query Fields with GraphQL Fragments
- [Unit Testing] Test async function with Jasmine
- [GraphQL] Deploy a GraphQL dev playground with graphql-up
- [React] Public Class Fields with React Components
- [RxJS] Reactive Programming - Rendering on the DOM with RxJS
- 【DL with Pytorch】第 1 章 :深度学习与PyTorch简介
- 理解javascript中的with关键字
- 【Pytorch with fastai】第 11 章 :使用 fastai 的中级 API 进行数据处理