GraphQLとは

APIのクエリ言語。 用途はBackend for Frontend。

良いところ

  • スキーマ(Schema Definition Language, SDL)による組織間コミュニケーション
  • オーバーフェッチが少ない
  • スキーマ駆動開発
  • 単一エンドポイント(複数リクエストを送る必要がなくなったりする)

環境構築(未検証)

npm install

npx prisma migrate dev
prisma generate

開発

GraphQLサーバ立ち上げ

node src/index.js   

migration

npx prisma migrate dev
prisma generate

データベースのクライアント

npx prisma studio

サンプルクエリ

query {
  users{
    id
    username
    description
    comments {
      id
      content
      createdAt
    }
  }
}

mutation {
  addUser(username: "kumaki", description: "エンジニアです") {
    id,
    description,
    createdAt,
    modifiedAt
  }
}

mutation {
  addUser(username: "詳細不明さん") {
    id,
    description,
    createdAt,
    modifiedAt
  }
}

mutation {
  changeName(id:2, username: "updated username") {
    id
    username
    createdAt
    modifiedAt
  }
}

mutation {
  deleteUser(id: 1) {
    id,
    username
  }
}

mutation {
  comment(userId: 2, content:"こんにちは") {
    id,
    content,
    createdAt,
  }
}

curl

curl -X POST -H 'content-type: application/json' -d '{"query":"{users { id, username }}"}' http://localhost:4000/