/graphql-practice

graphql練習

Primary LanguageJavaScript

GraphQL練習

  1. nodejsjson server當做後端restful api來試包練習
  2. nodejsapollo server練習GraphQL

參考資料

  1. graphQL apollo server
  2. restful json server
  3. httpClient axios

安裝nodejs

brew install node

安裝json-server

npm安裝

npm install -g json-server

初始化專案

在本專案目錄執行

npm init --yes

安裝dependencies

在本專案目錄執行

npm install --save apollo-server graphql axios

啟動json-server

在本專案目錄執行

json-server db.json

啟動後url

http://localhost:3000

啟動apollo server

在本專案目錄執行

node index.js

啟動後url

http://localhost:4000

查詢data request

query

{
  posts{
    id
    title
    author
  }
  comments{
    id
    body
    postId
  }
  profile{
    name
  }
}

更新data request

mutation

mutation UpdateData($post:PostIn,$comment:CommentIn,$profileName:String){
  addPost(data:$post){
    id
    title
    author
  }
  addComment(data:$comment){
    id
    body
    postId
  }
  editProfile(name:$profileName){
   	name 
  } 
}
{
  "post": {"title": "test","author": "leo"},
  "comment": {"body": "this is test comment."},
  "profileName": "TestGraphQL"
}