Skip to content

Instantly share code, notes, and snippets.

@jmquilario
Created August 22, 2023 16:42
Show Gist options
  • Save jmquilario/699e821c29292a8af14e43b9a6930f18 to your computer and use it in GitHub Desktop.
Save jmquilario/699e821c29292a8af14e43b9a6930f18 to your computer and use it in GitHub Desktop.
GraphQL Cheat Sheet
# GraphQL Cheat Sheet (with Pagination)
# Defining types
type User {
id: ID!
username: String!
posts: [Post!]!
}
type Post {
id: ID!
title: String!
content: String!
author: User!
}
# Querying data with pagination
query {
getUser(id: ID!): User
getAllPosts(first: Int, after: String): PostConnection!
}
# Pagination-related types
type PageInfo {
hasNextPage: Boolean!
endCursor: String
}
type PostEdge {
cursor: String!
node: Post!
}
type PostConnection {
edges: [PostEdge!]!
pageInfo: PageInfo!
}
# Requesting specific fields
query {
getAllPosts(first: 10) {
edges {
node {
title
}
}
}
}
# Fetching more pages
query {
getAllPosts(first: 10) {
edges {
node {
title
}
cursor
}
pageInfo {
hasNextPage
endCursor
}
}
}
# Fetching next page with cursor
query {
getAllPosts(first: 10, after: "cursorValue") {
edges {
node {
title
}
cursor
}
pageInfo {
hasNextPage
endCursor
}
}
}
# Mutation to create a post
mutation {
createPost(title: String!, content: String!, authorId: ID!): Post
}
# Mutation to update a post
mutation {
updatePost(id: ID!, title: String, content: String): Post
}
# Mutation to delete a post
mutation {
deletePost(id: ID!): Boolean
}
# Subscription for new posts (if supported)
subscription {
newPost: Post
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment