💾 Archived View for gem.sdf.org › s.kaplan › cheatsheets › programming-languages › graphql.md captured on 2024-08-31 at 12:51:04.
⬅️ Previous capture (2023-09-28)
-=-=-=-=-=-=-
# GraphQL Cheatsheet ## Overview of unique features - Query language for APIs - Allows clients to specify exactly what data they need - Supports introspection and self-documentation - Can be used for building efficient and flexible APIs ## Querying
{
user(id: "123") {
name
}
}
{
john: user(id: "123") {
name
}
jane: user(id: "456") {
name
}
}
{
users(role: "admin") {
name
}
}
{
user(id: "123") {
name
posts {
title
content
}
}
}
## Mutations
mutation {
createUser(input: {name: "John", email: "john@example.com"}) {
id
name
}
}
mutation {
updateUser(id: "123", input: {name: "John Doe"}) {
id
name
}
}
mutation {
deleteUser(id: "123") {
id
name
}
}
## Fragments
fragment UserInfo on User {
name
}
{
user(id: "123") {
...UserInfo
}
}
# Define a fragment with variables fragment UserInfo on User { name email posts(first: $count) { title content } } # Use a fragment with variables query($count: Int!) { user(id: "123") { ...UserInfo } }
# Use a directive to conditionally include a field { user(id: "123") { name email posts @include(if: $withPosts) { title content } } }
# Define a schema type User { id: ID! name: String! email: String! posts: [Post!]! } type Post { id: ID! title: String! content: String! author: User! } type Query { user(id: ID!): User posts: [Post!]! } type Mutation { createUser(input: CreateUserInput!): User! updateUser(id: ID!, input: UpdateUserInput!): User! deleteUser(id: ID!): User! } input CreateUserInput { name: String! email: String! } input UpdateUserInput { name: String email: String }
- [GraphQL documentation](https://graphql.org/learn/)
- [GraphQL tutorial](https://www.tutorialspoint.com/graphql/index.htm)
- [GraphQL forum](https://stackoverflow.com/questions/tagged/graphql) for community support and troubleshooting.