Skip to content

Commit

Permalink
fix: duplicate entities FDS
Browse files Browse the repository at this point in the history
  • Loading branch information
juanmardefago committed Dec 22, 2023
1 parent 5ac1b0a commit ae7b8ba
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 30 deletions.
20 changes: 14 additions & 6 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,10 @@ type GraphAccount @entity {
type GraphAccountMetadata @entity(immutable:true) {
"IPFS hash with account metadata details"
id: ID!
"Original graph account that created it"
graphAccount: [GraphAccount!] @derivedFrom(field:"metadata")
"Account that reference this metadata file. For compatibility purposes. For the full list use graphAccounts"
graphAccount: GraphAccount @derivedFrom(field:"metadata")
"Accounts that reference this metadata file"
graphAccounts: [GraphAccount!]! @derivedFrom(field:"metadata")
"True if it is an organization. False if it is an individual"
isOrganization: Boolean
"Main repository of code for the graph account"
Expand Down Expand Up @@ -440,8 +442,10 @@ type Subgraph @entity {
type SubgraphMetadata @entity(immutable:true) {
"Subgraph metadata ipfs hash"
id: ID!
"Subgraph entity"
"Subgraph that reference this metadata. For compatibility purposes. For the full list use subgraphs"
subgraph: Subgraph @derivedFrom(field:"metadata")
"Subgraphs that reference this metadata"
subgraphs: [Subgraph!]! @derivedFrom(field:"metadata")
"Short description of the subgraph"
description: String
"Image in string format"
Expand Down Expand Up @@ -503,8 +507,10 @@ type SubgraphVersion @entity {
type SubgraphVersionMetadata @entity(immutable:true) {
"Subgraph version metadata ipfs hash"
id: ID!
"Subgraph version entity"
"SubgraphVersion entity that references this metadata. For compatibility purposes. For the full list use subgraphVersions"
subgraphVersion: SubgraphVersion @derivedFrom(field:"metadata")
"SubgraphVersion entities that reference this metadata"
subgraphVersions: [SubgraphVersion!]! @derivedFrom(field:"metadata")
"Short description of the version"
description: String
"Semantic versioning label"
Expand Down Expand Up @@ -596,8 +602,10 @@ type SubgraphDeployment @entity {
type SubgraphDeploymentSchema @entity(immutable:true) {
"IPFS Hash"
id: ID!
"Link to SubgraphDeployment entity"
"Link to a SubgraphDeploymentManifest entity that references this schema. For backwards compatibility purposes only, for the full list of manifests use manifests"
manifest: SubgraphDeploymentManifest @derivedFrom(field:"schema")
"Links to SubgraphDeploymentManifest entities that reference this schema"
manifests: [SubgraphDeploymentManifest!]! @derivedFrom(field:"schema")
"Contents of the Schema file"
schema: String
}
Expand All @@ -607,7 +615,7 @@ type SubgraphDeploymentManifest @entity(immutable:true) {
id: ID!
"Link to SubgraphDeployment entity"
deployment: SubgraphDeployment @derivedFrom(field:"manifest")
"Schema entity. Not yet working due to limitations with File Data Sources"
"Schema entity"
schema: SubgraphDeploymentSchema
"Schema ipfs hash"
schemaIpfsHash: String
Expand Down
11 changes: 7 additions & 4 deletions src/mappings/ethereumDIDRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Bytes } from '@graphprotocol/graph-ts'
import { Bytes, DataSourceContext } from '@graphprotocol/graph-ts'
import { DIDAttributeChanged } from '../types/EthereumDIDRegistry/EthereumDIDRegistry'
import { GraphAccountMetadata as GraphAccountMetadataTemplate } from '../types/templates'
import { GraphAccount } from '../types/schema'
Expand All @@ -17,17 +17,20 @@ export function handleDIDAttributeChanged(event: DIDAttributeChanged): void {
// called it directly, it could crash the subgraph
let hexHash = changetype<Bytes>(addQm(event.params.value))
let base58Hash = hexHash.toBase58()
graphAccount.metadata = base58Hash
let metadataId = graphAccount.id.concat('-').concat(base58Hash)
graphAccount.metadata = metadataId
graphAccount.save()

// Update all associated vesting contract addresses
let tlws = graphAccount.tokenLockWallets
for (let i = 0; i < tlws.length; i++) {
let tlw = GraphAccount.load(tlws[i])!
tlw.metadata = base58Hash
tlw.metadata = metadataId
tlw.save()
}

GraphAccountMetadataTemplate.create(base58Hash)
let context = new DataSourceContext()
context.setString('id', metadataId)
GraphAccountMetadataTemplate.createWithContext(base58Hash, context)
}
}
39 changes: 26 additions & 13 deletions src/mappings/gns.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BigInt, BigDecimal, Bytes, log } from '@graphprotocol/graph-ts'
import { BigInt, BigDecimal, Bytes, log, DataSourceContext } from '@graphprotocol/graph-ts'
import {
SubgraphPublished,
SubgraphPublished1,
Expand Down Expand Up @@ -179,13 +179,15 @@ export function handleSubgraphMetadataUpdated(event: SubgraphMetadataUpdated): v

let hexHash = changetype<Bytes>(addQm(event.params.subgraphMetadata))
let base58Hash = hexHash.toBase58()

let metadataId = subgraph.id.concat('-').concat(base58Hash)
subgraph.metadataHash = event.params.subgraphMetadata
subgraph.metadata = base58Hash
subgraph.metadata = metadataId
subgraph.updatedAt = event.block.timestamp.toI32()
subgraph.save()

SubgraphMetadataTemplate.create(base58Hash)
let context = new DataSourceContext()
context.setString('id', metadataId)
SubgraphMetadataTemplate.createWithContext(base58Hash, context)
}

/**
Expand Down Expand Up @@ -235,11 +237,14 @@ export function handleSubgraphPublished(event: SubgraphPublished): void {
subgraphVersion.createdAt = event.block.timestamp.toI32()
let hexHash = changetype<Bytes>(addQm(event.params.versionMetadata))
let base58Hash = hexHash.toBase58()
let metadataId = subgraphVersion.id.concat('-').concat(base58Hash)
subgraphVersion.metadataHash = event.params.versionMetadata
subgraphVersion.metadata = base58Hash
subgraphVersion.metadata = metadataId
subgraphVersion.save()

SubgraphVersionMetadataTemplate.create(base58Hash)
let context = new DataSourceContext()
context.setString('id', metadataId)
SubgraphVersionMetadataTemplate.createWithContext(base58Hash, context)

let oldDeployment: SubgraphDeployment | null = null
if (oldVersionID != null) {
Expand Down Expand Up @@ -712,13 +717,15 @@ export function handleSubgraphMetadataUpdatedV2(event: SubgraphMetadataUpdated1)

let hexHash = changetype<Bytes>(addQm(event.params.subgraphMetadata))
let base58Hash = hexHash.toBase58()

let metadataId = subgraph.id.concat('-').concat(base58Hash)
subgraph.metadataHash = event.params.subgraphMetadata
subgraph.metadata = base58Hash;
subgraph.metadata = metadataId;
subgraph.updatedAt = event.block.timestamp.toI32()
subgraph.save()

SubgraphMetadataTemplate.create(base58Hash)
let context = new DataSourceContext()
context.setString('id', metadataId)
SubgraphMetadataTemplate.createWithContext(base58Hash, context)
}

// - event: SignalMinted(indexed uint256,indexed address,uint256,uint256,uint256)
Expand Down Expand Up @@ -1076,11 +1083,14 @@ export function handleSubgraphVersionUpdated(event: SubgraphVersionUpdated): voi
let subgraphVersion = SubgraphVersion.load(versionID)!
let hexHash = changetype<Bytes>(addQm(event.params.versionMetadata))
let base58Hash = hexHash.toBase58()
let metadataId = subgraphVersion.id.concat('-').concat(base58Hash)
subgraphVersion.metadataHash = event.params.versionMetadata
subgraphVersion.metadata = base58Hash
subgraphVersion.metadata = metadataId
subgraphVersion.save()

SubgraphVersionMetadataTemplate.create(base58Hash)
let context = new DataSourceContext()
context.setString('id', metadataId)
SubgraphVersionMetadataTemplate.createWithContext(base58Hash, context)
} else {
let oldVersionID = subgraph.currentVersion

Expand All @@ -1104,8 +1114,9 @@ export function handleSubgraphVersionUpdated(event: SubgraphVersionUpdated): voi
subgraphVersion.createdAt = event.block.timestamp.toI32()
let hexHash = changetype<Bytes>(addQm(event.params.versionMetadata))
let base58Hash = hexHash.toBase58()
let metadataId = subgraphVersion.id.concat('-').concat(base58Hash)
subgraphVersion.metadataHash = event.params.versionMetadata
subgraphVersion.metadata = base58Hash
subgraphVersion.metadata = metadataId

let oldDeployment: SubgraphDeployment | null = null
if (oldVersionID != null) {
Expand All @@ -1116,7 +1127,9 @@ export function handleSubgraphVersionUpdated(event: SubgraphVersionUpdated): voi
updateCurrentDeploymentLinks(oldDeployment, deployment, subgraph as Subgraph)
subgraphVersion.save()

SubgraphVersionMetadataTemplate.create(base58Hash)
let context = new DataSourceContext()
context.setString('id', metadataId)
SubgraphVersionMetadataTemplate.createWithContext(base58Hash, context)
}
}

Expand Down
19 changes: 12 additions & 7 deletions src/mappings/ipfs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { json, Bytes, dataSource, JSONValueKind, log } from '@graphprotocol/graph-ts'
import { json, Bytes, dataSource, JSONValueKind, log, DataSourceContext } from '@graphprotocol/graph-ts'
import {
SubgraphMetadata,
SubgraphVersionMetadata,
Expand All @@ -12,7 +12,8 @@ import {
import { jsonToString } from './utils'

export function handleSubgraphMetadata(content: Bytes): void {
let subgraphMetadata = new SubgraphMetadata(dataSource.stringParam())
let id = dataSource.context().getString("id")
let subgraphMetadata = new SubgraphMetadata(id)
let tryData = json.try_fromBytes(content)
if (tryData.isOk) {
let data = tryData.value.toObject()
Expand All @@ -39,7 +40,8 @@ export function handleSubgraphMetadata(content: Bytes): void {
}

export function handleSubgraphVersionMetadata(content: Bytes): void {
let subgraphVersionMetadata = new SubgraphVersionMetadata(dataSource.stringParam())
let id = dataSource.context().getString("id")
let subgraphVersionMetadata = new SubgraphVersionMetadata(id)
let tryData = json.try_fromBytes(content)
if (tryData.isOk) {
let data = tryData.value.toObject()
Expand All @@ -50,7 +52,8 @@ export function handleSubgraphVersionMetadata(content: Bytes): void {
}

export function handleGraphAccountMetadata(content: Bytes): void {
let graphAccountMetadata = new GraphAccountMetadata(dataSource.stringParam())
let id = dataSource.context().getString("id")
let graphAccountMetadata = new GraphAccountMetadata(id)
let tryData = json.try_fromBytes(content)
if (tryData.isOk) {
let data = tryData.value.toObject()
Expand All @@ -69,7 +72,8 @@ export function handleGraphAccountMetadata(content: Bytes): void {


export function handleSubgraphDeploymentSchema(content: Bytes): void {
let subgraphDeploymentSchema = new SubgraphDeploymentSchema(dataSource.stringParam())
let id = dataSource.context().getString("id")
let subgraphDeploymentSchema = new SubgraphDeploymentSchema(id)
if (content !== null) {
subgraphDeploymentSchema.schema = content.toString()
}
Expand Down Expand Up @@ -97,8 +101,9 @@ export function handleSubgraphDeploymentManifest(content: Bytes): void {
subgraphDeploymentManifest.schema = schemaIpfsHash
subgraphDeploymentManifest.schemaIpfsHash = schemaIpfsHash

// Can't create this template here yet (due to current implementation limitations on File Data Sources, but once that's sorted out, this should work.)
SubgraphDeploymentSchemaTemplate.create(schemaIpfsHash)
let context = new DataSourceContext()
context.setString('id', subgraphDeploymentManifest.id.concat('-').concat(schemaIpfsHash))
SubgraphDeploymentSchemaTemplate.createWithContext(schemaIpfsHash, context)
} else {
log.warning("[MANIFEST PARSING FAIL] subgraphDeploymentManifest: {}, schema file hash can't be retrieved. Error: schemaIpfsHashTry.length isn't 2, actual length: {}", [dataSource.stringParam(), schemaIpfsHashTry.length.toString()])
}
Expand Down

0 comments on commit ae7b8ba

Please sign in to comment.