Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
GraphQL subscriptions
Search
fr0gM4ch1n3
July 10, 2018
Education
80
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
34
Water Drop Refraction macro photography
fr0gm4ch1n3
0
24
GraphQL integration with Redux
fr0gm4ch1n3
0
70
GraphQL basics with the Apollo Client
fr0gm4ch1n3
0
52
Redux with GraphQL integration
fr0gm4ch1n3
0
55
Other Decks in Education
See All in Education
Public Space Is Not For Sale
drikkes
0
120
Soluciones al examen de Geografía 2026. JULIO (Convocatoria Extraordinaria)
juanmartin2026
0
1.7k
From Days to Minutes: How We Taught an AI to Onboard 50+ Tenants on our AI Features
mfcabrera
0
190
Πλουτοκρατία: Η Τυραννία του Μαμμωνά και η Μεταανθρώπινη Δουλεία
amethyst1
0
270
Science Tokyo国際卓越研究大学計画_202604
sciencetokyo
PRO
0
5.6k
Gitがない時代 インターネットがない時代の 開発話
sapi_kawahara
0
310
プログラミング言語において文字列を複数行にわたって だらだらと記載するアレ
sapi_kawahara
0
170
[2026前期火5] 論理学(京都大学文学部 前期 第3回)「形式言語と四つのキーワード:メタ・構成・意味論・ハーモニー」
yatabe
0
580
2026年度春学期 統計学 第1回 イントロダクション ー 統計的なものの見方・考え方について (2026. 4. 9)
akiraasano
PRO
0
190
Catecismo 26 #2 - Do Credo; Introdução ao 1º artigo
cm_manaus
0
140
Visionary Initiative: Future Intelligence — Laying the foundations for the future of science, intelligence, and society | Science Tokyo
sciencetokyo
PRO
0
110
AI時代に、 なぜ英語を勉強するのか
empelt
0
120
Featured
See All Featured
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
1k
Code Reviewing Like a Champion
maltzj
528
40k
New Earth Scene 8
popppiees
3
2.4k
Done Done
chrislema
186
16k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.6k
Discover your Explorer Soul
emna__ayadi
2
1.2k
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
62
44k
Leo the Paperboy
mayatellez
7
1.9k
How to Ace a Technical Interview
jacobian
281
24k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.5k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
380
The Art of Programming - Codeland 2020
erikaheidi
57
14k
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