[GraphQL] Reuse GraphQL Selection Sets with Fragments

Posted answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[GraphQL] Reuse GraphQL Selection Sets with Fragments相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

[GraphQL] Use GraphQL's Object Type for Basic Types

使用 express js、passport s 保护 GraphQL 查询

使用 nestjs 角度通用设置时找不到 api/graphql

通过 compose( graphql() ) 手动触发查询

直接干掉 RESTful:GraphQL 是真的香!

The Architectural Principles Behind Vrbo’s GraphQL Implementation