Query
Make a query:
{
Photo {
name
url
}
}
Send to GraphQL endpoint with cURL:
curl 'http://someurl.com/'
-H 'Content-Type: application/json'
--data '{"query":"{ Photo {name url}}"}'
Mutation
Modify data:
mutation {
setLiftStatus(id: "panorama" status: OPEN) {
name
status
}
}
Send to GraphQL endpoint with cURL:
curl 'http://someurl.com/'
-H 'Content-Type: application/json'
--data '{"query":"mutation {setLiftStatus(id: \"panorama\" status: OPEN) {name status}}"}'
Define Types
A type represents a custom object and these objects describe your application’s core features.
A scalar type is not an object type. It does not have fields.
An enum is a scalar type that allow a field to return a restrictive set of string values.
scalar DateTime
type Photo {
id: ID!
name: String!
url: String!
description: String
created: DateTime!
category: PhotoCategory!
}
enum PhotoCategory {
SELFIE
PORTRAIT
ACTION
LANDSCAPE
GRAPHIC
}
Connections and Lists
list declaration | definition |
---|---|
[Int] | A list of nullable integer values |
[Int!] | A list of non-nullable integer values |
[Int]! | A non-nullable list of nullable integer values |
[Int!]! | A non-nullable list of non-nullable integer values |
One to many connection example:
type User {
githubLogin: ID!
name: String
avatar: String
postedPhotos: [Photo!]!
}
Many to many connection example:
type User {
...
inPhotos: [Photo!]!
}
type Photo {
...
taggedUsers: [User!]!
}
Union type example:
union AgendaItem = StudyGroup | Workout
type StudyGroup {
name: String!
subject: String
students: [User!]!
}
type Workout {
name: String!
reps: Int!
}
type Query {
agenda: [AgendaItem!]!
}
Arguments
type Query {
Photo(id: ID!): Photo! # pass ID to select photo
allPhotos(category: PhotoCategory): [Photo!]! # pass category to filter photos
}
Paging
type Query {
allPhotos(first: Int=25 where: Int=0): [Photo!]! # cursor-based
allPhotos(first: Int=25 after: Int=0): [Photo!]! # Relay-style
allPhotos(limit: Int=25 offset: Int=0): [Photo!]! # offset-based
}
Sorting
enum SortablePhotoField {
name
description
category
created
}
Query {
allPhotos(
sortBy: SortablePhotoField = created
): [Photo!]!
}
query {
allPhotos(sortBy: name)
}
Variable
Variables replace the static value in the query so that we can pass dynamic values
mutation createSong($title:String! $numberOne:Int $by:String!) {
addSong(title:$title, numberOne:$numberOne, performerName:$by) {
id
title
numberOne
}
}
# use variable name as the JSON keys
{
"title": "No Scrubs",
"numberOne": true,
"by": "TLC"
}
Alias
Change the field names in the response object within the query.
query liftsAndTrails {
open: liftCount(status: OPEN)
chairlifts: allLifts {
liftName: name
status
}
skiSlopes: allTrails {
name
difficulty
}
}
Fragment
Fragments are selection sets that can be reused in multiple operations.
fragment liftInfo on Lift {
name
status
capacity
night
elevationGain
}
query {
Lift(id: "jazz-cat") {
...liftInfo
trailAccess {
name
difficulty
}
}
Trail(id: "river-run") {
name
difficulty
accessedByLifts {
...liftInfo
}
}
}
Reference
Eve Porcello and Alex Banks. 2018. Learning GraphQL: Declarative Data Fetching for Modern Web Apps (1st. ed.). O’Reilly Media, Inc.