Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
GraphQL subscriptions
Search
fr0gM4ch1n3
July 10, 2018
Education
84
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
GraphQL subscriptions
fr0gM4ch1n3
July 10, 2018
More Decks by fr0gM4ch1n3
See All by fr0gM4ch1n3
Colors
fr0gm4ch1n3
1
38
Water Drop Refraction macro photography
fr0gm4ch1n3
0
32
GraphQL integration with Redux
fr0gm4ch1n3
0
75
GraphQL basics with the Apollo Client
fr0gm4ch1n3
0
59
Redux with GraphQL integration
fr0gm4ch1n3
0
61
Other Decks in Education
See All in Education
人間のために、人間と共に働く時代の終わりの始まり
frievea
0
170
ちいかわを読め
uchi8977
1
330
[2026前期火5] 論理学(京都大学文学部 前期 第14回)「計算は、証明ではない——ハルシネーションを三層ハーモニーで診る」
yatabe
0
250
Comentario del plano urbano de Madrid hasta 1860 (1ª parte )
juanmartin2026
1
64k
チームの鏡になるー自分の癖を知ると、チームのパターンが見えてくる@スクフェス仙台
saorimurooka
0
180
Examen de Selectividad. Geografía julio 2026 (Convocatoria Extraordinaria). UCLM
juanmartin2026
1
13k
Πλουτοκρατία: Η Τυραννία του Μαμμωνά και η Μεταανθρώπινη Δουλεία
amethyst1
0
310
批判的応用言語学ワークショップ(2026-08-12 お茶の⽔⼥⼦⼤学)
terasawat
0
190
Sanapilvet opetuksessa
matleenalaakso
0
36k
[2026前期火5] 論理学(京都大学文学部 前期 第9回)「正規化の停止性——ヒドラゲームによる証明」
yatabe
0
270
Geografía y fútbol. Atlanta. la megalópolis del fútbol
juanmartin2026
1
12k
2026年度春学期 統計学 第10回 分布の推測とは - 標本調査,度数分布と確率分布 (2026. 6. 4)
akiraasano
PRO
0
210
Featured
See All Featured
Embracing the Ebb and Flow
colly
88
5.2k
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
380
We Are The Robots
honzajavorek
0
360
Designing Powerful Visuals for Engaging Learning
tmiket
1
540
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
530
4 Signs Your Business is Dying
shpigford
187
23k
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.5k
AI: The stuff that nobody shows you
jnunemaker
PRO
9
990
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
240
Leo the Paperboy
mayatellez
9
2.3k
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
320
Transcript
GraphQL subscriptions
Preview There are some public APIs to play with: •
SWAPI - The Star Wars API • Deutsche Bahn • And more …
Good and bad part Round Trips
DataLoader Batching is not an advanced feature, it's DataLoader's primary
feature DataLoader provides a memoization cache for all loads userLoader.load(1) .then(user => userLoader.load(user.invitedByID)) .then(invitedBy => console.log(`User 1 was invited by ${invitedBy}`)); Example
DataLoader helper const applyOrder = (idField: string, order: any[], input:
any[], emptyElement: ObjectConstructor = Object) => { const _input: any[] = [], result = []; for (let index0 = 0, len1 = order.length; index0 < len1; index0++) { _input.push(Object.assign({}, order[index0])); } loop: for (let index1 = 0, len1 = order.length; index1 < len1; index1++) { for (let index2 = 0, len2 = _input.length; index2 < len2; index2++) { if (order[index1] === _input[index2][idField]) { result.push(_input[index2]); index2 = len2; continue loop; } } result.push(new emptyElement()); } return result; };
Overview GraphQL subscriptions have to be defined in the schema,
just like queries and mutations: type Subscription { commentAdded(repoFullName: String!): Comment }
Overview On the client, subscription queries look just like any
other kind of operation: subscription onCommentAdded($repoFullName: String!){ commentAdded(repoFullName: $repoFullName){ id content } }
Overview The response sent to the client looks as follows:
{ "data": { "commentAdded": { "id": "123", "content": "Hello!" } } }
Server setup server.use('/graphql', bodyParser.json(), graphqlExpress({ schema })); server.use('/graphiql', graphiqlExpress({ endpointURL:
'/graphql', subscriptionsEndpoint: `ws://localhost:${PORT}/subscriptions` })); const ws = createServer(server); ws.listen(PORT, () => { new SubscriptionServer({ execute, subscribe, schema }, { server: ws, path: '/subscriptions', }); });
Client setup Add support for this transport to Apollo Client
import { WebSocketLink } from "apollo-link-ws"; import { SubscriptionClient } from "subscriptions-transport-ws"; const GRAPHQL_ENDPOINT = "ws://localhost:3000/graphql"; const client = new SubscriptionClient(GRAPHQL_ENDPOINT, { reconnect: true }); const link = new WebSocketLink(client);
Subscription Component Subscriptions are just listeners, they don’t request any
data! Still need an initial query: const COMMENT_QUERY = gql` query Comment($repoName: String!) { entry(repoFullName: $repoName) { comments { id content } } } `; this.commentsQuery = apollo.watchQuery({ query: COMMENT_QUERY, variables: { repoName: `${params.org}/${params.repoName}` } }); this.comments = this.commentsQuery.valueChanges;
Subscription Component same component ... const COMMENTS_SUBSCRIPTION = gql` subscription
onCommentAdded( $repoFullName: String!){ commentAdded(repoFullName: $repoFullName){ id content } } `; this.commentsQuery.subscribeToMore({ document: COMMENTS_SUBSCRIPTION, variables: { repoFullName: params.repoFullName, }, updateQuery: (prev, {subscriptionData}) => { if (!subscriptionData.data) { return prev; } return { ...prev, entry: { comments: [ subscriptionData.data.commentAdded, ...prev.entry.comments] } }; } });
Integration with NativeScript You can use Apollo with NativeScript exactly
as you would with normal Angular application.
GraphQL-IO
subZero Software development stack, with the primary focus of building
GraphQL and REST APIs backed by a PostgreSQL database
Example App