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

[GraphQL] Reuse Query Fields with GraphQL Fragments

时间:2019-01-13 00:15:09      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:lan   tor   show   red   comm   use   VID   query   val   

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.

# Well 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.

# Well 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
}

 

[GraphQL] Reuse Query Fields with GraphQL Fragments

标签:lan   tor   show   red   comm   use   VID   query   val   

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

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