Skip to content

Instantly share code, notes, and snippets.

@krlozadan
Last active September 2, 2019 19:44
Show Gist options
  • Save krlozadan/1c8a0dfe55ae22b4367c0757cc345065 to your computer and use it in GitHub Desktop.
Save krlozadan/1c8a0dfe55ae22b4367c0757cc345065 to your computer and use it in GitHub Desktop.
# Queries
# Use fragments when we want to reuse fields for the same object
fragment sharedOrganizationFields on Organization {
name
url
description
websiteUrl
repositories(first:2) {
edges{
node{
# We can include or exclude fields with directives like @include and @skip
forkCount @include(if: $withFork)
diskUsage @skip(if: $noDU)
name
}
}
}
}
# Add query parameters and generate dynamic queries
# Function params can be required (Type!) or default (Type=value)
# You can name queries to make them more declarative
query OrganizationFetch($organization:String = "epic-games", $withFork:Boolean!, $noDU:Boolean!) {
viewer {
name
}
# Use aliases when fetching specific objects from the same type
epic: organization(login:$organization) {
...sharedOrganizationFields
}
ricoh: organization(login:"ric-sv") {
...sharedOrganizationFields
}
}
# Mutations
mutation SwitchStar($repositoryId:ID!, $newStatus:String!) {
# Mutations act like functions and the body is the return query
addStar(input:{ starrableId: $repositoryId }){
starrable{
id
viewerHasStarred
}
}
# Mutations will be ran in series 1 after another
removeStar(input:{ starrableId: $repositoryId }){
starrable{
id
viewerHasStarred
}
}
changeUserStatus(input: { emoji: ":computer:", message: $newStatus }) {
newStatus: status{
emoji
message
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment