zoukankan      html  css  js  c++  java
  • [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
    }
  • 相关阅读:
    马拉车算法【Manachar】
    AcWing算法进阶课 基础算法 启发式合并
    iframe嵌套跨域子页面变化高度自适应
    js数组中的每一项异步请求
    利用geo3d地图数据画地图上面的柱子
    地图上面加柱状图组
    antd-select选项位置偏移问题解决
    git
    React
    前端面试题
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10261423.html
Copyright © 2011-2022 走看看