Skip to content

Instantly share code, notes, and snippets.

@chuck0523
Last active June 22, 2016 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuck0523/adb1b72869636eacbfd7dbf864028405 to your computer and use it in GitHub Desktop.
Save chuck0523/adb1b72869636eacbfd7dbf864028405 to your computer and use it in GitHub Desktop.
//// GraphQLクエリ
// 引数可
{
recentPosts(count: 5) {
title
}
}
// ネストでも引数
{
recentPosts(count: 2) {
title,
comments(limit: 1) {
content
}
}
}
// ルートクエリは複数可
{
latestPost {
title
},
authors {
name
}
}
// 変数
{
latestPost {
title
},
authorNames: authors {
name
}
authorIds: authors {
_id
}
}
//// ミューテーション
// 新しくauthorを生成して、author {_id, name}を返す
mutation {
createAuthor(
_id: "john",
name: "John Carter",
twitterHandle: "@john"
) {
_id
name
}
}
// required argumentsなしでは、ミューテーションは使用不可
// name: String! <- nameはString型でrequiredであることを意味している。
// 複数ミューテーション
mutation {
sam: createAuthor(
_id: "sam",
name: "Sam Hautom"
twitterHandle: "@sam"
) {
_id
name
},
chris: createAuthor(
_id: "chris",
name: "Chris Mather",
twitterHandle: "@chris"
) {
_id
name
}
}
// ミューテーションはシークエンスとして実行される。
// ミューテーションはGraphQLのデータセットを変更する。
//// フラグメント
// フラグメント:Fieldの共通化と再利用に用いる
// フラグメントを使わない冗長な例。
{
arunoda: author(_id: "arunoda") {
_id
name
twitterHandle
},
pahan: author(_id: "pahan") {
_id,
name
twitterHandle
},
indi: author(_id: "indi") {
_id
name
twitterHandle
}
}
// フラグメントを用いた例。
{
arunoda: author(_id: "arunoda") {
...authorInfo
},
pahan: author(_id: "pahan") {
...authorInfo
},
indi: author(_id: ""indi) {
...authorInfo
}
}
fragment authorInfo on Author {
_id,
name,
twitterHandle
}
// フィールドとフラグメントを混ぜることも可能
{
indi: author(_id: "indi") {
...requiredAuthorInfo
twitterHandle
}
}
fragment requiredAuthorInfo on Author {
_id,
name
}
// フラグメントはネストも多重利用も可能
{
post1: post(_id: "03390abb5570ce03ae524397d215713b") {
...postInfo
},
post2: post(_id: "0176413761b289e6d64c2c14a758c1c7") {
...postInfo
}
}
fragment postInfo on Post {
title,
content,
author {
...authorInfo
},
comments {
content,
author {
...authorInfo
}
}
}
fragment authorInfo on Author {
_id,
name
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment