Skip to content

Instantly share code, notes, and snippets.

@kellishouts
Last active August 16, 2016 02:34
Show Gist options
  • Save kellishouts/ed4a6c79e94da8c2abd56ea3abb0d4e5 to your computer and use it in GitHub Desktop.
Save kellishouts/ed4a6c79e94da8c2abd56ea3abb0d4e5 to your computer and use it in GitHub Desktop.
GraphQL

GraphQL

Pre-Requisites

This material should come after ABC and definitely before ABC. Students should already be familiar with ABC and ABC.

Class Format / Time to Allow for Subject

This material usually takes XXX-time to introduce and XXX-time for exercises and reinforcement.

About this Topic

This module covers GraphQL as an API and database interlayer. Using GraphQL with a client side framework is part of the FrontEndFrameworks/GraphQL Module.

Topics & Expected Outcomes

Levels of Understanding

Students will have one of three levels of understanding about each topic upon completion of this module.

  • grok: fully understand the topic in order to replicate code, communicate, and explain concepts without referring to any notes.
  • explain: understand enough about the topic to describe concepts without referring to notes.
  • know about: understand enough to look up further documentation when asked about the subject.

  • Students should know about XXX-thing
  • Students should grok XXX-thing
  • Students should be able to explain XXX-thing

Suggested Format for Delivery

The following format is meant to be a guideline for effective delivery. Instructors can present material in another way if it is more effective for the students.

Introduce GraphQL at a high level

Properties

  • Declarative Nature
  • Compositional Nature
  • Strong-Typed Nature

Discuss the two parts

  • Query Language
    • standard
  • Runtime
    • the interlayer that translates the graphql query language stuff into something the server understands, like sql etc.
      • whiteboard : (Client) - (GraphQL) - (Server)
    • it's an agent that translates to the server, or multiple servers
    • standardizes communication
    • single point to communicate with
      • cause usually data is stored in multiple places

Explain benefits from the client perspective:

  • Declarative nature
    • describe what you want, not how to get it
    • data requirement is declarative

Question: What kind of structure would you use to represent data?

  • graphs
  • any data is a graph
    • because there are nodes
      • any node could be connected to the other nodes
    • true to any database

To work with any graph you need to start at some node

  • once you start at a node, the graph becomes a tree
    • a tree is just a specialized version of a graph
  • because it's a graph, there are multiple root levels
    • every root level will be it's own tree

GraphQL - Interactive Intro

http://slides.com/theremix/graphql-interactive-intro

  • GraphQL language
  • Graphiql
  • queries
    • notice the query is the same structure as the result!
      • declarative power to achieve the mental model
      • query drives the shape of the response
      • if you have the query, you know the shape of the response
      • be confident without having to inspect, console.log, read documentation
    • inverse is true!
      • if you have the response, you can create the query request
    • no overfetching!
    • client is not filtering the data
    • server is actually querying exactly what you need
  • introspective api
  • arguments
  • query variables
  • mutation
  • subscription
  • fragments
  • aliases
  • directive
    • in Relay, if you flip the switch, it only sends data about the avatar

Sample Final Query

https://www.graphqlhub.com/playground?query=query%20GithubUserRepos%20(%24username%3AString!%2C%20%24showAvatar%3A%20Boolean!)%20%7B%0A%20%20github%20%7B%0A%20%20%20%20user(username%3A%20%24username)%20%7B%0A%20%20%20%20%20%20id%0A%20%20%20%20%20%20company_name%3A%20company%0A%20%20%20%20%20%20avatar_url%20%40include(if%3A%20%24showAvatar)%0A%20%20%20%20%20%20repos%20%7B%0A%20%20%20%20%20%20%20%20...RepoInfo%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%0A%0Aquery%20GithubQueryFB%20%7B%0A%20%20github%20%7B%0A%20%20%20%20repo(ownerUsername%3A%20%22facebook%22%2C%20name%3A%20%22graphql%22)%20%7B%0A%20%20%20%20%20%20...RepoInfo%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%0A%0Afragment%20RepoInfo%20on%20GithubRepo%20%7B%0A%20%20name%0A%20%20commits%20%7B%0A%20%20%20%20message%0A%20%20%7D%0A%7D&variables=%7B%0A%20%20%22username%22%3A%20%22theRemix%22%2C%0A%20%20%22showAvatar%22%3A%20true%0A%7D

Extended Intro Optional (if there is time)

Exercises & Projects

GraphQL Server

The exercise is conducted as a live workshop led by the slideshow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment