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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
fr0gM4ch1n3
July 10, 2018
Education
85
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
39
Water Drop Refraction macro photography
fr0gm4ch1n3
0
33
GraphQL integration with Redux
fr0gm4ch1n3
0
76
GraphQL basics with the Apollo Client
fr0gm4ch1n3
0
60
Redux with GraphQL integration
fr0gm4ch1n3
0
62
Other Decks in Education
See All in Education
Quality Control of Crude Drugs
pawan_pharm
0
160
良書紹介08_ 頭のいい子がやっているすごいグラフの読み方
bunnchinn3
0
160
新しいJavaを学んで・使っていこう! / osd26do
gishi_yama
0
260
NDIAS Automotive / IoT CTF 2026 Recap - Keyfob & OSINT
himitu23
0
340
セキュリティ担当がいない中小医療機関を院外のRISSはどう支えるべきか / How External RISS Can Help Small Medical Institutions
eller86
1
210
Sanapilvet opetuksessa
matleenalaakso
1
36k
1人 × AI、1か月でここまで作れる ー 数年前の外注換算3.8〜7.4億円・241〜379人月分の作業を、AI費用 約10万円・31日で
frievea
0
530
学習者データを「見る」:外国語教師のためのデータの入力、分析、解釈方法
uranoken
0
340
Geografía y Fútbol: Chattanooga Geografía del Búnker de La Roja.
juanmartin2026
1
12k
Stardy 会社紹介資料
stardy
0
8.6k
実践プレゼンテーション・デザイン
dragon2
1
130
CLASSIFICATION OF CRUDE DRUGS
pawan_pharm
0
190
Featured
See All Featured
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
1
540
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
1.1k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.3k
Claude Code のすすめ
schroneko
67
230k
The untapped power of vector embeddings
frankvandijk
2
1.9k
Being A Developer After 40
akosma
91
590k
We Are The Robots
honzajavorek
0
380
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
3k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.5k
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
2
290
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