[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 GitHub API to test.

We have:

# Type queries into this side of the screen, and you will
# see intelligent typeaheads aware of the current GraphQL type schema,
# live syntax, and validation errors highlighted within the text.

# We‘ll get you started with a simple query showing your username!
query {
  organization(login: "moonhighway") {
    email,
    url,
    repository(name: "learning-graphql") {
      url,
      description
    }
  },
  repository(owner:"facebook" name:"graphql"){
    url,
    description,
    name,
    languages(first:1){
      nodes {
        name
      }
    }
  }
}

To resue ‘url‘, ‘description‘ for Repository, we can create fragment:

fragment CommonFields on Repository {
  url,
  description
}

Therefore, we can reuse it:

# Type queries into this side of the screen, and you will
# see intelligent typeaheads aware of the current GraphQL type schema,
# live syntax, and validation errors highlighted within the text.

# We‘ll get you started with a simple query showing your username!
query {
  organization(login: "moonhighway") {
    email,
    url,
    repository(name: "learning-graphql") {
      ...CommonFields
    }
  },
  repository(owner:"facebook" name:"graphql"){
    ...CommonFields
    name,
    languages(first:1){
      nodes {
        name
      }
    }
  }
}

fragment CommonFields on Repository {
  url,
  description
}

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

时间: 2024-10-21 00:31:00

[GraphQL] Reuse Query Fields with GraphQL Fragments的相关文章

[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介绍(Introduction to GraphQL)

Introduction to GraphQL GraphQL介绍 Learn about GraphQL, how it works, and how to use it in this series of articles. Looking for documentation on how to build a GraphQL service? There are libraries to help you implement GraphQL in many different langua

使用lua graphql 模块让openresty 支持graphql api

graphql 是一个很不错的api 查询标准语言,已经有一个lua 的版本支持graphql 项目使用docker&&docker-compose 运行 环境准备 模块安装 luarocks install graphql docker镜像准备 模块使用luarocks 安装,默认alpine 镜像是没有安装这个包,我们使用alpine-fat的 FROM openresty/openresty:alpine-fat RUN /usr/local/openresty/luajit/bin

GraphQL介绍&使用nestjs构建GraphQL查询服务

GraphQL介绍&使用nestjs构建GraphQL查询服务(文章底部附demo地址) GraphQL一种用为你 API 而生的查询语言.出自于Facebook,GraphQL非常易懂,直接看查询语句就能知道查询出来的数据是什么样的.本质上属于API Layer层,负责前端请求的合并.数据整理等功能. 查询示例 使用几个简单的例子看下GraphQL的查询是什么样子的. 普通查询 { me { name } } 查询出来的数据格式如下: { "me": { "name

以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 中的数据提供了一套易于理解的完整描述,使得客户端能够准确地获得它需要

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入门指南

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

[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