Making SPA Smarter With GraphQL
Making SPA Smarter With GraphQL
Presented By
Making SPA [Smarter!]! (?)
with GraphQL
Ken Rimple
Director, Training and Mentoring - Chariot Solutions
Philly Emerging Technologies for the Enterprise 2019
Graph Querying Language
• Data is fetched and updated via a graph-like hierarchy of objects
• Data types
• Queries
• Mutations
• Subscriptions
An alternative to REST APIs
• Well-defined schema
type Quiz {
id: Int! Types are defined based
on primitives such as
name: String!
Int, String, Boolean
description: String! and other types
}
Query with GraphQL
import {client} from './apollo-graphql-client';
import gql from 'graphql-tag';
• Newest feature of GraphQL (2018) and hence support is all over the map
Defining Subscriptions
type Subscription { Same semantics as
nextMessage: GamePlayMessage queries but they
} are refreshed by
the server
type GamePlayMessage {
gameOver: Boolean (can have input parameters
leaderBoard: [LeaderBoardEntry!]! and return responses)
}
type LeaderBoardEntry {
playerId: String!
nickName: String!
score: Int!
}
Summary of Type Syntax Oddities
• Thing - A Thing. It is not required
• [Thing] - An array of things. The array isn’t required, nor are the individual
entries (which is odd, but a GrapQL-ism)
• Can generate TypeScript, Flow, Java, even integrate to client APIs such as
Apollo
• Apollo GraphQL
• JavaScript • Go
• Java • Groovy
• Scala • Erlang
• Python • Clojure
• Ruby • Elixr…
GraphiQL Playground
GraphQL Voyager
GraphQL Implementations
• Relay - Facebook’s JS RI
• GraphQL-js - a reference implementation
• Apollo - JavaScript clients and Servers, maintained by Prisma (our focus today)
• Lightweight express-GraphQL, HAPI-GraphQL projects
• Clients/Servers for many other languages / platforms including Java/Spring,
Scala, Clojure, Ruby, Python, PHP, Go, .NET, Elixir, Haskell and more
• Some query tools provide curl examples to the clipboard… (I didn’t type
this in!)
• You need to decide what level of granularity your API will support and you
need to implement each query/mutation/subscription, or delegate it to an API
for processing (like PRISMA)
Compared to JSON
GraphQL REST/JSON