Designing Scalable Schemas
Eve Porcello
@eveporcello
Tahoe City, CA
GraphQL is a query language for your API.
{ }
{ }
Query
Response
GraphQL is a spec that describes:
A Query Language
A Schema Definition Language
Building the
Pet Library Schema
Keep In Mind:
-
What Clients Need
-
Specific, Consistent Naming
-
Custom Objects Over Scalar Values
Union Types
type Schedule {
agenda: [AgendaItem!]!
}
union AgendaItem = StudyGroup | Workout
query {
agenda {
...on Workout {
name
reps
}
...on StudyGroup {
name
subject
students
}
}
}
Query
Interfaces
interface ScheduleItem {
name: String!
start: Int
end: Int
}
type StudyGroup implements ScheduleItem {
name: String!
start: Int
end: Int
students: Int!
}
type Workout implements ScheduleItem {
name: Location!
start: Int
end: Int
reps: Int!
}
type Query {
agenda: [ScheduleItem!]!
}
enum PetCategory
DOG
CAT
STINGRAY
RABBIT
type Pet
id: ID!
name: String!
category: PetCategory!
...
HORSE
sleepAmount: Int
curious: Boolean
favoriteFood: String
floppy: Int
good: Boolean
chill: Boolean
fast: Boolean
streetsmart: Boolean
majestic: Boolean
interface Pet
type Cat implements Pet
id: ID!
name: String
...
sleepAmount: Int
curious: Boolean
id: ID!
name: String!
weight: Float
status: PetStatus
photo: Photo
dueDate: Date
inCareOf: Customer
type Dog implements Pet
id: ID!
name: String
...
good: Boolean
type Rabbit implements Pet
id: ID!
name: String
...
favoriteFood: String
floppy: Int
type Stingray
implements Pet
id: ID!
name: String
...
chill: Boolean
fast: Boolean
type Horse
implements Pet
id: ID!
name: String
...
streetsmart: Boolean
majestic: Boolean
type *TBDFuturePet
implements Pet
id: ID!
name: String
...
newField: _____
newField: _____
Union Types
type Query {
familyPets: [FamilyPets!]!
}
union FamilyPet = Cat | Dog
Query
query {
familyPets {
__typename
...on Cat {
name
weight
}
...on Dog {
name
status
}
}
}
{ }
Query
Response
{ }
Query
Response
Gateway
{ }
Query
Response
Gateway
Reviews
Colors
Accounts
Thank You!
- Eve Porcello
- eve@moonhighway.com
- www.moonhighway.com
- @eveporcello
- www.graphqlworkshop.com
Schema Design
By Moon Highway
Schema Design
- 613