Skip to content

Commit 5c7cec2

Browse files
authored
feat(network-subgraphs): Add idAsString field (#1024)
Added new field to `Stream` entity: `Stream#idAsString`. It has same value as in the `id` field. The field allows us to do substring queries for the field id (by using `idAsString_contains` where clause). This feature is needed e.g. in streamr-dev/network#3132.
1 parent ef5b501 commit 5c7cec2

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

packages/network-subgraphs/schema.graphql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ type StreamPermission @entity {
3535
type Stream @entity {
3636
"stream ID = 'creator address'/'path' where path can be any string"
3737
id: ID!
38+
"This field has the same value as the ID field. It enables us to perform substring queries on the id field using the idAsString_contains where clause"
39+
idAsString: String! @index
3840
"Stream metadata JSON"
3941
metadata: String!
4042
"Permissions that each Ethereum address owns to this stream"

packages/network-subgraphs/src/streamRegistry.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export function handleStreamCreation(event: StreamCreated): void {
2020
return
2121
}
2222
let stream = new Stream(event.params.id)
23+
stream.idAsString = event.params.id
2324
stream.metadata = event.params.metadata
2425
stream.createdAt = event.block.timestamp
2526
stream.updatedAt = event.block.timestamp
@@ -37,7 +38,15 @@ export function handleStreamUpdate(event: StreamUpdated): void {
3738
[event.params.id, event.params.metadata, event.block.number.toString()])
3839
let stream = Stream.load(event.params.id)
3940
if (stream === null) {
41+
// If the stream was initialized using the trustedSetStreamMetadata() method instead of the usual createStream(),
42+
// the Stream entity does not yet exist. This pattern was used at least in the brubeck-migration-script
43+
// (see https://github.com/streamr-dev/network-contracts/pull/1007), and possibly also in ENS stream creation
44+
// (see https://github.com/streamr-dev/network-contracts/pull/109).
45+
// The trustedSetStreamMetadata() method, which existed in StreamRegistry before version 5, only emitted
46+
// the StreamUpdated event. Since no StreamCreated event was triggered during this process, the handleStreamCreation()
47+
// function was not called, and therefore the Stream entity wasn't created. For this reason we need to create it here.
4048
stream = new Stream(event.params.id)
49+
stream.idAsString = event.params.id
4150
stream.createdAt = event.block.timestamp
4251
}
4352
stream.metadata = event.params.metadata

0 commit comments

Comments
 (0)