Brief Introduction to GraphQL

Juho Vepsäläinen

Topics

  • What is GraphQL?
  • GraphQL Queries
  • GraphQL Schema
  • Implementing a Server
  • Solutions
1/40

What is GraphQL?

2/40

Brief History

  • 1930s - Operators between client and server
  • 2000s - REST between client and server
  • 2010s - GraphQL between client and server
3/40

GraphQL

4/40

Why GraphQL?

  • You have multiple consumers
  • You have multiple legacy APIs and want to unite them
  • You write applications

    Discuss: Think about your current projects. Can you see potential for using GraphQL?

5/40

The consumer is always right

6/40

Exercises

  1. Find a couple of GraphQL APIs online.
  2. What sort of functionality do the APIs implement?
  3. What commonalities do the APIs have?
  4. What differences do the APIs have?
7/40

GraphQL Queries

8/40

Queries

{
  themes {
    primaryColor
  }
  theme(id: "survivejs") {
    primaryColor
    secondaryColor
    background
  }
}
9/40

Queries with Ids

query getTheme {
  themes {
    primaryColor
  }
  theme(id: "survivejs") {
    primaryColor
    secondaryColor
    background
  }
}
10/40

Queries with Variables

query($themeID: ID!) {
  theme(id: $themeID) {
    primaryColor
  }
}

Variables

{
  "themeID": "survivejs"
}
11/40

Fragments

fragment SponsorFragment on Contact {
  name
}

query RootQuery($conferenceId: ID!) {
  conference(id: $conferenceId) {
    partners {
      ...SponsorFragment
    }
    goldSponsors {
      ...SponsorFragment
    }
  }
}
12/40

Aliases

{
  colors: themes {
    primaryColor
  }
}
13/40

Mutations

mutation {
  changePresentationTheme(
    presentationID: "intro-to-graphql",
    themeID: "survivejs"
  ) {
    id
    theme {
      id
    }
  }
}
14/40

While query fields are executed in parallel, mutation fields run in series, one after the other. - GraphQL documentation

15/40

Playgrounds

Discuss: Investigate the playgrounds. Can you see what functionality they have?

16/40

Exercises

  1. How many speakers does GraphQL Finland 2018 have? API. Source.

    Use conferenceId graphql-finland-2018 as an id to the conference 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.

18/40

Exercises

  1. What was the title and release date of the first Star Wars movie? API. Source.
  2. How many open source licenses is GitHub aware of? API. Documentation.
  3. What are the names of the followers of the user LinusTorvalds in GitHub? (Not to be confused with Linux Linus.)
19/40

Exercises

  1. Choose a client from the list and try performing some of the queries using it.
  2. Look up GraphQL directives. What's the purpose of those? Where would you use them?
20/40

GraphQL Schema

21/40

GraphQL Schema Definition Language (SDL)

  • A definition language is used to define an API
  • Separate language to describe types and their relations
  • In addition code implementing Queries and Mutations is needed
22/40

Types

type Presentation {
  id: ID!
  theme: Theme!
  slides: [Slide]!
}

Exercise: Which types does GraphQL support out of the box?

23/40

Enums

enum Layout {
  TITLE
  SECTION
  EMBED
  MARKDOWN
}

Discuss: What's the use case for enums? Where would use you use them?

24/40

Unions

union ContentType =
    TitleContent
  | SectionContent
  | EmbedContent
  | MarkdownContent

Discuss: What's the use case for unions? Where would use you use them?

25/40

Interfaces

interface Content {
  title: String
  background: ContentBackground
}

Discuss: What's the use case for interfaces? Where would use you use them?

26/40

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?

27/40

Mutations

type Mutation {
  changePresentationTheme(
    presentationID: String!,
    themeID: String!
  ): Presentation
}

Discuss: What's the use case for mutations? Where would use you use them?

28/40

Introspection

{
  __schema {
    types {
      name
    }
  }
}

Discuss: What's the use case for introspection? Where would use you use it?

29/40

Exercises

  1. What's the difference between an Interface and a Union?
  2. Model a schema for a small presentation application API.
  3. Try graphqlviz against an API.
  4. Look up JSON Schema. What's the relationship between that and GraphQL SDL? See also graphql-json-schema.
31/40

Implementing a Server

32/40

Apollo Server

34/40

Solutions

36/40

Answers 2-1

{
  conference(id: "graphql-finland-2018") {
    speakers {
      name
    }
  }
}
37/40

Answers 2-2

{
  allFilms {
    films {
      title
      releaseDate
    }
  }
}
38/40

Answers 2-3

query {
  licenses {
    name
  }
}
39/40

Answers 2-4

query {
  user(login: "LinusTorvalds") {
    followers(first: 10) {
      nodes {
        name
      }
    }
  }
}
40/40