[GraphQL] Query GraphQL Interface Types in GraphQL Playground

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
      },
     ...

}

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

时间: 2024-10-07 11:02:16

[GraphQL] Query GraphQL Interface Types in GraphQL Playground的相关文章

以LeetCode为例——如何发送GraphQL Query获取数据

前言 ??GraphQL 是一种用于 API 的查询语言,是由 Facebook 开源的一种用于提供数据查询服务的抽象框架.在服务端 API 开发中,很多时候定义一个接口返回的数据相对固定,因此要获得更多信息或者只想得到某部分信息时,基于 RESTful API 的接口就显得不那么灵活.而 GraphQL 对 API 中的数据提供了一套易于理解的完整描述,使得客户端能够准确地获得它需要的数据,而且没有任何冗余,也让 API 更容易地随着时间推移而演进,还能用于构建强大的开发者工具. ??目前,L

以LeetCode为例重庆幸运农场——如何发送GraphQL Query获取数据

前言GraphQL 是一种用于 API 的查询语言重庆幸运农场QQ2952777280[话仙源码论坛]hxforum.com[木瓜源码论坛]papayabbs.com ,是由 Facebook 开源的一种用于提供数据查询服务的抽象框架.在服务端 API 开发中,很多时候定义一个接口返回的数据相对固定,因此要获得更多信息或者只想得到某部分信息时,基于 RESTful API 的接口就显得不那么灵活.而 GraphQL 对 API 中的数据提供了一套易于理解的完整描述,使得客户端能够准确地获得它需要

[GraphQL] Query Lists of Multiple Types using a Union in GraphQL

Unions are used when we want a GraphQL field or list to handle multiple types of data. With a Union Type, we can define a field that could resolve completely different types of data. In this lesson, we will write a query that obtains a list of differ

[GraphQL] Query a GraphQL API with graphql-request

To query a GraphQL API, all you need to do is send an HTTP request that includes the query operation in the body of the request. In this lesson, we will use the browser’s fetch method to request the total days skied from our GraphQL API. const query

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

We can create the most basic components of our GraphQL Schema using GraphQL's Object Types. These types allow us to group related fields together under a specific type, such as a Video or a User, and then allows us to fetch these types when we query

Why GraphQL is Taking Over APIs

A few years ago, I managed a team at DocuSign that was tasked with re-writing the main DocuSign web app which was used by tens of millions of users. The APIs didn’t exist yet to support our new shiny front-end app because since the beginning the web

[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

[GraphQL] Write a GraphQL Schema in JavaScript

Writing out a GraphQL Schema in the common GraphQL Language can work for simple GraphQL Schemas, but as our application grows, or when we start using more complex types like interfaces or unions, we find that we can’t use a GraphQL Language file in t

ASP.NET Core中使用GraphQL - 第六章 使用EF Core作为持久化仓储

ASP.NET Core中使用GraphQL ASP.NET Core中使用GraphQL - 第一章 Hello World ASP.NET Core中使用GraphQL - 第二章 中间件 ASP.NET Core中使用GraphQL - 第三章 依赖注入 ASP.NET Core中使用GraphQL - 第四章 GrahpiQL ASP.NET Core中使用GraphQL - 第五章 字段, 参数, 变量 本篇中我将演示如何配置持久化仓储,这里原文中是使用的Postgres, 这里我改用