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

[GraphQL] Query GraphQL Interface Types in GraphQL Playground

时间:2019-08-20 19:07:28      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:out   library   hql   fun   hold   check   alt   tis   rar   

Interfaces are similar to Unions in that they provide a mechanism for dealing with different types of data. However, an interface is more suited for data types that include many of the same fields. In this lesson, we will query different types of pets.

To follow along with these queries, go to the Pet Library GraphQL Playground.

 

Interface:

技术图片

 

# Write your query or mutation here
query {
  totalPets,
  availablePets,
  checkedOutPets,
  allAvailablePets {
    __typename,
    ...PetDetail,
    # only related to CAT
    ... on Cat {
      sleepAmount
    },
    # only related to Rabbit
    ... on Rabbit {
      favoriteFood,
      floppy
    },
    # only related to Stingray
    ... on  Stingray {
      fast
    }
  }
}

fragment PetDetail on Pet {
  name,
  weight,
  status,
  photo {
    thumb
  }
}

 

Data return:

{
  "data": {
    "totalPets": 25,
    "availablePets": 23,
    "checkedOutPets": 2,
    "allAvailablePets": [
      {
        "__typename": "Cat",
        "name": "Biscuit",
        "weight": 10.2,
        "status": null,
        "photo": {
          "thumb": "https://www.catis.life/placeholder/200"
        },
        "sleepAmount": 21
      },
     ...

}

 

[GraphQL] Query GraphQL Interface Types in GraphQL Playground

标签:out   library   hql   fun   hold   check   alt   tis   rar   

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

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