{
}empty query
{
posts {
title
}
}posts query (titles)
{
posts {
title,
_id,
summary
}
}posts query (title, id, summary)
{
posts {
title,
_id,
summary
},
authors {
name
}
}posts query and authors query
{
posts {
_id,
title,
timestamp,
summary
}
posts {
_id,
title,
content,
category
}
}
different results for same schema
{
postsShort: posts {
_id,
title,
timestamp,
summary
}
postsFull: posts {
_id,
title,
content,
category
}
}
{
author(_id: "indi"){
_id,
name
}
recentPosts(count: 5) {
title,
summary,
timestamp
}
}
query with params
named query results
{
currentAuthor: author(_id: "indi"){
_id,
name
}
currentPosts: recentPosts(count: 5) {
title,
summary,
timestamp
}
allPosts: posts {
title,
content,
summary
}
}
Single entries
{
indi: author(_id: "indi"){
_id,
name,
twitterHandle
}
arunoda: author(_id: "arunoda"){
_id,
name,
twitterHandle
}
pahan: author(_id: "pahan"){
_id,
name,
twitterHandle
}
}Nested queries
{
posts {
title,
_id,
author {
name
}
}
}{
posts {
title,
_id,
comments {
_id,
content
}
}
}{
posts {
title,
_id,
comments {
_id,
content
},
author {
_id,
name
}
}
}{
posts {
title,
_id,
comments {
_id,
content,
author {
_id,
name
}
}
}
}{
posts {
title,
_id,
comments {
_id,
content,
author {
_id,
name
}
},
author {
_id,
name
}
}
}Repetitive queries - use fragments
{
indi: author(_id: "indi"){
_id,
name,
twitterHandle
}
arunoda: author(_id: "arunoda"){
_id,
name,
twitterHandle
}
pahan: author(_id: "pahan"){
_id,
name,
twitterHandle
}
}{
indi: author(_id: "indi"){
...authorData
}
arunoda: author(_id: "arunoda"){
...authorData
}
pahan: author(_id: "pahan"){
...authorData
}
}
fragment authorData on Author {
_id,
name,
twitterHandle
}Repetitive queries - use fragments
{
posts {
title,
_id,
comments {
_id,
content,
author {
_id,
name
}
},
author {
_id,
name
}
}
}{
posts {
title,
_id,
comments {
_id,
content,
author {
...authorName
}
},
author {
...authorName
}
}
}
fragment authorName on Author {
_id,
name
}Repetitive queries - use fragments and interfaces
{
posts {
title,
_id,
comments {
_id,
content,
author {
_id,
name
}
},
author {
_id,
name
}
}
}{
posts {
title,
_id,
comments {
_id,
content,
...authorOf
},
...authorOf
}
}
fragment authorOf on HasAuthor {
author {
_id,
name,
}
}{
posts {
title,
_id,
comments {
_id,
content,
author {
...authorName
}
},
author {
...authorName
}
}
}
fragment authorName on Author {
_id,
name
}Change data - Mutations
mutation {
createAuthor(
_id: "john",
name: "John Carter",
twitterHandle: "@john"
) {
_id
name
}
}mutation {
sam: createAuthor(
_id: "sam",
name: "Sam Hautom"
twitterHandle: "@sam"
) {
_id
name
},
chris: createAuthor(
_id: "chris",
name: "Chris Mather"
twitterHandle: "@chris"
) {
_id
name
}
}Change data - Mutations
mutation {
carter: createAuthor(
_id: "carter",
name: "Carter Boom"
twitterHandle: "@carter"
) {
_id
},
carter2: createAuthor(
_id: "carter",
name: "Carter Boom"
twitterHandle: "@carter"
) {
_id
}
}Query Variables
{
recentPosts(count: 10) {
title
}
}for hard-coded values
query getFewPosts {
recentPosts(count: 10) {
title
}
}full query syntax (with query name)
query getFewPosts($postCount: Int!) {
recentPosts(count: $postCount) {
title
}
}query with declared variable
Query Variables
multiple variables
query getFewPosts($postCount: Int!, $commentCount: Int) {
recentPosts(count: $postCount) {
title,
comments(limit: $commentCount) {
content
}
}
}query getFewPosts($postCount: Int!, $commentCount: Int) {
recentPosts(count: $postCount) {
title,
...comments
}
}
fragment comments on Post {
comments(limit: $commentCount) {
content
}
}variables in fragments
You can send GraphQL queries through HTTP: