码迷,mamicode.com
首页 > 其他好文 > 详细

[GraphQL] Reuse GraphQL Selection Sets with Fragments

时间:2019-08-20 18:45:59      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:sso   types   tom   sel   stat   long   span   col   customer   

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] Reuse GraphQL Selection Sets with Fragments

标签:sso   types   tom   sel   stat   long   span   col   customer   

原文地址:https://www.cnblogs.com/Answer1215/p/11384452.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!