Day02: GraphQL and GitHub GraphQL - Handling Data


Day02: GraphQL and GitHub GraphQL - Handling Data

Day01 作業我的寫法

  • 可到右邊 Docs 看 Query 細節內容
    img
  • Query
    query { 
    repositoryOwner(login:"tensorflow", ){
    avatarUrl
    id
    repository(name: "models"){
      url
     }
    }
    }
    
  • 回傳結果
     {
    "data": {
      "repositoryOwner": {
        "avatarUrl": "https://avatars1.githubusercontent.com/u/15658638?v=4",
        "id": "MDEyOk9yZ2FuaXphdGlvbjE1NjU4NjM4",
        "repository": {
          "url": "https://github.com/tensorflow/models"
        }
      }
    }
    }
    

Handling Data

  1. Ailases

    • 如果要重複使用 Field 的話可以用這招
    • 都去看 tensorflow 下面的 repo: tensorflowmodels

      { 
      repository(name:"tensorflow", owner:"tensorflow" ){
        homepageUrl
        id
       }
      repository(name:"models", owner:"tensorflow" ){
        homepageUrl
        id
       }
      }
      
      • 會出錯

        {
        "errors": [
        {
        "path": [],
        "extensions": {
        "code": "fieldConflict",
        "fieldName": "repository",
        "conflicts": "{name:\"\\\"tensorflow\\\"\",owner:\"\\\"tensorflow\\\"\"} or {name:\"\\\"models\\\"\",owner:\"\\\"tensorflow\\\"\"}"
        },
        "locations": [
        {
          "line": 7,
          "column": 1
        },
        {
          "line": 12,
          "column": 1
        }
        ],
        "message": "Field 'repository' has an argument conflict: {name:\"\\\"tensorflow\\\"\",owner:\"\\\"tensorflow\\\"\"} or {name:\"\\\"models\\\"\",owner:\"\\\"tensorflow\\\"\"}?"
        }
        ]
        }
        
    • Ailases: 改寫成這樣

      • 在query前面加上自訂的名稱

        { 
         tesnsorflowProject: repository(name:"tensorflow", owner:"tensorflow" ){
               homepageUrl
               id
           }
         modelsProject: repository(name:"models", owner:"tensorflow" ){
               homepageUrl
               id
              }
        }
        
      • 完美回傳

        {
        "data": {
        "tesnsorflowProject": {
        "homepageUrl": "https://tensorflow.org",
        "id": "MDEwOlJlcG9zaXRvcnk0NTcxNzI1MA=="
        },
        "modelsProject": {
        "homepageUrl": "",
        "id": "MDEwOlJlcG9zaXRvcnk1MTExNzgzNw=="
        }
        }
        }
        
  2. Fragments

  • 把會重覆要抓抓的 fielss 拿出來方便使用&簡潔~

    • ...寫法跟 javascript 一樣 XDD

      { 
      tesnsorflowProject: repository(name:"tensorflow", owner:"tensorflow" ){
       ...repoFields
      }
      
      modelsProject: repository(name:"models", owner:"tensorflow" ){
       ...repoFields
      }
      }
      
      fragment repoFields on Repository{
      id
      homepageUrl
      description
      }
      
    • 回傳

      {
      "data": {
      "tesnsorflowProject": {
       "id": "MDEwOlJlcG9zaXRvcnk0NTcxNzI1MA==",
       "homepageUrl": "https://tensorflow.org",
       "description": "An Open Source Machine Learning Framework for Everyone"
      },
      "modelsProject": {
       "id": "MDEwOlJlcG9zaXRvcnk1MTExNzgzNw==",
       "homepageUrl": "",
       "description": "Models and examples built with TensorFlow"
      }
      }
      }
      

很好~ 今天玩很多了 XDDD

Day02 作業 (其實懶得打了 XDD

  • 如果要query Nested fields / Pagination 怎麼半?

其實我也是跟著影片學 XD 
Linkedin Learning
七天~ 逼一下自己

我都不會 GraphQL
模仿是最好的學習,耶! 一直很想學會&實作 GraphQL。就靠這七天拉!!! 七天內,把學到的嘎成文字和筆記:) PS.主要跟著: https://www.linkedin.com/learning/paths/stay-ahead-in-graphql?u=43607124






Related Posts

每日心得筆記 2020-06-25(四)

每日心得筆記 2020-06-25(四)

在 Yourator 的兩個月零九天與社會新鮮人生活雜談

在 Yourator 的兩個月零九天與社會新鮮人生活雜談

[Week2] 給自己看的 JavaScript 筆記 - 迴圈、函式、其他觀念

[Week2] 給自己看的 JavaScript 筆記 - 迴圈、函式、其他觀念

Git 版本控制入門(4)- git 狀況劇

Git 版本控制入門(4)- git 狀況劇

教你朋友 CLI(command line)

教你朋友 CLI(command line)

Day01 : 關於這系列的 React 七天寫作松

Day01 : 關於這系列的 React 七天寫作松



Comments