Topics
Brief History
Why GraphQL?
Discuss: Think about your current projects. Can you see potential for using GraphQL?
The consumer is always right
Exercises
Queries
{
themes {
primaryColor
}
theme(id: "survivejs") {
primaryColor
secondaryColor
background
}
}
Queries with Ids
query getTheme {
themes {
primaryColor
}
theme(id: "survivejs") {
primaryColor
secondaryColor
background
}
}
Queries with Variables
query($themeID: ID!) {
theme(id: $themeID) {
primaryColor
}
}
Variables
{
"themeID": "survivejs"
}
Fragments
fragment SponsorFragment on Contact {
name
}
query RootQuery($conferenceId: ID!) {
conference(id: $conferenceId) {
partners {
...SponsorFragment
}
goldSponsors {
...SponsorFragment
}
}
}
Aliases
{
colors: themes {
primaryColor
}
}
Mutations
mutation {
changePresentationTheme(
presentationID: "intro-to-graphql",
themeID: "survivejs"
) {
id
theme {
id
}
}
}
While query fields are executed in parallel, mutation fields run in series, one after the other. - GraphQL documentation
Playgrounds
Discuss: Investigate the playgrounds. Can you see what functionality they have?
Exercises
Use
conferenceId
graphql-finland-2018
as anid
to theconference
query. See the schema explorer. You can count the data using a little script or perform the request using graphql-request and then process it further.
Exercises
LinusTorvalds
in GitHub? (Not to be confused with Linux Linus.)Exercises
GraphQL Schema Definition Language (SDL)
Types
type Presentation {
id: ID!
theme: Theme!
slides: [Slide]!
}
Exercise: Which types does GraphQL support out of the box?
Enums
enum Layout {
TITLE
SECTION
EMBED
MARKDOWN
}
Discuss: What's the use case for enums? Where would use you use them?
Unions
union ContentType =
TitleContent
| SectionContent
| EmbedContent
| MarkdownContent
Discuss: What's the use case for unions? Where would use you use them?
Interfaces
interface Content {
title: String
background: ContentBackground
}
Discuss: What's the use case for interfaces? Where would use you use them?
Queries
type Query {
themes: [Theme]!
theme(id: String!): Theme
presentations: [Presentation]!
presentation(id: String!): Presentation
}
Discuss: What's the use case for queries? Where would use you use them?
Mutations
type Mutation {
changePresentationTheme(
presentationID: String!,
themeID: String!
): Presentation
}
Discuss: What's the use case for mutations? Where would use you use them?
Introspection
{
__schema {
types {
name
}
}
}
Discuss: What's the use case for introspection? Where would use you use it?
Integrating GraphQL with Tools
Exercises
Interface
and a Union
?Answers 2-1
{
conference(id: "graphql-finland-2018") {
speakers {
name
}
}
}
Answers 2-2
{
allFilms {
films {
title
releaseDate
}
}
}
Answers 2-3
query {
licenses {
name
}
}
Answers 2-4
query {
user(login: "LinusTorvalds") {
followers(first: 10) {
nodes {
name
}
}
}
}