-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathgraph.html
More file actions
337 lines (306 loc) · 8.74 KB
/
graph.html
File metadata and controls
337 lines (306 loc) · 8.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
---
layout: default
title: Graph
permalink: /graph/
section: graph
---
<h1 class="page-header">Graph</h1>
<p>
<code>graph.cash</code> is a GraphQL indexer for the Memo network.
It exposes posts, profiles, transactions, blocks, and SLP token data through queries and live subscriptions.
</p>
<ul>
<li><a href="#graph-endpoint">Endpoint</a></li>
<li><a href="#graph-schema">Schema</a></li>
<li><a href="#graph-queries">Queries</a></li>
<li><a href="#graph-subscriptions">Subscriptions</a></li>
<li><a href="#graph-mutations">Mutations</a></li>
<li><a href="#graph-scalars">Scalars</a></li>
</ul>
<br>
<a class="anchor" id="graph-endpoint"></a>
<h2>
Endpoint
<a class="anchor-link" href="#graph-endpoint">
<span class="glyphicon glyphicon-link"></span>
</a>
</h2>
<p>
The endpoint accepts <code>POST</code> for queries and mutations, and a WebSocket upgrade for subscriptions.
</p>
<h4>HTTP</h4>
<pre>https://graph.cash/graphql</pre>
<h4>WebSocket</h4>
<pre>wss://graph.cash/graphql</pre>
<p>
Subscriptions follow the
<a href="https://github.com/enisdenjo/graphql-ws/blob/master/PROTOCOL.md">graphql-transport-ws</a>
protocol.
A live playground with introspection is available for development.
</p>
<br>
<a class="anchor" id="graph-schema"></a>
<h2>
Schema
<a class="anchor-link" href="#graph-schema">
<span class="glyphicon glyphicon-link"></span>
</a>
</h2>
<p>
Top-level types and the operations that return them.
</p>
<pre><code class="language-graphql">type Query {
tx(hash: Hash!): Tx
txs(hashes: [Hash!]): [Tx]
address(address: Address!): Lock
addresses(addresses: [Address!]): [Lock]
block(hash: Hash!): Block
block_by_height(height: Int!): Block
block_newest: Block
blocks(newest: Boolean, start: Uint32): [Block!]
profiles(addresses: [Address!]): [Profile]
posts(txHashes: [Hash!]): [Post]
posts_newest(start: Date, tx: Hash, limit: Uint32): [Post]
room(name: String!): Room!
}
type Subscription {
tx(hash: Hash!): Tx
address(address: Address!): Tx
addresses(addresses: [Address!]): Tx
blocks: Block
posts(hashes: [Hash!]): Post
profiles(addresses: [Address!]): Profile
rooms(names: [String!]): Post
room_follows(addresses: [Address!]): RoomFollow
}
type Mutation {
broadcast(raw: String!): Boolean!
}</code></pre>
<p>
Selected object types most relevant for building feeds and profile views:
</p>
<pre><code class="language-graphql">type Post {
tx_hash: Hash!
address: Address!
text: String!
tx: Tx!
lock: Lock!
likes: [Like!]
parent: Post
replies: [Post!]
room: Room
}
type Profile {
address: Address!
lock: Lock!
name: SetName
profile: SetProfile
pic: SetPic
following(start: Date): [Follow]
followers(start: Date): [Follow]
posts(start: Date, newest: Boolean): [Post]
rooms(start: Date): [RoomFollow!]
}
type Lock {
address: Address
profile: Profile
txs(start: Date, tx: Hash, limit: Uint32): [Tx!]
}
type Tx {
hash: Hash!
raw: Bytes!
inputs: [TxInput!]!
outputs: [TxOutput!]!
blocks: [TxBlock]
seen: Date
version: Int32!
locktime: Uint32!
}
type Room {
name: String!
posts(start: Int): [Post!]
followers(start: Int): [RoomFollow!]
}</code></pre>
<br>
<a class="anchor" id="graph-queries"></a>
<h2>
Queries
<a class="anchor-link" href="#graph-queries">
<span class="glyphicon glyphicon-link"></span>
</a>
</h2>
<h3>Newest posts</h3>
<p>
Page through recent posts. Pass <code>tx</code> with the oldest post's hash from the previous page to paginate.
</p>
<pre><code class="language-graphql">query Newest($limit: Uint32) {
posts_newest(limit: $limit) {
tx_hash
text
address
tx { seen }
lock { profile { name { name } } }
room { name }
}
}</code></pre>
<h3>Single post and replies</h3>
<pre><code class="language-graphql">query Post($hash: Hash!) {
posts(txHashes: [$hash]) {
tx_hash
text
address
likes { address tip }
replies {
tx_hash
text
address
}
}
}</code></pre>
<h3>Profile with recent posts</h3>
<pre><code class="language-graphql">query Profile($address: Address!) {
profiles(addresses: [$address]) {
address
name { name }
profile { text }
pic { pic }
posts(newest: true) {
tx_hash
text
tx { seen }
}
}
}</code></pre>
<h3>Address transactions</h3>
<pre><code class="language-graphql">query AddressTxs($address: Address!) {
address(address: $address) {
txs(limit: 25) {
hash
seen
outputs { amount lock { address } }
}
}
}</code></pre>
<h3>Calling from JavaScript</h3>
<pre><code class="language-javascript">async function graphql(query, variables) {
const res = await fetch("https://graph.cash/graphql", {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify({query, variables})
});
const json = await res.json();
if (json.errors) {
console.error(json.errors);
}
return json.data;
}</code></pre>
<br>
<a class="anchor" id="graph-subscriptions"></a>
<h2>
Subscriptions
<a class="anchor-link" href="#graph-subscriptions">
<span class="glyphicon glyphicon-link"></span>
</a>
</h2>
<p>
Subscriptions stream new events as they are indexed. Pass an empty list to <code>posts(hashes: [])</code>
to receive every new post on the network; pass specific hashes to receive replies and likes for those posts.
</p>
<h3>All new posts</h3>
<pre><code class="language-graphql">subscription {
posts(hashes: []) {
tx_hash
text
address
tx { seen }
lock { profile { name { name } } }
room { name }
}
}</code></pre>
<h3>Replies and likes for specific posts</h3>
<pre><code class="language-graphql">subscription Watch($hashes: [Hash!]) {
posts(hashes: $hashes) {
tx_hash
text
parent { tx_hash }
likes { address tip }
}
}</code></pre>
<h3>Address activity</h3>
<pre><code class="language-graphql">subscription Activity($address: Address!) {
address(address: $address) {
hash
seen
}
}</code></pre>
<h3>New blocks</h3>
<pre><code class="language-graphql">subscription {
blocks {
hash
height
timestamp
}
}</code></pre>
<h3>Connecting from JavaScript</h3>
<p>
Subscriptions use the <code>graphql-transport-ws</code> sub-protocol over a WebSocket.
The minimum handshake is <code>connection_init</code> → <code>connection_ack</code>, then
<code>subscribe</code> for each subscription, and <code>next</code> messages stream back.
</p>
<pre><code class="language-javascript">const ws = new WebSocket("wss://graph.cash/graphql", "graphql-transport-ws");
ws.onopen = () => {
ws.send(JSON.stringify({type: "connection_init"}));
};
ws.onmessage = (event) => {
const msg = JSON.parse(event.data);
if (msg.type === "connection_ack") {
ws.send(JSON.stringify({
id: "1",
type: "subscribe",
payload: {
query: "subscription { posts(hashes: []) { tx_hash text address } }"
}
}));
} else if (msg.type === "next") {
console.log("new post", msg.payload.data.posts);
} else if (msg.type === "ping") {
ws.send(JSON.stringify({type: "pong"}));
}
};</code></pre>
<br>
<a class="anchor" id="graph-mutations"></a>
<h2>
Mutations
<a class="anchor-link" href="#graph-mutations">
<span class="glyphicon glyphicon-link"></span>
</a>
</h2>
<p>
The graph supports broadcasting raw transactions. Sign the transaction client-side (for example with
<a href="#jmemo">JMemo</a>) and submit the hex-encoded raw transaction:
</p>
<pre><code class="language-graphql">mutation Broadcast($raw: String!) {
broadcast(raw: $raw)
}</code></pre>
<br>
<a class="anchor" id="graph-scalars"></a>
<h2>
Scalars
<a class="anchor-link" href="#graph-scalars">
<span class="glyphicon glyphicon-link"></span>
</a>
</h2>
<ul>
<li><code>Hash</code> — 32-byte transaction or block hash, hex-encoded.</li>
<li><code>Address</code> — Bitcoin Cash address (legacy <code>1...</code> form).</li>
<li><code>Bytes</code> — arbitrary byte sequence, hex-encoded.</li>
<li><code>Date</code> — RFC 3339 timestamp.</li>
<li><code>Int32</code>, <code>Int64</code>, <code>Uint8</code>, <code>Uint32</code>, <code>Uint64</code> — sized integers.</li>
<li><code>HashIndex</code> — transaction hash + output index pair.</li>
</ul>
<br>
<h2>Live example</h2>
<p>
See the <a href="{{ '/example/posts-feed/' | relative_url }}">posts feed example</a> for a self-contained page that
renders the live posts subscription and paginates with <code>posts_newest</code>.
</p>