LoginSignup
17
13

More than 5 years have passed since last update.

GraphCMSのQuery、Mutation逆引き [GraphQL]

Last updated at Posted at 2019-01-12

概要

GraphQLのQeury、Mutationの構文をちょくちょく忘れるので備忘録としてまとめました。
GraphCMSで以下のようなSchemeを設定したと想定した際のQuery、Mutationの書き方です。
随時追加していきます。

基準となるScheme

type Post implements Node {
  status: Status!
  id: ID!
  createdAt: DateTime!
  updatedAt: DateTime!
  title: String
  contents: String
  thumbnail: Asset
}

Query

全ての投稿を取得する

query allPosts {
  posts(where: {status: PUBLISHED}) {
    id
    createdAt
    title
    contents
    thumbnail {
      url
    }
  }
}

最新のN件のデータを取得する

query fetchPostById($first: Int = 3) {
  posts(orderBy: createdAt_DESC, first: $first) {
    id
    createdAt
    title
    contents
    thumbnail {
      url
    }
  }
}

指定のIDの投稿を取得する

query fetchPostById($id: ID = "cjq8y7cn6g0nv0a84vxi2j2ty") {
  post(where: {id: $id}) {
    id
    createdAt
    title
    contents
    thumbnail {
      url
    }
  }
}

投稿件数を取得する

query maxPostCount {
  postsConnection {
    aggregate {
      count
    }
  }
}

N件抜かした所からM件取得する(ページング用)

query fetchPostByPage($skip: Int = 2, $first: Int = 2 ) {
  posts(first: $first, skip: $skip, orderBy: createdAt_DESC) {
    id
    title
    createdAt
    thumbnail {
      url
    }
  }
}

ある投稿以降の投稿N件を取得する(ページング用)

query fetchAfterPostById($id: String = "cjq8y7cn6g0nv0a84vxi2j2ty", $skip: Int = 1) {
  posts(after: $id, skip: $skip) {
    id
    createdAt
    title
    contents
    thumbnail {
      url
    }
  }
}

ある投稿以前の投稿N件を取得する(ページング用)

query fetchBeforePostById($id: String = "cjq8y7cn6g0nv0a84vxi2j2ty", $skip: Int = 1) {
  posts(before: $id, skip: $skip) {
    id
    createdAt
    title
    contents
    thumbnail {
      url
    }
  }
}

最後からN件取得する

query fetchPostFromLast($last: Int = 2) {
  posts(last: $last) {
    id
    createdAt
    title
    contents
    thumbnail {
      url
    }
  }
}

最初からN件取得する

query fetchPostFromFirst($first: Int = 2) {
  posts(first: $first) {
    id
    createdAt
    title
    contents
    thumbnail {
      url
    }
  }
}

複数条件で指定して取得する(AND検索)

query fetchPostFilterAnd($title: String = "はじめての投稿", $datetime: DateTime = "2018-12-29T04:09:19.625Z") {
  posts(where: {AND: [{title: $title}, {createdAt_gt: $datetime}]}) {
    id
    createdAt
    title
    contents
    thumbnail {
      url
    }
  }
}

復数条件で指定して取得する(OR検索)

query fetchPostFilterAnd($title: String = "はじめての投稿", $datetime: DateTime = "2018-12-29T04:09:19.625Z") {
  posts(where: {OR: [{title: $title}, {createdAt_gt: $datetime}]}) {
    id
    createdAt
    title
    contents
    thumbnail {
      url
    }
  }
}

Mutation

投稿の追加 (Insert)

mutation createPost($status: Status = PUBLISHED, $title: String = "hoge", $contents: String = "fugafuga") {
  createPost(data: {status: $status, title: $title, contents: $contents}) {
    id
  }
}

投稿内容の更新(Update)

mutation updatePost($status: Status = DRAFT, $id: ID = "cjqtmexas2xpq0a84tdbf2xub") {
  updatePost(data: {status: $status}, where: {id: $id}) {
    id
    status
  }
}

投稿の削除(Delete)

mutation deletePost($id: ID = "cjqtmexas2xpq0a84tdbf2xub") {
  deletePost(where: {id: $id}) {
    id
  }
}
17
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
13