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

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

时间: 2024-10-17 05:40:09

[GraphQL] Reuse GraphQL Selection Sets with Fragments的相关文章

[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

[GraphQL] Reuse Query Fields with GraphQL Fragments

A GraphQL fragment encapsulates a collection of fields that can be included in queries. In this video, we'll look at how to create fragments on types to reduce the amount of typing that needs to occur as queries become more complex. We'll use the Git

让ASP.NET Core支持GraphQL之-GraphQL的实现原理

众所周知RESTful API是目前最流行的软件架构风格之一,它主要用于客户端和服务器交互类的软件.基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制. RESTful的优越性是毋庸置疑的,不过GraphQL也可以作为一种补充,让你的服务既支持RESTful的http调用,也容许客户端通过GraphQL支持的声明式语法调用服务. 本篇文章并不想对比RESTful和GraphQL孰轻孰重,或者那种方式更好,相关比较可以参考GraphQL的前世今生.本文旨在介绍如何在ASP.NET C

[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 pet

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学习之原理篇

前言 在上一篇文章基础篇中,我们介绍了GraphQL的语法以及类型系统,算是对GraphQL有个基本的认识.在这一篇中,我们将会介绍GraphQL的实现原理.说到原理,我们就不得不依托于GraphQL的规范:GraphQL 概述 GraphQL规范主体部分有6大部分,除去我们在上一节讲到的类型系统(Type System)和语言(Language),剩下的便是整个GraphQL的主流程.也就是如下图所示的: 根据规范的章节,也就是GraphQL的实现流程,我们原理篇一一来看看规范到底定义了些什么

GraphQL入门指南

Introduction GraphQL is Facebook's new query language for fetching application data in a uniform way. GraphQL并不是一个面向图数据库的查询语言,而是一个数据抽象层,包括数据格式.数据关联.查询方式定义与实现等等一揽子的东西.GraphQL也并不是一个具体的后端编程框架,如果将REST看做适合于简单逻辑的查询标准,那么GraphQL可以做一个独立的抽象层,通过对于多个REST风格的简单的接口

记一次通过c#运用GraphQL调用Github api

GraphQL是什么 .net下如何运用GraphQL 运用GraphQL调用Github api 结语 一.Graphql是什么 最近在折腾使用Github api做个微信小程序练练手,本篇文章就是在这个过程中记录. 直接先看下GraphQL的语法风格,感受一下: query { repository(owner:"octocat", name:"Hello-World") { id } } 这是最最最简单的一个运用示例,效果上等价于http://graphqla

使用ASP.NET Core开发GraphQL服务器 -- 预备知识(上)

为了介绍使用ASP.NET Core构建GraphQL服务器,本文需要介绍一下GraphQL,其实看官网的文档就行. 什么是GraphQL? GraphQL 既是一种用于 API 的查询语言也是一个满足你数据查询的运行时. GraphQL 对你的 API 中的数据提供了一套易于理解的完整描述,使得客户端能够准确地获得它需要的数据,而且没有任何冗余,也让 API 更容易地随着时间推移而演进,还能用于构建强大的开发者工具. 官网地址:https://graphql.org/ 中文网址(感觉不是官方的