<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[LT Lab - Medium]]></title>
        <description><![CDATA[You can become a full stack developer - Medium]]></description>
        <link>https://medium.com/lion-f2e?source=rss----d76981da6b21---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>LT Lab - Medium</title>
            <link>https://medium.com/lion-f2e?source=rss----d76981da6b21---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 23:56:35 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/lion-f2e" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[前端工程的展現，NextJS 完美出擊]]></title>
            <link>https://medium.com/lion-f2e/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E7%9A%84%E5%B1%95%E7%8F%BE-nextjs-%E5%AE%8C%E7%BE%8E%E5%87%BA%E6%93%8A-c964ca05c631?source=rss----d76981da6b21---4</link>
            <guid isPermaLink="false">https://medium.com/p/c964ca05c631</guid>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[router]]></category>
            <category><![CDATA[server-side-rendering]]></category>
            <category><![CDATA[nextjs]]></category>
            <dc:creator><![CDATA[DEBUG]]></dc:creator>
            <pubDate>Thu, 14 May 2020 05:45:11 GMT</pubDate>
            <atom:updated>2020-05-14T23:02:45.702Z</atom:updated>
            <content:encoded><![CDATA[<h3>LionEdu 1–5 前端工程的展現，NextJS 完美出擊</h3><p>如果你是前端新手或者是自家產品技術包袱很沈重的專案，在使用 NextJS 可以仔細評估一下其中的效益；會用 NextJS framewrok 都基於用了 ReactJS 需要 SSR(Server Side Render)，讓 SEO 能正確的爬到內容，但並非所有的開發項目都需要將 View 的部分給拆分出來，過往的後端開發模式 MVC (Model-View-Controler) 或許會是更好的方案，透過 Controller 去調用 View 的模板，並將資料塞進指定位置並回應請求，也大大降低開發中的複雜度。</p><p>技術包袱沈重的專案，應該都是以後端為主的 MVC 開發架構，會考慮使用 ReactJS 不外乎想把 View 給抽離出來，做更專業的分工，減少過往前端切版，後端再套版重工的問題，ReactJS 屬於 UI Library ，由瀏覽器端渲染畫面(Client Side Render(CSR))，如果有 SSR 的需求，可以考慮透過自家使用的後端語言(諸如 ASP.NET、PHP 等)做伺服端 JS 渲染(V8JS引擎)；如果前者不是那麼順利的話，想使用 NextJS 來解決 SSR 的問題，可能會是另一個大工程，會需要啟動 NodeJS Service，目的是在處理 UI Render 的問題，現行的 http Service 也需要做調整；可能最後評估下來是保持現狀，以策安全。</p><p>自己測試用伺服端去做 SSR 的經驗也沒那麼順利，用 PHP 去安裝 V8JS 擴展，就搞得昏天黑地，最後放棄收場；如果是使用 NodeJS 去做 SSR 網路上的教學文就蠻多的，可以透過 react-dom/server 提供的方法做到，自己當時實作結果，首次載入會做 Server 跟 Client 兩次渲染的問題，Try Try 滋味就沒深入研究。</p><p>ReactJS 的 SSR 方案並非只有 NextJS ，還有另一套叫做 AfterJS，印象中它的出現是要解決當時 NextJS 路由問題，但軟體工程的可怕在於沒有最好只有更好，NextJS 路由後來也做得越來越完善。(之前也踩過坑，動態路由重刷新會有 404 的問題，當時是透過 <a href="https://github.com/fridays/next-routes">fridays/next-routes</a> 來解決路由問題)。</p><p>NextJS 是前端工程的展現，從過去切版時期所產出的待加工品，變成成品或元件的過程，最後透過軟體工程的整合(服務之間的分離，資料流的傳遞)，組合出完整的應用，每個職位各司其職(一人分飾多角，就一起加油吧…)，更專精於該領域的開發與優化。</p><p>NextJS 它算是 hybrid 的方式，首次載入使用 SSR，後續頁面的切換使用NextJS 的路由處理，透過 CSR 的方式去更新畫面。在 pages 目錄下本身就是路由結構，這邊頁面的檔案型別為 NextPage，具有 function compoent 或 class compoent 的方法或屬性，還擁有一個方法(getInitialProps)，會觸發 SSR 或 CSR。</p><p>SSG 使用情境，將不常異動的資料內容或者是靜態頁面，預先打包成靜態檔案(HTML、CSS、JS)，當請求進來時直接輸出靜態檔，相較於 SSR 不需再重新渲染畫面，減少伺服器的負載。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AQ_uUbtuUPGv7oUwOxifaQ.png" /><figcaption><a href="https://espeo.eu/blog/client-and-server-side-rendering-static-site-generators/">https://espeo.eu/blog/client-and-server-side-rendering-static-site-generators/</a></figcaption></figure><p>除了 getInitialProps，NextJS 9.3 提供三個方法做到 SSR 或 SSG(Static Site Generators)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8f4d49436ee98fa55ebf7f498aff7c2f/href">https://medium.com/media/8f4d49436ee98fa55ebf7f498aff7c2f/href</a></iframe><p>getStaticProps 及 getStaticPaths 主要處理靜態檔案及靜態檔動態路由捕捉，運行時間在於 build 的時候；getServerSideProps 及 getInitialProps，會依據請求運行於 Server 端，差別在於 getInitialProps 會因為運行 next/router 路由，而在 Client 端發出 XHR 請求。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CXAW9os7pcVUWRGk8Y9hHg.png" /></figure><p>NextJS 另一個重點是路由，他依據 pages 下的目錄結構來決定(也可自訂 server 來客製化路由位置)，會 import 到兩種東西 next/link 或 next/router，差在於一個是用 a 標籤來表示，另一個是透過 JS 的方式來控制。如果需要使用動態變數，做動態路由，檔案命名會以中括號表示，括號裡面放變數名稱。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6bad4ea46589038248e959d77400fe05/href">https://medium.com/media/6bad4ea46589038248e959d77400fe05/href</a></iframe><p>next/router 調用的方法，還有一個 options 參數使用，是否做淺層路由(shallow routing)，網址變更但不觸發 getInitialProps ，可用於換頁時，資料已更新連帶變更 url，當做頁面刷新時，能有相同內容，須注意網址變更需要相同的路由位置。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5775f8189edb5e77e7b5a88452eb07e5/href">https://medium.com/media/5775f8189edb5e77e7b5a88452eb07e5/href</a></iframe><p>NextJS 的基本技能不外乎，Webpack、ReactJS、Typescript(如果喜歡強型別)及 CSS，以上技能需要協助或加強的，歡迎找我們教育訓練組的老面孔<a href="https://medium.com/u/b3f6134304a8">Jinwei Chang</a>，前端開發多年經驗，教學認真又富耐心，業界號稱移動的代碼庫，代碼解說具有深度又不失幽默與易懂，就這樣了，不幫 <a href="https://medium.com/u/b3f6134304a8">Jinwei Chang</a> 哥哥工商了(P.S. 因為工商才有機會聽哥哥講 Docker)。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c964ca05c631" width="1" height="1" alt=""><hr><p><a href="https://medium.com/lion-f2e/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E7%9A%84%E5%B1%95%E7%8F%BE-nextjs-%E5%AE%8C%E7%BE%8E%E5%87%BA%E6%93%8A-c964ca05c631">前端工程的展現，NextJS 完美出擊</a> was originally published in <a href="https://medium.com/lion-f2e">LT Lab</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Coursera Financial Aid 申請(簡單三步驟）]]></title>
            <link>https://medium.com/lion-f2e/coursera-financial-aid-%E7%94%B3%E8%AB%8B-%E7%B0%A1%E5%96%AE%E4%B8%89%E6%AD%A5%E9%A9%9F-819c96f5b2a0?source=rss----d76981da6b21---4</link>
            <guid isPermaLink="false">https://medium.com/p/819c96f5b2a0</guid>
            <dc:creator><![CDATA[錢端工程師ＪＷ (Jinwei)]]></dc:creator>
            <pubDate>Wed, 04 Mar 2020 23:43:50 GMT</pubDate>
            <atom:updated>2020-03-05T10:36:18.263Z</atom:updated>
            <content:encoded><![CDATA[<p>你是否也有想學程式或課程找不到資源的狀況呢？在此推薦Coursera平台，能夠免費上國內外大學的課程，打好程式基礎。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/470/1*1zdOfsNVHVN40EoH9H4jVg.jpeg" /><figcaption>▲coursera (<a href="https://complementarytraining.net/two-new-great-courses/coursera-logo-face/">ImageSource</a>)</figcaption></figure><p>以下介紹申請Coursera獎學金的方式只要簡單3步驟即可上課！</p><p>申請獎學金，與付費上課的差別就在於等待期(15天)。其他權益基本上都一樣，完成課程後可以把證書秀在LinkedIn上面。作業繳交與課程討論都與付費內容完全一樣！</p><p>Note: 若申請旁聽，則無法繳交作業與獲得學分與證書。</p><h4>1. 先到<a href="https://www.coursera.org/"><strong>Coursera官網</strong></a>註冊帳號</h4><h4>2. 搜尋想學的課程(e.g. ReactJS)</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/733/1*xPcA8fVc4X7T-nvNMhF1Aw.png" /><figcaption>最上面會出現enroll &amp; Financial aid Available (申請獎助學金）</figcaption></figure><ul><li>此時請選擇Financial aid available</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/909/1*2LimTks7fGg4jrUqP6P0_A.png" /><figcaption>▲ 之後會再次詢問是否要申請獎學金，並說明有15天等待期</figcaption></figure><h4>3. 開始申請步驟</h4><p>3–1 此時可以開始填寫申請表</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/1*Bc67G3a1pPSlc5lL2x8c6w.png" /><figcaption>▲3–1 上面全部勾選並點選繼續</figcaption></figure><p>3–2 填寫基本資料</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/655/1*baLBYAz3AFcqf9bM3vWjDQ.png" /><figcaption>▲3–2 填寫基本資料</figcaption></figure><p>PS. ID 認證部分需要有額外的流程，需上傳照片及資料，基本上按照上面操作，大約3天左右(沒記錯的話）就會回覆是否認證成功…</p><p>驗證審核完上面會有ID Verification</p><p>3–3 詢問為何要申請獎學金等問題</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*M2sLEinB4Pfx6R9l_hJjeQ.png" /><figcaption>▲詢問兩個申請問題</figcaption></figure><p>3–4 最後的問題與意願調查</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/524/1*lZBc2-xFMAXFOcVXL8NViA.png" /><figcaption>▲詢問是否使用低利息方式來付款</figcaption></figure><p>上面從Background information到最後全部填完後就點選送出</p><p>之後Coursera會寄信告知已收到獎助學金申請。之後就等2週左右即可開始上課</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/538/1*XUxtoJzIzKxkcCxvSQTRXQ.png" /><figcaption>▲表示已經收到獎學金申請</figcaption></figure><h4>那學完課程還能做什麼？</h4><p>放到把證書放到LinkedIn中</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/792/1*7KhY8cqbVInqnvbDPoWeiQ.png" /></figure><p>以上！</p><p>有興趣的大家馬上去申請吧～</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=819c96f5b2a0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/lion-f2e/coursera-financial-aid-%E7%94%B3%E8%AB%8B-%E7%B0%A1%E5%96%AE%E4%B8%89%E6%AD%A5%E9%A9%9F-819c96f5b2a0">Coursera Financial Aid 申請(簡單三步驟）</a> was originally published in <a href="https://medium.com/lion-f2e">LT Lab</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[淺談]在 Laravel 中使用 GraphQL(三)]]></title>
            <link>https://medium.com/lion-f2e/%E6%B7%BA%E8%AB%87-%E5%9C%A8-laravel-%E4%B8%AD%E4%BD%BF%E7%94%A8-graphql-%E4%B8%89-d8ce84f2f065?source=rss----d76981da6b21---4</link>
            <guid isPermaLink="false">https://medium.com/p/d8ce84f2f065</guid>
            <category><![CDATA[php]]></category>
            <category><![CDATA[laravel-5]]></category>
            <category><![CDATA[mysql]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[graphql-playground]]></category>
            <dc:creator><![CDATA[Jian-Kai,Kuo]]></dc:creator>
            <pubDate>Tue, 26 Nov 2019 06:23:30 GMT</pubDate>
            <atom:updated>2019-11-26T06:23:30.037Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v_bFZcGA4g6sfHAXhRQuUw.png" /><figcaption>圖片來自： <a href="https://blog.pusher.com/graphql-laravel/">https://blog.pusher.com/graphql-laravel/</a></figcaption></figure><h3>前言</h3><p>上一次我們談到了兩件事情，一是透過 Laravel 的 factory 跟 seed ，根據資料表的屬性產生大量的資料，並且將其寫入資料庫中；二是我們在 Laravel 上使用 GraphQL ，建立了 Query 跟 Type 來完成 GraphQL 中基礎的 schemas ，之後也利用了 GraphQL playground 來向資料庫請求商品資料表中的資料。</p><p>這次我們會談到當資料表之間有關聯時，如何在 model 之間建立連結，接者透過 GraphQL 向資料庫請求完整的資料；接者，當我們需要新增或是修改資料時，如何透過 Mutation 來達到資料的修改或新增。</p><h3>Laravel：model relation→GraphQL： related Type</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/775/1*6Yo6T-nmTyA2vMsOrfCQeg.png" /><figcaption>資料表之間的關聯圖</figcaption></figure><ol><li>products.id → product_tag_ralated.product_id</li><li>product_tag_ralated.tag_id → product_tags.id</li><li>products.product_category_id → product_category.id</li></ol><p>上面的圖表與列表表達了我們在四張資料表之間的關聯，我們可以從 products 的 product_category_id 去對應到 product_categories 的 id，因此我們就可以知道 product 的商品類別是甚麼了；依此類推，來找到商品的標籤有哪些。</p><p>首先，我們需要先在 <strong><em>App\Product.php</em></strong> 新增與商品類別的關聯。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/711/1*o0Tt9MCNxKXs7CzR5Y7xgA.png" /><figcaption><strong><em>App\Product.php </em></strong><em>與 category的關聯</em></figcaption></figure><p>因為一個商品只會對應到一個商品類別，所以它們的關係是一對一的關係，所以透過 <a href="https://laravel.tw/docs/5.2/eloquent-relationships#one-to-one">Laravel 的 hasOne</a> 方法來建立關聯；其中第一個參數代表是的關聯資料表 model ，第二個可以設定為關聯對應的外鍵名稱，第三個則是關聯的外鍵名稱，關於參數的詳細說明可以到 <a href="https://laravel.tw/docs/5.2/eloquent-relationships#one-to-one">Laravel 的官方文件</a>去了解。</p><p>接者在 <strong><em>App\GraphQL\Type\ProductType.php</em></strong> 中加入 category 的屬性。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/527/1*mflW074OtQCASRtaOc8LPQ.png" /><figcaption><strong><em>App\GraphQL\Type\ProductType.php </em></strong><em>新增類別的屬性</em></figcaption></figure><p>之後我們就可以在 graphql-playground 上測試看看是否有成功 query 到類別的資料。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5EQ4TAr6J6tMnw7nfyWcUg.png" /><figcaption>graphql-playground 成功獲得 category 的資料</figcaption></figure><p>成功獲得商品類別的資料後，接者我們想要知道該商品的標籤有哪些？在 product_tag_related 這張資料表告訴了我們 product 與 product_tag 兩張資料表的對應關係，一個商品可能會有多個標籤，一個標籤也有可能會被多個商品擁有；在 <a href="https://laravel.tw/docs/5.2/eloquent-relationships#many-to-many">Larevel 官方文件</a>中提到這是一個多對多的關係。</p><p>因此，我們需要先在 <strong><em>App\Product.php</em></strong> 新增與商品標籤的關聯。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/851/1*i-_nWQYDg3vT_QJqnXtSbQ.png" /><figcaption><strong><em>App\Product.php </em></strong><em>與 tag 的關聯</em></figcaption></figure><p>belongsToMany 有四個參數，第一個參數一樣是關聯的 model ，第二個為兩者之間的關係資料表名稱， 第三個參數是關聯中的模型的外鍵名稱，而第四個則是要合併的模型中的外鍵名稱。詳細的說明可以去<a href="https://laravel.tw/docs/5.2/eloquent-relationships#many-to-many"> Laravel 的官方文件</a>了解。</p><p>接者在 <strong><em>App\GraphQL\Type\ProductType.php</em></strong> 中加入 tag 的屬性。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/670/1*RnAZMXXK6IOalNbxw0ltgw.png" /><figcaption><strong><em>App\GraphQL\Type\ProductType.php </em></strong><em>新增標籤的屬性</em></figcaption></figure><p>因為一個商品可能會有多個標籤，所以在前面用 listOf 的方式列舉出條件的資料。</p><p>之後我們一樣可以在 graphql-playground 上測試看看是否有成功 query 到標籤的相關資料。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jqci7qeIZUERhDhGVfDHlw.png" /><figcaption>graphql-playground 成功獲得 tag 的資料</figcaption></figure><p>到目前為止我們已經可以透過 GraphQl 向資料庫請求資料了，也成功將資料表之間的關聯建立起來了；接下來我們希望可以像資料庫新增或是修改一筆資料。</p><h3>GraphQL：Mutation</h3><p>在 a<strong><em>pp\GraphQL\Mutation </em></strong>新增一個 <strong><em>CreateProductMutation.php</em></strong> ，在裡面我們會設定在新增一筆商品時需要給予的參數，以及接收到參數後要如何進行處理。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7472a4079b10a757f2593e51bab4e0b3/href">https://medium.com/media/7472a4079b10a757f2593e51bab4e0b3/href</a></iframe><p>其中，在 tags 的參數是用字串的方式傳入，然後將字串切割後再與商品 ID 依序寫入 product_tag_related 資料表中。</p><p>之後到 app\Product.php 與 app\ProductTagRelated.php 中依序加入下面程式碼，為了要讓 model 知道那些屬性是能夠被寫入的。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/699/1*dbebeMbDdDVoMEEMDfH7Xw.png" /><figcaption>app\Product.php</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/467/1*JZX4kLZYACvdxHxK22eiwQ.png" /><figcaption>app\ProductTagRelated.php</figcaption></figure><p>最後，我們在 <strong><em>config\graphql.php </em></strong>中加入 Mutation 的相關設定就完成了所有步驟。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/693/1*EKB6TftNkI9aDmmCpTUZZw.png" /><figcaption><strong><em>config\graphql.php </em></strong><em>加入設定</em></figcaption></figure><p>我們一樣到 graphql-playground 來測試看看能不能新增資料到資料庫中。下面的結果圖可以知道 GraphQL 有成功新增一筆資料，而且 id 會自動遞增。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vWUk-NISji5YCJ163jPM1A.png" /><figcaption>GraphQL 成功新增一筆資料</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/946/1*FdCL4SmicmRqM9MI9QvA6g.png" /><figcaption>在資料庫中也看到第 51 筆資料被新增了</figcaption></figure><p>更新單筆資料可以參考下面的範例。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8facfca882525aea2e08a20f632373a1/href">https://medium.com/media/8facfca882525aea2e08a20f632373a1/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kwhbkh0SPnj6xt6xMGHaFw.png" /><figcaption>graphql-playground 的結果</figcaption></figure><h3>結論</h3><p>終於，我們談完了如何在 Laravel 上使用 GraphQL 來向資料庫請求資料，但這只是一個初步的開始，還必須要去思考要如何防止被攻擊以及使用者的身份驗證的問題…等等，這些都值得一在地討論以及研究，還有在 resolve 向資料庫請求或是修改資料時，如何才能夠達到高效率。</p><p>總之，不管是哪種 php 的 Laravel 也好， GraphQL 也好，都只是一款工具，在我們學會了這些工具後，該如何去發揮我們所學，從我們所學的去解決遇到的問題才是一門學問。</p><p>謝謝各位的觀看，如有任何問題或是有寫錯的部分也可以在下方留言。</p><h3>相關參考資料：</h3><p>graphql-laravel：</p><p><a href="https://github.com/rebing/graphql-laravel">rebing/graphql-laravel</a></p><p>Laravel 官方文件：</p><p><a href="https://laravel.tw/docs/5.3">安裝</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d8ce84f2f065" width="1" height="1" alt=""><hr><p><a href="https://medium.com/lion-f2e/%E6%B7%BA%E8%AB%87-%E5%9C%A8-laravel-%E4%B8%AD%E4%BD%BF%E7%94%A8-graphql-%E4%B8%89-d8ce84f2f065">[淺談]在 Laravel 中使用 GraphQL(三)</a> was originally published in <a href="https://medium.com/lion-f2e">LT Lab</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[淺談]在 Laravel 中使用 GraphQL(二)]]></title>
            <link>https://medium.com/lion-f2e/%E6%B7%BA%E8%AB%87-%E5%9C%A8-laravel-%E4%B8%AD%E4%BD%BF%E7%94%A8-graphql-%E4%BA%8C-a315fd40c00b?source=rss----d76981da6b21---4</link>
            <guid isPermaLink="false">https://medium.com/p/a315fd40c00b</guid>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[graphql-playground]]></category>
            <category><![CDATA[xampp]]></category>
            <category><![CDATA[mysql]]></category>
            <category><![CDATA[laravel-5]]></category>
            <dc:creator><![CDATA[Jian-Kai,Kuo]]></dc:creator>
            <pubDate>Mon, 25 Nov 2019 05:49:06 GMT</pubDate>
            <atom:updated>2019-11-25T05:50:52.749Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V9QMa82Aq46W4PzcMp-spw.png" /><figcaption>圖片來自： <a href="https://blog.pusher.com/graphql-laravel/">https://blog.pusher.com/graphql-laravel/</a></figcaption></figure><h3>前言</h3><p>在上一篇淺談中，我們談到了透過 Xampp 架設 Laravel 框架，並且連接 MySQL 中的資料庫，最後透過指令將我們設定的 migraions 在資料庫中產生相對應的資料表。</p><h3>Laravel → factory + seeder → 資料庫新增資料</h3><p>Laravel 提供了可以自動產生資料的方法，透過在 factories 裡設定好要如何產生資料，就能夠在資料庫中自動的產出資料了。</p><p>我們打開 factories 中任意一個檔案會看到裡面空空如也，因為我們需要自己去撰寫資料表中每一筆資料是要如何去產生的。</p><pre>$factory-&gt;define([資料表model]::class, function (Faker $faker) {</pre><pre>    return [</pre><pre>        &quot;name&quot; =&gt; $faker-&gt;name, // Faker 中 隨機的 名稱</pre><pre>        &quot;product_type_id&quot; =&gt; mt_rand(1, 5), // 1~5隨機數字</pre><pre>        &quot;description&quot; =&gt; Str::random(4), // 隨機長度為4的字串</pre><pre>        &quot;price&quot; =&gt; mt_rand(10, 100),</pre><pre>    ];</pre><pre>});</pre><p>所以我們需要針對上次建立好的四個資料表的欄位進行各個欄位的資料填寫，由於資料需要一點隨機性，所以利用 <a href="https://github.com/fzaninotto/Faker">Faker </a>這個套件來隨機產生資料內容。</p><p>Faker 這個套件有許多已經寫好的資料內容可以去取用，詳細資料可以觀看 github 上的文件。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2e4786a01ffb93cf4e7dc408909aa63b/href">https://medium.com/media/2e4786a01ffb93cf4e7dc408909aa63b/href</a></iframe><p>有了 factory 後我們需要有驅動工場的動力， seeder 就是可以驅動工廠的動能。首先，我們透過指令 php artisan make::seeder ProductSeeder ，會在 <strong><em>database/seeds</em></strong> 資料夾下新增 ProductSeeder.php 。</p><pre>$ php artisan make:seeder [Seeder Name]</pre><p>我們需要在 seeder 裡面設定每一個資料表需要產生的筆數或是要在資料庫中直接新增那些資料。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a3959759b7d3b94a19142227baecb1a5/href">https://medium.com/media/a3959759b7d3b94a19142227baecb1a5/href</a></iframe><p>我們需要去 <strong><em>app</em></strong> 資料夾引用相關的 model，然後在 run 函式中設定要產生進去資料庫的資料筆數。</p><p>可以透過下面的指令產生資料：</p><pre>$ php artisan db:seed --class[seeder 的 class Name]</pre><p>執行完上面的指令後，到 MySQL 資料庫就可以看到四個資料表都填入設定好的資料筆數了。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/806/1*B9BBdW7iVVfxJWYrtysAcg.png" /><figcaption>資料表 products 中新增了 50 筆資料</figcaption></figure><p>我們在 <strong><em>database/seeds/DatabaseSeeder.php </em></strong>的 run function 中添加程式碼 $this-&gt;call(ProductSeeder::class); 指定到對應的 seeder ，就可以透過下面的指令，將現有的資料庫從新刷新，產生新的資料表跟資料。</p><pre>$ php artisan migrate:refresh --seed</pre><h3>GraphQL：安裝</h3><p>文章到了這邊終於進入了我們的主題「GraphQL」，到目前為止我們已經透過了 Laravel 成功地在資料庫中產生了幾筆資料，我們就可以透過 GraphQL 進行資料的撈取了。</p><p>首先，我們需要先安裝 GraphQL 的套件以及相關設定：</p><pre>$ composer require rebing/graphql-laravel</pre><p>接者到 <strong><em>config\app.php </em></strong>，在 providers 和 aliases 中加入：</p><pre>// providers<br>Rebing\GraphQL\GraphQLServiceProvider::class</pre><pre>//aliases<br>&#39;GraphQL&#39; =&gt; Rebing\GraphQL\Support\Facades\GraphQL::class</pre><p>接者執行 下面指令來產生 GraphQL 相關的 config 檔：</p><pre>$ php artisan vendor:publish --provider=&quot;Rebing\GraphQL\GraphQLServiceProvider&quot;</pre><p>最後會產生 <strong><em>config\graphql.php</em></strong> 並且看到相關的設定。</p><h3>GraphQL：Schemas</h3><blockquote>GraphQL相較於REST以及其他web service架構提供了一種高效、強大和靈活的開發web APIs的方式。它通過由客戶端根據所需定義資料結構，同時由服務端負責返回相同資料結構的對應數據的方式避免了服務端大量冗餘數據的返回，但與此同時也意味著這種方式不能有效利用起查詢結果的web緩存。GraphQL這種查詢語言所帶來的靈活性和豐富性的同時也增加了複雜性，使得這項技術對於簡單APIs並不是好的選擇。 <br>— 引用至 <a href="https://zh.wikipedia.org/wiki/GraphQL">https://zh.wikipedia.org/wiki/GraphQL</a></blockquote><p>GraphQl 是透過一串 query 指令像資料庫請求需要的相關資料，像是：</p><pre>query {<br>   users(id:5){<br>      id<br>      name<br>      job<br>   }<br>}</pre><p>上面的指令結構告訴了 GraphQL 我們要去 users 資料表中找到 id 為 5 的哪個 user 的 id 、 name 、 以及 job。</p><p>首先，我們先在 <strong><em>app </em></strong>資料夾中新增<strong><em> GraphQL </em></strong>資料夾，我們會將相關的檔案放置在這個資料夾中。</p><p>接者在 <strong><em>app\GraphQL</em></strong> 中新增三個資料夾，分別為 <strong><em>Mutation </em></strong>、 <strong><em>Query </em></strong>以及 <strong><em>Type </em></strong>，三個資料夾中放置的檔案有不同的功用。</p><ol><li><strong><em>Mutation</em></strong> 資料夾：負責資料的更新或是資料新增。</li><li><strong><em>Query </em></strong>資料夾：負責定義查詢時的參數以及處理。</li><li><strong><em>Type </em></strong>資料夾：負責 Query 時回傳的資料格式。</li></ol><p>資料夾建完資後，我們先到 <strong><em>app\Type</em></strong> 中新增 <strong><em>ProductType.php </em></strong>、<strong><em> ProductCategoryType.php </em></strong>以及<strong><em> ProductTagType.php</em></strong> 三個檔案。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/236/1*MTmzK5bwkNuoImWIILPtqA.png" /><figcaption>Type 資料夾</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fb718249f2dbf04fb52a0a90812128de/href">https://medium.com/media/fb718249f2dbf04fb52a0a90812128de/href</a></iframe><p>定義完 Type 後，我們需要定義 Query 來讓 GraphQL 接收我們的 Query 字串。這次的淺談我們只針對商品進行資料的請求，因此我們只在 Query 資料夾中新增 ProductQuery.php ，來獲得商品的資料。若需要得到其他資料表的資料，可以參考 ProductQuery.php 是如何設置的。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6300787673372a1909baa3f0dcb8f2f7/href">https://medium.com/media/6300787673372a1909baa3f0dcb8f2f7/href</a></iframe><p>其中， <strong><em>type </em></strong>函式會從 <strong><em>config\graphql.php</em></strong> 的 type 中找到 product 的資料格式以及型別。<strong><em>resolve </em></strong>函式則是會簡單的判斷 query 的參數有 id 會去找出此 id 的商品，沒有則是回傳整個 products 資料表的商品資料。</p><p>接者要在 <strong><em>config\graphql.php </em></strong>設定 query 時的 schemas 跟 資料回傳時的 types。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/559/1*s9O8bjOEOkK1cTPlMkw6Bg.png" /><figcaption>在 <em>config\graphql.php 引用相關</em><strong><em> class</em></strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*snrwdCaWDsqZNYeDgV4M4Q.png" /><figcaption>在 schemas 中設定 product 的 query</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/452/1*2dxh6ENUpEWim3SCcuUSGA.png" /><figcaption>在 types中設定相關的 type</figcaption></figure><p>完成上述的動作後，我們就完成了基礎的 GraphQL 的設定了，接者我們需要在安裝測試工具來測試我們的 GraphQL 能不能作用了。</p><h3>GraphQL：安裝測試工具及測試</h3><p>這裡我們使用 <a href="https://github.com/mll-lab/laravel-graphql-playground">mll-lab/laravel-graphql-playground</a> 來測試我們設定好的 GraphQL 能不能作用。</p><pre>$ composer require mll-lab/laravel-graphql-playground</pre><p>在 <strong><em>config\app.php </em></strong>中加入 provider</p><pre>&#39;providers&#39; =&gt; [<br>    // Other providers...<br>    MLL\GraphQLPlayground\GraphQLPlaygroundServiceProvider::class,<br>]</pre><p>接者執行下面指令，產生 <strong><em>config/graphql-playground.php</em></strong> 。</p><pre>php artisan vendor:publish --provider=&quot;MLL\GraphQLPlayground\GraphQLPlaygroundServiceProvider&quot; --tag=config</pre><p>最後在瀏覽器打開 <a href="http://graphql.local/graphql-playground">http://graphql.local/graphql-playground</a> 就可以看到 GraphQL 的測試環境了。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U1Fw57Py0e5uqq5SANt8-w.png" /><figcaption>GraphQL 測試環境</figcaption></figure><p>測試看看可不可以從資料庫拿到資料。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dWegv7QSTw8D4NpRTFenCA.png" /><figcaption>成功拿到全部 product 資料</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8VX0pItCMFY0yvvHyvCvWA.png" /><figcaption>成功拿到 id 為 5 的 product 資料</figcaption></figure><p>這次的淺談，我們談到如何使用 factory 加上 seed 來產生資料集，接著透過 GraphQL 在資料庫裡面請求資料。而我們在這次也終於有提到了 GraphQL 的相關設定以及程式碼的演示，當我們將 Query 的 schema 結構定義清楚以及 resolve 該做出甚麼動作，我們就可以透過 GraphQL 來向資料庫請求資料了。</p><p>下一次的淺談，我們會談到如何處理資料表之間的關聯，以及如果要新增或式修改資料時該如何去處理。</p><p>謝謝各位的觀看，如有任何問題或是有寫錯的部分也可以在下方留言。</p><p>相關參考資料：</p><p>graphql-laravel：</p><p><a href="https://github.com/rebing/graphql-laravel">rebing/graphql-laravel</a></p><p>laravel-graphql-playground：</p><p><a href="https://github.com/mll-lab/laravel-graphql-playground">mll-lab/laravel-graphql-playground</a></p><p>在 Laravel 中使用 GraphQL 一【获取数据】：</p><p><a href="https://learnku.com/articles/8115/using-graphql-one-in-laravel-get-data">在 Laravel 中使用 GraphQL 一【获取数据】</a></p><p>多面體工作室 Dometi — Laravel</p><p><a href="https://dometi.com.tw/blog/category/%E7%B6%B2%E9%A0%81/%E7%B6%B2%E9%A0%81%E5%BE%8C%E7%AB%AF/laravel/">Laravel 彙整 - 多面體工作室 Dometi</a></p><p>Laravel 官方文件</p><p><a href="https://laravel.tw/docs/5.3">安裝</a></p><h3><strong>備註：</strong></h3><p>如果有遇到 Laravel 無法解析 class 的問題，可以參考下面連結的解決方法。</p><p><a href="https://stackoverflow.com/questions/32475892/reflectionexception-class-classname-does-not-exist-laravel#comment52814653_32475892">ReflectionException: Class ClassName does not exist - Laravel</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a315fd40c00b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/lion-f2e/%E6%B7%BA%E8%AB%87-%E5%9C%A8-laravel-%E4%B8%AD%E4%BD%BF%E7%94%A8-graphql-%E4%BA%8C-a315fd40c00b">[淺談]在 Laravel 中使用 GraphQL(二)</a> was originally published in <a href="https://medium.com/lion-f2e">LT Lab</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[淺談]在 Laravel 中使用 GraphQL(一)]]></title>
            <link>https://medium.com/lion-f2e/%E6%B7%BA%E8%AB%87-%E5%9C%A8-laravel-%E4%B8%AD%E4%BD%BF%E7%94%A8-graphql-%E4%B8%80-23bbe1b54d3a?source=rss----d76981da6b21---4</link>
            <guid isPermaLink="false">https://medium.com/p/23bbe1b54d3a</guid>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[xampp]]></category>
            <category><![CDATA[laravel-framework]]></category>
            <category><![CDATA[php]]></category>
            <dc:creator><![CDATA[Jian-Kai,Kuo]]></dc:creator>
            <pubDate>Fri, 22 Nov 2019 07:24:18 GMT</pubDate>
            <atom:updated>2019-11-22T07:24:17.931Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V9QMa82Aq46W4PzcMp-spw.png" /><figcaption>圖片來自： <a href="https://blog.pusher.com/graphql-laravel/">https://blog.pusher.com/graphql-laravel/</a></figcaption></figure><h3>前言</h3><p>有許多人會問，為什麼要使用 GraphQL ，而不是直接使用傳統的 RESTful API 就好了呢？簡單的在 Google 上谷歌 GraphQL 優勢或好處，自然就會看到許多文章，或許你也是從這裡看到這篇文章也不一定。</p><p>稍微簡介一下， GraphQL 與 RESTful API 不同的地方是， GraphQL 可以更直覺的對資料庫進行資料的要求，跟資料庫下 SQL 一樣，設定好 select 、 from 、 where 就能夠要求到需要的資料。在這個優勢下，以往一個頁面可能需要請求三到四支 API 才能夠得到足夠渲染的資料， GraphQL 可能只需要向後端伺服器請求一次就能夠獲取足夠的資料，能夠省去前後端溝通的時間及次數。</p><p>那為什麼會有這篇文章呢？簡單來說就是因為「潮」， Github 、 Shopify 、 Coursera 、 Twitter 等大型的公司都開始在用了，那我們也要來嘗試看看。</p><p>廢話不多說就開始淺談這次的教學。</p><h3>XAMPP + Laravel</h3><p>一開始我們需要先有能夠運行 php 的環境來執行 Laravel 的專案。</p><p>這裡我們採用的是 <a href="https://www.apachefriends.org/zh_tw/index.html">Xampp</a> ，Xampp 裡面包含了很多東西，其中有 MySQL 可以當作是存取的資料庫。若有自己已經有資料的資料庫，也可以使用自己熟悉的資料庫。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EvTcNUmUIPOXLHwr5oqyaw.png" /><figcaption>Xampp 的下載畫面，網址： <a href="https://www.apachefriends.org/zh_tw/index.html">https://www.apachefriends.org/zh_tw/index.html</a></figcaption></figure><p>下載完後，開啟 XAMPP Control ，把 Appche 與 MySQL 起動。啟動之後就可以打開瀏覽器前往 <a href="http://localhost/dashboard/">http://localhost/dashboard/</a>，會看到 Xampp 的介紹頁。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/723/1*9QeDW3TNKZJuM3uvqkz1iA.png" /><figcaption>Xampp 啟動 Apache 與 MySQL</figcaption></figure><p>接下來我們要去安裝 <a href="https://laravel.com/">Laravel </a>，是一個 PHP 的框架。在這之前需要先去安裝 <a href="https://getcomposer.org/">Composer</a> ，我們需要透過 Composer 來管理套件的相依性。</p><p>首先，使用 Composer 下載 Laravel installer：</p><pre>composer global require &quot;laravel/installer&quot;</pre><p>然後在指定的目錄下新增 Laravel 專案：</p><pre>laravel new GraphQL</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g1ZZQni14nEO67_zAXht0Q.png" /><figcaption>新增 Laravel 專案</figcaption></figure><p>這裡我們是透過 Apache 來執行我們的專案，所以需要做一些設定。先到 <strong><em>xampp/apache/conf/httpd.conf</em></strong> 以及 <strong><em>xampp/apache/conf/extra/httpd-vhosts.conf </em></strong>設定 Apache 的虛擬站台，這樣可以在瀏覽器看到我們專案的成果。</p><ul><li>在 <strong><em>xampp/apache/conf/httpd.conf </em></strong>確保 Include conf/extra/httpd-vhosts.conf 沒有備註解掉。</li><li>在 <strong><em>xampp/apache/conf/extra/httpd-vhosts.conf </em></strong>我們要去新增我們的虛擬站台。在 http-vhosts.conf 中加入：</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/850f29089b07d809c136e2907ae6be06/href">https://medium.com/media/850f29089b07d809c136e2907ae6be06/href</a></iframe><p>之後，我們去 C:\Windows\System32\drivers\etc\hosts 設定 hosts ，需要在最下面加上 127.0.0.1 GraphQL.local ，需要有系統管理員才能修改檔案。</p><p>全部設定完後打開瀏覽器到 <a href="http://graphql.local/">http://graphql.local/</a> 就能夠看到大大的 Laravel了，也就完成了基礎的設定了。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U257fIV8m7Sv0M_hPczwDA.png" /><figcaption>設定完後，看到Laravel 初始頁面</figcaption></figure><h3>Laravel → migratons → 資料庫新增資料表</h3><p>GraphQL 是要從資料庫中去獲取資料，因此我們需要先有一個資料庫，而且資料庫裏面需要有一小筆的資料。</p><p>首先我們在 Xampp 的控制面板上，點擊 MySQL 上 Admin 按鈕，打開 phpMyAdmin ，新增一個新的資料庫叫做 GraphDB ，我們會將資料寫入這個資料庫。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xOoQ5MpSF3VFqHood43ABQ.png" /><figcaption>MySQL 在 php 上的控制板</figcaption></figure><p>我們在資料庫中會新增 products 、 product_tags 、 product_category 、 product_tag_related 這四張資料表，來表達商品的種類、標籤等等關係。</p><p>首先要先到 .env 檔案中，設定要連接的資料庫、使用者名稱以及密碼。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/691/1*ya_rUnkqc-yXWAjHB8XCUA.png" /></figure><p>接者我們需要建立 model 與資料庫中的資料表互動，因此我們需要執行下列命令：</p><pre>$ php artisan make:model Product -fmr<br>$ php artisan make:model ProductTag -fmr<br>$ php artisan make:model ProductCategory -fmr<br>$ php artisan make:model ProductTagRelated -fmr</pre><p>上述每個指令後面的 -fmr 可以讓我們在產生 model 時，同一時間產生相對應的 migration 、 factory 。 migration 可以自動地在資料庫中產生資料表，而 factory 則是可以在資料表中自動地產生資料。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/419/1*yf_c0RKh2jG-kAoB417gLQ.png" /><figcaption>利用指令產生的 model 、 migration 以及 factory</figcaption></figure><p>接者打開每一個 migration ，我們要開始寫 code 了。Laravel 會根據 migration 在資料庫中建立對應的資料表。</p><p>在 migration 中會的 class 中會有 up 跟 down 兩個函式，分別代表建立資料表以及丟棄資料表。</p><pre>// 建立資料表<br>up(){ <br>    <em>Schema</em>::create([你的資料表名稱], function(Blueprint $table){<br>        $table-&gt;bigIncrements(&#39;id&#39;);  // 設定表格的欄位名稱以及欄位型別<br>    })<br>}</pre><pre>// 丟棄資料表<br>down(){ <br>    <em>Schema</em>::dropIfExists([你的資料表名稱])<br>}</pre><blockquote>欄位型別可以參考 Laravel 的<a href="https://laravel.com/docs/5.5/migrations">官方文件</a>。</blockquote><ol><li>product 的 migration 會建立 product 資料表的各個欄位以及型別。依此類推 product_tag 、 product_type 、 product_tag_related。</li><li>建立 product_tag_related 的目的是要能夠在資料庫中可以連接 product 以及 product_tag ，是屬於關聯式資料庫中 ER-model 中的 R 。</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1b6f441497fb0d0855747463fe6a4f85/href">https://medium.com/media/1b6f441497fb0d0855747463fe6a4f85/href</a></iframe><p>接者就可以透過指令來在資料庫中建立資料表。</p><p>執行所有未完成的migrations</p><pre>php artisan migrate</pre><p>還原最後的migration動作</p><pre>php artisan migrate:rollback</pre><p>還原指定次數的migration動作</p><pre>php artisan migrate:rollback --step=5</pre><p>還原所有的migrations</p><pre>php artisan migrate:reset</pre><p>回到 phpMyAdmin ，進入 graphql 資料庫，我們可以看到透過指令建立的四個資料表。其中會看到 migrations 資料表，是用來記錄到目前為止各個資料表修改的次數，可以用來回朔資料表的狀態。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/999/1*t8jXEjlRmz3EC1nzLGRNew.png" /><figcaption>透過指令產生的四個資料表</figcaption></figure><p>這次的淺談，我們談到如何透過 Laravel 在 MySQL 上建立以及新增資料表。透過上述的方式，雖然前置動作有點多，但是我們可以自動化的產生資料表，也能夠回朔；當測試階段到了一定程度就可以把測試的假資料丟棄，或是開發階段發生錯誤，也可以把資料庫回到正確的時候。</p><p>下一次的淺談，我們會談如何透過 factory 跟 seeder 來自動的產生假資料，以及如何用 GraphQL 取得資料庫中的資料。</p><p>謝謝各位的觀看，如有任何問題或是有寫錯的部分也可以在下方留言。</p><p>相關參考資料：</p><p>[分享] Laravel</p><p><a href="https://medium.com/@app030242/2fb4431b8eb8">[分享] Laravel</a></p><p>多面體工作室 Dometi — Laravel</p><p><a href="https://dometi.com.tw/blog/category/%e7%b6%b2%e9%a0%81/%e7%b6%b2%e9%a0%81%e5%be%8c%e7%ab%af/laravel/">Laravel 彙整 - 多面體工作室 Dometi</a></p><p>Laravel 官方文件</p><p><a href="https://laravel.tw/docs/5.3">安裝</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=23bbe1b54d3a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/lion-f2e/%E6%B7%BA%E8%AB%87-%E5%9C%A8-laravel-%E4%B8%AD%E4%BD%BF%E7%94%A8-graphql-%E4%B8%80-23bbe1b54d3a">[淺談]在 Laravel 中使用 GraphQL(一)</a> was originally published in <a href="https://medium.com/lion-f2e">LT Lab</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[心得] 程式Online(一)]]></title>
            <link>https://medium.com/lion-f2e/%E5%BF%83%E5%BE%97-%E7%A8%8B%E5%BC%8Fonline-%E4%B8%80-d7837cd7ab48?source=rss----d76981da6b21---4</link>
            <guid isPermaLink="false">https://medium.com/p/d7837cd7ab48</guid>
            <category><![CDATA[experience]]></category>
            <category><![CDATA[code]]></category>
            <category><![CDATA[engineer]]></category>
            <dc:creator><![CDATA[chengyucai]]></dc:creator>
            <pubDate>Sun, 22 Sep 2019 06:42:49 GMT</pubDate>
            <atom:updated>2019-09-22T06:50:01.398Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2PkkqFD3WyMIwiYwzkdBJg.png" /></figure><blockquote><strong>前言</strong></blockquote><p>從高中開始學習程式到現在，從還是學生時期一路銜接至工作，已經有七年了，主要會想來撰寫這一篇，是想給剛剛進路這領域的「萌新」們及想踏入到的人，分享一些建議與方向，在這條道路上可能會發生的事。</p><h3>天賦</h3><p>首先，請你先問自己你適合成為軟體工程師嗎…，對~不要懷疑對這很重要，有人可能會想問你在開玩笑，我根本都還不曉得才來你這篇，現在要我問我自己合不合適，我哪知道啊～。老實說分辨法很簡單，你不一定要是一位程式天才或是什麼此領域的相關特長，你只需要具備以下特點就已經是一名合格的工程師:</p><p><strong>熱忱：</strong>寫程式就跟蓋房子一樣是由小小的物體推砌而成，彼此互相連接，互相輔助來達成一個目標，但是藍圖永遠只是一個目標，如何實現是靠你自己，途中絕對會遭遇挫折，你會從亂無章法的程式邏輯、工作時被電爆，這都是必經之路，熱忱是給了你能持續站起來的關鍵，像是遊戲中的續命道具一樣讓你不斷的浴火重生。</p><p><strong>學習：</strong>科技的進步與改變非常的快速。沒幾個月就會有新的語言、框架及工具與技術。因此相較於其他職業，網路開發者或軟體工程師是需要一直不斷的學習，並且與時並進，才能確保自己的能力，這種環境會帶來巨大的壓力，但也帶來刺激與挑戰。</p><p><strong>追求：</strong>會想來當工程師的人大多數可能是因為：工作機會、薪水、充滿未知與挑戰、發揮自身的價值…，這些動機都很好，寫程式其實也是一種對「欲」的追求，對於一樣事物的執著，希望以完美的方式呈現，無形間增加你自身的價值，並對於未知的事物充滿探索與求知，投身於「知識」的大海。</p><blockquote><strong>總結</strong></blockquote><p>事實部分的軟體工程師都可能會有這樣的疑惑，『我到底是不是一位軟體工程師』還是就只是會寫一點code而已，其實有一大部分的工程師並不是擁有相關學位的，可能曾經是位水電工、廚師或是一位醫師，對於自身能力並沒有有效的基準來衡量，往往在挫折與失敗中迷失了自我，或是同業的鄙視，對於『沒有對應的學位就不是一位軟體工程師?』這詞顯得更加憂愁。其實你可以試著放下心中芥蒂，問問自己，自己所認定的工程師是什麼，是有頂令人稱羨的學歷 ? 還是有一張漂亮的經歷 ? 我認為都不是；工程師就是一位能解決問題才能叫工程師，不能解決問題甚至逃避那與打嘴砲無異，只要你現在是在處理著問題，那麼不管別人怎麼說你就是一位工程師。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d7837cd7ab48" width="1" height="1" alt=""><hr><p><a href="https://medium.com/lion-f2e/%E5%BF%83%E5%BE%97-%E7%A8%8B%E5%BC%8Fonline-%E4%B8%80-d7837cd7ab48">[心得] 程式Online(一)</a> was originally published in <a href="https://medium.com/lion-f2e">LT Lab</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[lerna — JS package 管理工具]]></title>
            <link>https://medium.com/lion-f2e/lerna-js-package-%E7%AE%A1%E7%90%86%E5%B7%A5%E5%85%B7-e9ed360d1143?source=rss----d76981da6b21---4</link>
            <guid isPermaLink="false">https://medium.com/p/e9ed360d1143</guid>
            <category><![CDATA[lerna]]></category>
            <category><![CDATA[verdaccio]]></category>
            <category><![CDATA[monorepo]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[npm]]></category>
            <dc:creator><![CDATA[DEBUG]]></dc:creator>
            <pubDate>Fri, 06 Sep 2019 00:31:02 GMT</pubDate>
            <atom:updated>2022-01-08T05:28:09.340Z</atom:updated>
            <content:encoded><![CDATA[<h3>lerna — JS package 管理工具</h3><p>是不是覺得發布套件到 NPM 上距離很遙遠，程式碼總是 Copy Paste 來 Copy Paste 去，最後搞到加班發大財，總覺得自己是草包離大神們好遠，就讓 lerna 帶你跳上巨人肩膀…</p><p>開始之前，需要先了解以下知識</p><ul><li>何謂 mono-repo 及 multi-repo</li><li>git 與 npm 基本操作及流程</li><li>typescript 基本設定</li></ul><p>會選擇 mono-repo 的原因，在於每個 package 都是簡單的小元件，首先應用在前端 UI 元件及功能上的實現，由於只有一個 repo 也易於管理及維護這些 package。</p><p>已知管理 mono-repo 的工具有幾套 bolt、lerna、rush…，最終選擇 lerna， 主要原因在於旁邊有大神(有普渡有感應)可以指引我一盞明燈，工具本身都有其優缺點，在開發過程中能駕馭，並順利解決問題及完成專案就是好工具。以下附上這幾個工具的趨勢數據。</p><p><a href="https://www.npmtrends.com/bolt-vs-lerna-vs-rush">bolt vs lerna vs rush | npm trends</a></p><p>接下來以 lerna 的使用操作為主，lerna 本身結合 git 及 npm，指令雖然不多，但過程卻非常飄渺，整個專案會以 ts 方式撰寫，套件的安裝使用 yarn 為主。</p><p>首先需要安裝 lerna 於全域環境。</p><pre>yarn global add lerna</pre><p>建立資料夾並做 lerna 的初始化動作。</p><p>lerna init 預設的模式為 Fixed，Fixed 模式下所有 package 固定一個版本號。另一個模式為 independent，每個 package 都有自己的版本號。意思是在專案下會有 package-01、package-02、package-03 …，當前版本均為 1.0.0，突然有使用者反映 package-01 有 bug，於是 F2E 立馬修正，此時在 Fixed 模式下，所有 package 版本均升級到了 1.0.1，沒更新的也升級了，是不是覺得怪怪的。但在 independent 下，只有 package-01 升級到1.0.1，其他能維持在 1.0.0。初始化完後，如需要更改模式，可以透過 lerna.json 做修改。</p><pre>mkdir lerna-repo<br>cd lerna-repo<br>lerna init --independent</pre><p>初始完會產生以下結構，packages 是一個空目錄，放置的內容為要開發的 package。lerna.json 就是透過 lerna 來管理旗下 packages 的設定檔。</p><pre>- packages/<br>- lerna.json<br>- package.json<br>- .git</pre><p>以下針對 lerna.json 及 package.json 做基本設定，如果使用 yarn，需要注意useWorkspaces 參數，攸關是否將 node_modules 統一管理。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/40119f4f8ea7220cfea410d072607070/href">https://medium.com/media/40119f4f8ea7220cfea410d072607070/href</a></iframe><p>接下來建立一個 package，packages/ 下會多一個 package-01 的資料結構。</p><pre>lerna create package-01 -y</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/528/1*k03yuEcnC1-vFmGc_hXANA.png" /></figure><p>可以不透過指令，直接在 packages 下按右鍵，建立資料夾，需手動做 yarn init -y，原因是專案下認的是 package.json。</p><p>package.json 有個很重要的欄位 name，name 的使用時機是專案在引用時，會依此名稱。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/784/1*JSFM920Use-QLazaEHIoHg.png" /></figure><p>整個 packages 下的 package，建議 name 可以使用一個前綴詞(<strong><em>@taiwan</em></strong>)，諸如<strong><em>@taiwan</em></strong>/package-01，用意在於在引用這些 package 時，node_modules 會統一歸類在一個資料夾下。可以參考如下 babel 的 package( babel 也使用 lerna !!)。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/568/1*2YsFQ16K4HZIcF7ZFMvyqQ.png" /></figure><p>基本的環境都設定好後，開始安裝相依的套件，諸如 react 及 react-dom。我們之後要做的 package 都是基於 react 下，所以我們在專案的根目錄下執行以下指令。</p><pre>yarn add react react-dom</pre><p>上述會報出一個錯誤</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*APM260YnDz-SZ6YzuYNobg.png" /></figure><p>指令需要 -W 參數</p><pre>yarn add react react-dom -W</pre><p>每個 packages 下都依賴 react 及 react-dom，執行以下指令，每個 package.json 的 dependencies 下會有 react 及 react-dom。當然也可以手動方式加入到 package.json。</p><pre>lerna add react react-dom</pre><p>也可以指定相依套件，要安裝在哪個 packages 下</p><pre>lerna add react --scope packages/package-01</pre><blockquote>補充：package.json 中 peerDependencies</blockquote><blockquote>一般專案開發下，很常見dependencies 及 devDependencies ，差別在於是否在開發測試的狀態與否。如果是開發 package 讓多個專案共用的話，可能要思考是否使用 peerDependencies 取代 dependencies ，諸如 react 版本，在使用此 package 時，先確認要引入專案的 react 版本，如果達不到套件需求版本，就終止安裝。</blockquote><blockquote>dependencies 其他種類可參考如下<br><a href="https://classic.yarnpkg.com/en/docs/dependency-types/">https://classic.yarnpkg.com/en/docs/dependency-types/</a></blockquote><p>當相依套件加入 package 後會產生 node_modules，如果每個node_module 下都有 react 及 react-dom 的套件，整個專案會變得非常肥，可以加入 hoist 參數，讓套件統一根目錄管理，以下指令同於 yarn 或 npm install。</p><pre>lerna bootstrap --hoist</pre><p>如果是使用 yarn 的會報錯，會建議使用 workspace 替代，上述的設定以使用 workspace 方式，因此不需要加 hoist 參數。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*ShuQREfFFvSqyudUuz32Gw.png" /></figure><p>如果要清除所有專案下的 node_modules</p><pre>lerna clean</pre><p>這時可以嘗試先 build 出一個版本，在 build 一個新版本時，需要有一個遠端的 git repo 並確定連結及提交所有變更的 commit，如果沒有將無法進行後續。</p><pre>lerna version</pre><p>如果模式為 independent，會逐一詢問版本的狀態，bug (patch) 修正可能 0.0.1 開始往上加，新功能 (minor) 加入可能 0.1.0 往上加，大改版 (major)可能就是 1.0.0 往上加，也可以自訂版本等。設定完後，他會自動 push 到遠端 git repo。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ci6Oa_s_FNi5bQw7RtGpSg.png" /></figure><p>到目前為止只是 build 出個版本號過過癮，別人還是無法用你的 package。接下來會從如何開發 package 開始，及開發完後如何在本地引入，最後開啟自建 npm server 引入自己做的 package 到專案中。</p><p>在開發 package 前，還需要前置作業及環境的設定，首先需要安裝 storybook，展示我們開發 package 的成果以及加入一點開發難度全部使用 ts 開發，便於我們後續程式開發的統一及維護性。</p><blockquote>webpack 的編譯是使用 node，如要使用 ts 需安裝 ts-node<br><a href="https://medium.com/webpack/unambiguous-webpack-config-with-typescript-8519def2cac7">https://medium.com/webpack/unambiguous-webpack-config-with-typescript-8519def2cac7</a></blockquote><p>storybook 及 typescript 的安裝，可參考<a href="https://storybook.js.org/docs/guides/guide-react/">官網</a>，這邊不一一列出安裝方式。設定檔(webpack.config.js)副檔名 js 直接轉成 ts，會有問題應該都是缺型態，將型態暫時設為 any。</p><p>記得安裝的套件，後面要加 -W</p><pre>yarn add <a href="http://twitter.com/storybook/react">@storybook/react</a> -D <strong>-W</strong></pre><blockquote>補充：</blockquote><blockquote><a href="https://github.com/storybookjs/storybook/tree/master/addons/notes">storybook notes 插件</a>，用途是加入備註及說明，讓開發者能透過說明了解其使用方法，這邊也可以引入 markdown，引入 markdown，會有 typescript 的錯誤，會找不到 .md 的定義檔案。</blockquote><pre>import markdownNotes from &#39;../readme.md&#39;;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/894/1*U-zO7iYnEEkBIlI7mlaQXQ.png" /></figure><p>建議在根目錄下創建 typings 目錄，加入以下檔案 (還加入了 scss 的定義檔，主要在做 css modules 會使用到)。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5aa891f5f7784e1cb610ddd342a549fe/href">https://medium.com/media/5aa891f5f7784e1cb610ddd342a549fe/href</a></iframe><p>加入後有問題，可能需要確認 tsconfig.json 的 include 是否包含 typings 的目錄。</p><p>接下來，我們來創建第一個 package 名為 Footer</p><pre>cd packages<br>mkdir Footer<br>yarn init -y</pre><p>查看是否有此 package</p><pre>lerna list </pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/528/1*7ZhL2wDC-_XSOY-vjjs0yQ.png" /></figure><p>可以開始建立裡面的內容，storybook 上如何呈現就不加贅述，可以定義在每個 package 下 xxx. stories.tsx，也可以獨立開一個資料夾來放 package 元件，差別可能可以戰 TDD 的流程是否好壞(暈…)</p><pre>mkdir src<br>cd src <br>touch Module.tsx </pre><p>Module.tsx 的內容，顯示 Footer 字樣</p><pre>import * as React from &#39;react&#39;;</pre><pre>const Module = () =&gt; {<br>      return (&lt;div&gt;Footer&lt;/div&gt;)<br>}<br>export default Module;</pre><p>在 Footer 目錄下會建立一個入口點 index.ts，內容只是引入剛剛建立的Module.tsx</p><pre>import Module from &#39;./src/Module&#39;;</pre><pre>export default Module;</pre><p>可以試著查看目前成果</p><pre>yarn storybook</pre><p>但離發布套件給自己用，還有一小段距離，有點敏感度的人應該知道要將 ts 、tsx、scss 等，編譯成瀏覽器能夠懂的語言 js 及 css。這邊就以 tsc 方式實作，先排除 css 的部分，如需要可以使用 webpack 方式做編譯。</p><p>tsc 就是 typescript cli ，可以將 typescript 裝在全域。之後著重在兩隻檔案的操作上，tsconfig.json 及 package.json。</p><p>可以將建立在根目錄的 tsconfig.json 複製過來，調整一下，編譯完之後的位置(這邊放置於 lib)，需要編譯的檔案( src 下及 index.ts )</p><pre>{<br>  &quot;compilerOptions&quot;: {<br>    &quot;outDir&quot;: &quot;lib&quot;,<br>  },<br>  &quot;include&quot;: [<br>    &quot;../../typings/**/*&quot;,<br>    &quot;src/**/*&quot;,<br>    &quot;index.ts&quot;<br>  ]<br>}</pre><p>看似沒用的 package.json 才是關鍵角色</p><pre>{<br>  // 套件的使用名稱這邊加入前綴，可以下 lerna list，查看套件名稱變化<br>  &quot;name&quot;: &quot;@taiwan/footer&quot;, <br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  <br>  // 程式入口點，須為編譯後的入口<br>  &quot;main&quot;: &quot;lib/index.js&quot;,<br>  <br>  // 告知定義檔<br>  &quot;types&quot;: &quot;lib/index.d.ts&quot;,<br>  &quot;license&quot;: &quot;MIT&quot;, <br>  <br>  // 編譯指令<br>  &quot;scripts&quot;: {<br>    &quot;build-tsc&quot;: &quot;tsc&quot;<br>  }, </pre><pre>  // 需要上 npm server 的檔案列表，須為編譯後結果(package.json很重要，別忘了)，跟 Git 上原始碼有別<br>  &quot;files&quot;: [<br>    &quot;lib&quot;,<br>    &quot;package.json&quot;<br>  ]<br>}</pre><p>在 Footer 目錄下執行</p><pre>yarn build-tsc</pre><pre>如果要 build 整個專案<br>lerna run build-tsc</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/442/1*yyLX7b3a8LqkumuOw-FjyA.png" /></figure><p>接下來就是享用自建的 package 的時間。</p><p>接下來偷吃步一下，直接使用 create-react-app，來引用 @taiwan/footer，引用的方式有幾種，一種是透過 yarn link (捷徑的概念，檔案會在~/.config/yarn/link)，另一種是採相對或絕對路徑的方式，這邊以相對路徑實作。</p><p>到你的 create-react-app 下，安裝你自建的 Footer 的資料夾位置</p><pre>yarn add ../lerna-repo/packages/Footer</pre><p>這時到 App.js 引入，要使用 package name @taiwan/footer</p><pre>import Footer from &#39;@taiwan/footer&#39;;</pre><p>Footer package 本地測試成功。</p><p>接下來就是部署到自己私有的 npm server，這邊使用的是 <a href="https://github.com/verdaccio/verdaccio">verdaccio</a>。</p><p>需要裝在全域環境</p><pre>yarn global add verdaccio</pre><p>並執行 verdaccio 服務，會產生一個 localhost 的連結</p><pre>verdaccio</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7xHImHe8w9ddkTST862fbQ.png" /></figure><p>首先需要註冊一個帳號，需要輸入帳號密碼及 email</p><pre>npm adduser --registry <a href="http://localhost:4873/">http://localhost:4873</a></pre><blockquote>補充：</blockquote><blockquote>現在的 node 安裝，都建議使用 nvm 來管理與安裝，這邊就來註記幾個基本指令</blockquote><blockquote>nvm ls-remote 遠端版本<br>nvm ls 本地版本<br>nvm use [version] or nvm use node 使用特定版本</blockquote><p>這時先別急著做 publish 的動作，原因是有可能你本機已設定官方 npm public 的<a href="https://www.npmjs.com/">連結</a>，這時 publish 就會發布出去，要刪除需要填申請單。這邊會使用 nrm 管理我們的私有 npm server。</p><pre>yarn global add nrm</pre><p>列出可用管理工具</p><pre>nrm ls </pre><p>加入私有 npm server 連結，並使用</p><pre>nrm add yarn <a href="http://localhost:4873/">http://localhost:4873</a><br>nrm use yarn</pre><p>可以針對單一 package 進行發布</p><pre>yarn publish</pre><p>對所有已更新 package 發布</p><pre>lerna publish</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fuCLvjULxft_mLxp5YGpgg.png" /></figure><p>發布成功後，即可看見 @taiwan/footer</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6hK2AXCQdaFNqLTuzr7f-w.png" /></figure><p>在本機執行 yarn add @taiwan/footer ，如果找不到，就會到對外的管理套件做下載。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eSodWgtAyX2vgcXrfHydyQ.png" /></figure><p>這篇介紹忽略了需多步驟，整個操作下來會有許多懵懵懂懂的地方，很多細節需要花時間踩過才知道。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e9ed360d1143" width="1" height="1" alt=""><hr><p><a href="https://medium.com/lion-f2e/lerna-js-package-%E7%AE%A1%E7%90%86%E5%B7%A5%E5%85%B7-e9ed360d1143">lerna — JS package 管理工具</a> was originally published in <a href="https://medium.com/lion-f2e">LT Lab</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[撰寫API的利器-apidoc]]></title>
            <link>https://medium.com/lion-f2e/%E6%92%B0%E5%AF%ABapi%E7%9A%84%E5%88%A9%E5%99%A8-apidoc-8fda7bca944e?source=rss----d76981da6b21---4</link>
            <guid isPermaLink="false">https://medium.com/p/8fda7bca944e</guid>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[apidoc]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[錢端工程師ＪＷ (Jinwei)]]></dc:creator>
            <pubDate>Thu, 29 Aug 2019 09:57:03 GMT</pubDate>
            <atom:updated>2019-09-06T09:08:29.227Z</atom:updated>
            <content:encoded><![CDATA[<p><strong><em>身為前端工程師的我們，在寫專案的過程中你是否也有需要&quot;通靈&quot;的時候? 或許有或許沒有，當我一開始進到 Lion F2E時，前輩常講你來這邊學會的第一件事就是&quot;通靈&quot;XD</em></strong></p><h4>所以開發專案需要&quot;通靈&quot;到底是什麼意思?</h4><p><strong>一個專案開始進行時後端在api還沒完成的情況下，前端套資料時必須要猜測後端可能之後開出的資料格式的狀況。或許這種狀況比較容易發生在大公司，當前後端工作需求項目不同時，前端開發進度會比較快(像我們team就是這樣，後端都還沒開始開發api。前端已經切完版了，但第二階段串接api卻無法進行下去)</strong></p><h4>在需要&quot;通靈&quot;的狀況下，到底怎樣才能讓協同開發專案變得更有效率呢?</h4><p>剛剛提到因為後端api還沒開始寫，因此我們只好自己先&quot;通靈&quot;寫api，雖然可能跟後端給的api會不太一樣，也可能非常不一樣，但我們至少可以用真正的api，並且讓畫面有資料。講到這邊，就要介紹撰寫api的好工具 — apidoc</p><h4>Apidoc簡介與優點</h4><ul><li>透過源代碼中的注釋來生成API文檔</li><li>可以追溯不同版本寫的api並進行比對</li></ul><p>先來看看apiDoc的語法與呈現</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/939/1*mSGUd9D0iSZtv-gJYSia1A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sK9lEU75-SAEY_L8XHOUdQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jNNWmvoL7Nf8eFyA408bFg.png" /><figcaption>▲apiDoc原始代碼與網頁上的呈現狀況</figcaption></figure><h4>apiDoc 使用流程</h4><p>Step 1: 安裝 apidoc</p><pre>npm install apidoc -g</pre><p>Step 2: 在專案資料夾中新增apidoc.json的檔案</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/256/1*Up8Fh96qJmuk0tFdOgYHeQ.png" /><figcaption>在newApi2中創建apidoc.json資料夾</figcaption></figure><pre>apidoc.json</pre><pre>{</pre><pre><em>   &quot;name&quot;</em>: &quot;測試的Api文件&quot;,</pre><pre><em>   &quot;version&quot;</em>: &quot;0.1.0&quot;,</pre><pre><em>   &quot;description&quot;</em>: &quot;API 文件&quot;,</pre><pre><em>   &quot;title&quot;</em>: &quot;測試的Api文件&quot;,</pre><pre><em>   &quot;url&quot;</em>: &quot;https://localhost:3000/api&quot;</pre><pre>}</pre><p>Step 3: 加入欲讀取的api文件</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/249/1*-c_86qp-PnIjioVcSforXA.png" /><figcaption>加入myApp並把api文件檔案放進來(cart.js)</figcaption></figure><p>cart.js長相</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/931/1*R0h5wi6YoffPGYzHR5fqLA.png" /></figure><p>可以發現內容部分裡面都是用註解的方式</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-843iX7UgS4kJ7Chm3EZjw.png" /></figure><p>api {CRUD方法(post)} url後綴(/api/cart/add) 項目標題(新增至購物車) <br>apigroup 表示api大項目<br>apiParam 參數</p><p>Step 4: 下指令產出頁面</p><p>newApi2的目錄底下輸入以下指令產出整理過後的API畫面</p><pre>$apidoc -i myapp/ -o apidoc/</pre><pre>$apidoc -i 輸入的api文件夾/ -o 輸出的資料夾名稱/</pre><pre>i: input<br>o: output</pre><pre>如果還有自訂的template可以使用多加入template的指令(-t)<br>$apidoc -i myapp/ -o apidoc/ -t mytemplate/</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/324/1*q5GY48wKVcAHSCtMGcRgRA.png" /><figcaption>▲下完指令後產生的apidoc</figcaption></figure><p>Step 5: 點開index.html看畫面呈現</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/971/1*FUNKHTjyR-yAJtHVjw2aMw.png" /></figure><p>以上就是簡單的apiDoc介紹!!</p><p>template部分基本上不太會用到，內建apiDoc產出的頁面基本上不會太難看，再加上主要的價值還是api文件內容及參數的型別、格式定義。更進階的用法也可以參考官網文件喔!!</p><h3>參考資料</h3><h4>ApiDoc官網: <a href="http://apidocjs.com/">http://apidocjs.com/</a></h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8fda7bca944e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/lion-f2e/%E6%92%B0%E5%AF%ABapi%E7%9A%84%E5%88%A9%E5%99%A8-apidoc-8fda7bca944e">撰寫API的利器-apidoc</a> was originally published in <a href="https://medium.com/lion-f2e">LT Lab</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[分享] TypeScript’s Basic]]></title>
            <link>https://medium.com/lion-f2e/%E5%88%86%E4%BA%AB-typescripts-basic-46f4a4f69e9b?source=rss----d76981da6b21---4</link>
            <guid isPermaLink="false">https://medium.com/p/46f4a4f69e9b</guid>
            <category><![CDATA[js]]></category>
            <category><![CDATA[web-design]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[concept]]></category>
            <dc:creator><![CDATA[chengyucai]]></dc:creator>
            <pubDate>Sun, 18 Aug 2019 03:54:28 GMT</pubDate>
            <atom:updated>2019-10-18T03:00:09.022Z</atom:updated>
            <content:encoded><![CDATA[<h3>[分享] TypeScript&#39;s Basic &lt;I&gt;</h3><p><em>本篇分享關於TypeScript 基礎邏輯</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kXv_4vznnyItbvt3vWpT-A.jpeg" /></figure><p>以下是TypeScript中所使用的類型</p><blockquote><strong>Boolean</strong></blockquote><p><strong>Boolean</strong>是最基本也是的數據類型，就是只有( true / false )的值</p><pre><strong>const</strong> value：<strong>Boolean</strong>     // only =&gt; true | fales</pre><blockquote><strong>number</strong></blockquote><p>TypeScript中的所有數字都是浮點值，可支援的進制有以下幾種:</p><ul><li>十進制(dec)</li><li>十六進制(hex)</li><li>八進制(oct)</li><li>二進制(bin)</li></ul><pre><strong>let</strong> decimal: number = 6       // 十進制<br><strong>let</strong> hex: number = 0xf00d      // 十六進制<br><strong>let</strong> octal: number = 0o744     // 八進制<br><strong>let</strong> binary: number = 0b1010   // 二進制</pre><blockquote>string</blockquote><p>是基本的文本數據， TypeScript也使用雙引號（&quot;）或單引號（&#39;）來包圍字符串數據。</p><pre><strong>const </strong>name：s​​tring =`Lion travel` <br><strong>const</strong> exp：number = 7 <br><strong>const</strong> text：string =` I have ${num} year coding experience `.</pre><p>可以使用反引號(` backtick)， 嵌入表達式 ${ }</p><blockquote><strong>Array</strong></blockquote><p><strong>首先 Array 的 Interface 基本定義方法:</strong></p><pre><strong>const </strong>value: Array&lt;<strong>string</strong>&gt;     // rule: Array&lt;type&gt;</pre><p>可先定義型態為Array並在 &lt;&gt; 內定義此陣列的結構內部為何種型態</p><p><strong>或者是用另一種方式:</strong></p><pre><strong>const </strong>value: <strong>string</strong>[]    // rule: type[]</pre><p>直接定義此變數由string所組成的陣列</p><p><strong>如果需求有同時存在兩種以上的型態存在時，可利用以下方式:</strong></p><pre><strong>const </strong>value: (<strong>number </strong>| <strong>string</strong>)[]    // rule: (type | type)[]</pre><p>使用特殊符號 | 的方式，將兩種不同型態做共存的形式</p><blockquote><strong>Object</strong></blockquote><p><strong>首先 Object 的 Interface 基本定義方法:</strong></p><pre><strong>const </strong>value: Object      // Object: {}</pre><p>Object的型態不像Array需強制定出內部結構，所以只要這樣做就可以了。基本上把Object type 當作是 json 的資料處理方式比較容易理解(需注意幾的重點)</p><pre>JSON values <strong>cannot </strong>be one of the following data types:<br>. a function <br>. a date<br>. <em>undefined</em></pre><p><strong>如果是要定義 Object 的 Value 型態 ，可利用以下方式:</strong></p><pre><strong>Object</strong>: { [<strong>key</strong>: string]: number }       //key only (string | number)</pre><p>Key 即使定義也是無法改變 Object 的 Key 自身的屬性，所以無論是定義成字串或是數字結果都不會改變</p><blockquote><strong>Function</strong></blockquote><p>函數是JavaScript中任何應用程序的基本構建區塊。它可以作為一個模組使用。在TypeScript中，函數具有類別( classes)、命名( namespaces)和模組( modules)，函數在程序上有著關鍵的作用。TypeScript還為標準JavaScript函數添加了一些新功能，使其更易於使用</p><pre><strong>function</strong> <strong>add</strong>(x: number, y: number): <strong>number</strong> { <br>    <strong>return</strong> x + y; <br>}</pre><p>函數的型別(Type)具有兩個部分：參數的類型和返回類型，</p><p>這兩個部分的參數類型是必須都寫出來，且參數的部分必須每個都賦予名稱和類型。(名稱是為了提高可讀性)</p><pre><strong>function mergeString</strong>(firstName: string, lastName: string) {<br>     <strong>return</strong> firstName + &quot; &quot; + lastName; <br>}  <br><strong>const </strong>rs1 = <strong>mergeString</strong>(&quot;Hello&quot;);                 // error<br><strong>const </strong>rs2 = <strong>mergeString</strong>(&quot;Hello&quot;, &quot;world&quot;, &quot;T_T&quot;); // error <br><strong>const </strong>rs3 = <strong>mergeString</strong>(&quot;Hello&quot;, &quot;world&quot;);        // Hello world</pre><p>在TypeScript中，函數在未設定默認參數的狀況下每個參數都需要，因為不給參數的狀況下，會被默認成 null<strong> </strong>所以類型不合</p><pre><strong>function mergeString</strong>(firstName?: string, lastName = &quot;React&quot;) {<br>     <strong>return</strong> firstName + &quot; &quot; + lastName; <br>}  <br><strong>const </strong>rs1 = <strong>mergeString</strong>(&quot;Hello&quot;);           // Hello React<br><strong>const </strong>rs2 = <strong>mergeString</strong>(&quot;Hello&quot;, &quot;world&quot; ); // Hello world<br><strong>const </strong>rs3 = <strong>mergeString</strong>( );                 // &quot; React&quot;</pre><p>函數在設定默認參數或是可省略的狀況下</p><blockquote><strong>enum</strong></blockquote><p>是一種標準資料狀態型別，資料的內容在未設定的狀況下，是依據上一個的狀態做決定，舉例如下:</p><pre><strong>enum</strong> Color {Red = 1, Green, Blue}</pre><pre><strong>const </strong>colorName: string = Color[2];   // Displays &#39;Green&#39;</pre><p>如果把<strong>enum</strong>轉換成Object大致如下:</p><pre><strong>enum</strong> Color {Red = 1, Green = 4, Blue}</pre><pre>Color = { <br>   &#39;<strong>red</strong>&#39; = 1，<br>   &#39;<strong>Green</strong>&#39; = 4，<br>   &#39;<strong>Blue</strong>&#39;= 5，<br>   &#39;<strong>1</strong>&#39; = &#39;Red&#39;，<br>   &#39;<strong>4</strong>&#39; = &#39;Green&#39;，<br>   &#39;<strong>5</strong>&#39; = &#39;Blue&#39;<br>}</pre><blockquote><strong>any</strong></blockquote><p>在編寫程序時我們不知道的變量類型。這些值可以用any來表達內容類型</p><blockquote><strong>void</strong></blockquote><p>void有點像是相反的any 是沒有任何類型</p><blockquote><strong>結語</strong></blockquote><p>類型在未定義時基本就是自動以any來表達內容類型，但是盡量別用，為了 使代碼更具可讀性和可測試性，即使是資料型態再複雜也因該避免使用any</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=46f4a4f69e9b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/lion-f2e/%E5%88%86%E4%BA%AB-typescripts-basic-46f4a4f69e9b">[分享] TypeScript’s Basic</a> was originally published in <a href="https://medium.com/lion-f2e">LT Lab</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Docker 安裝 Jupyter 跑 JS 與 Typescript]]></title>
            <link>https://medium.com/lion-f2e/docker-%E5%AE%89%E8%A3%9D-jupyter-%E8%B7%91-js-%E8%88%87-typescript-522d3624e430?source=rss----d76981da6b21---4</link>
            <guid isPermaLink="false">https://medium.com/p/522d3624e430</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[jupyter-notebook]]></category>
            <dc:creator><![CDATA[DEBUG]]></dc:creator>
            <pubDate>Fri, 16 Aug 2019 00:31:01 GMT</pubDate>
            <atom:updated>2019-08-16T00:31:01.438Z</atom:updated>
            <content:encoded><![CDATA[<p>Docker 是什麼？</p><blockquote>Docker利用<a href="https://zh.wikipedia.org/wiki/Linux%E6%A0%B8%E5%BF%83">Linux核心</a>中的資源分離機制，例如<a href="https://zh.wikipedia.org/wiki/Cgroups">cgroups</a>，以及Linux核心<a href="https://zh.wikipedia.org/w/index.php?title=Linux%E5%91%BD%E5%90%8D%E7%A9%BA%E9%96%93&amp;action=edit&amp;redlink=1">命名空間</a>（namespaces），來建立獨立的<a href="https://zh.wikipedia.org/wiki/%E4%BD%9C%E6%A5%AD%E7%B3%BB%E7%B5%B1%E5%B1%A4%E8%99%9B%E6%93%AC%E5%8C%96">容器</a>（containers）。這可以在單一Linux實體下運作，避免啟動一個<a href="https://zh.wikipedia.org/wiki/%E8%99%9B%E6%93%AC%E6%A9%9F%E5%99%A8">虛擬機器</a>造成的額外負擔<a href="https://zh.wikipedia.org/wiki/Docker#cite_note-2">[2]</a>。<br>來源: <a href="https://zh.wikipedia.org/wiki/Docker">維基百科</a></blockquote><p>開發者常碰到，在本地開發是用 Windows 系統，上線後的環境是 Linux 系統，系統的不同造成程式錯誤的機率就越大。早期的普遍做法就是透過 virtualbox 或 vmware 等，來安裝整個作業系統，安裝系統及設定系統，需要相當的時間，且也佔用相當多的本地資源，Docker 是使用本地系統的核心，以最小資源運行其環境。</p><blockquote>淺談輕量化的虛擬技術 — Docker容器<a href="http://www.cc.ntu.edu.tw/chinese/epaper/0036/20160321_3611.html">http://www.cc.ntu.edu.tw/chinese/epaper/0036/20160321_3611.html</a></blockquote><p>Windows 也支援 Linux 的開發環境，可以搜尋關鍵字 Windows Subsystem for Linux。在 Windows 下安裝 Docker Desktop for Windows 需滿足以下條件。</p><blockquote>Windows 10 64bit: Pro, Enterprise or Education (Build 15063 or later). Virtualization is enabled in BIOS. Typically, virtualization is enabled by default. This is different from having Hyper-V enabled. For more detail see Virtualization must be enabled in Troubleshooting. <br>CPU SLAT-capable feature. <br>At least 4GB of RAM.<br>來源: <a href="https://docs.docker.com/docker-for-windows/install/">https://docs.docker.com/docker-for-windows/install/</a></blockquote><p>會開始接觸 Docker 的場景，當 MAC 系統更新，本地設定的開發環境都會還原成預設值，一開時的做法認為環境跑掉就重設，沒什麼難度，但每次的重設都是不同的操作流程才能完成，最後真的心累的，才漸漸使用 Docker 來部署環境。Docker 環境建置好了，不管新人舊人來要協同開發，也相對省時，省去建置環境的時間。</p><p>這次實作，使用的 image 為 jupyter/datascience-notebook，預設安裝支援的語言有Julia 、R、Python，我們需要再加入Javascript 及 Typescript。</p><blockquote>找尋是否有現成 image 可以上 docker hub 搜尋<br><a href="https://hub.docker.com/search?q=jupyter&amp;type=image">https://hub.docker.com/search?q=jupyter&amp;type=image</a></blockquote><p>這邊啟用 Container 的方式，是透過 Dockerfile 及 docker-compose 的方式。</p><p>下面是 Dockerfile 的代碼，針對 image jupyter/datascience-notebook 做一些調整，以下代碼均來自 <a href="https://hub.docker.com/r/jupyter/datascience-notebook/dockerfile">https://hub.docker.com/r/jupyter/datascience-notebook/dockerfile</a>。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f479b6c596182342699aade641eb5442/href">https://medium.com/media/f479b6c596182342699aade641eb5442/href</a></iframe><p>針對上述，我們加入的部份如下，我們增加nodejs、 ijavascript、itypescript</p><pre>RUN conda install -c conda-forge nodejs</pre><pre>RUN npm install -g ijavascript itypescript</pre><pre>RUN ijsinstall</pre><pre>RUN its — install=local</pre><blockquote>ijavascript<br><a href="https://github.com/n-riesco/ijavascript">https://github.com/n-riesco/ijavascript</a><br>itypescript<br><a href="https://github.com/nearbydelta/itypescript">https://github.com/nearbydelta/itypescript</a></blockquote><p>docker-compose 的目的在於，針對不同的 image 做管理，由於此範例只有一個 image ，無法看出 docker-compose 的方便性。這邊有定義 Port，本地的Port 8888 對應到容器的 8888，啟動服務後，可以使用 <a href="http://localhost/">http://localhost:</a>8888，查看該服務。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e417a7bdf73b81cccc4c366bac7ec729/href">https://medium.com/media/e417a7bdf73b81cccc4c366bac7ec729/href</a></iframe><p>上述設定完後，以下指令，安裝及啟動服務，耗時差不多 20 分鐘，image 檔案約 5G。</p><pre># docker-compose up</pre><p>完成後，終端機下會顯示一段如下 URL，token 的用意，是在認證您的身份。</p><p><a href="http://0.0.0.0:8888/?token=e3d5f492feacd43e1100644ff6f1e6fa5d646317308c2240">http://0.0.0.0:8888/?token=[</a>token]</p><p>順利的話，會顯示如下畫面，且會有Javascript 及 Typescript 的選項。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FCrn4RCcARRKoGfnV2CRBw.png" /></figure><p>通常都不順利，我們可以查看目前運多的服務，直接進入 Container 下啟用。</p><p>以下指令查看當前運作的 Container</p><pre># docker ps</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ixgi88_s81aNZj3OEDdrog.png" /></figure><p>將看到的CONTAINER ID 帶入以下的[CONTAINER ID]</p><pre># docker exec -it -u root [CONTAINER ID] /bin/bash</pre><p>進入後，執行以下指令，讓Javascript 及 Typescript 可以運作。重整剛剛網頁的畫面，應該就會顯示這兩個選項。</p><pre># ijsinstall</pre><pre># its --install=local</pre><p>來 TRY 一下 Typescript。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V3j5I_gyjTzIfiFcOMvetw.png" /></figure><p>刪除 Container</p><pre># docker-compose down</pre><p>啟動存在的 Container</p><pre># docker-compose start</pre><p>停止 Container</p><pre># docker-compose stop</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=522d3624e430" width="1" height="1" alt=""><hr><p><a href="https://medium.com/lion-f2e/docker-%E5%AE%89%E8%A3%9D-jupyter-%E8%B7%91-js-%E8%88%87-typescript-522d3624e430">Docker 安裝 Jupyter 跑 JS 與 Typescript</a> was originally published in <a href="https://medium.com/lion-f2e">LT Lab</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>