Skip to content

Moved to one SubgraphManifest schema file per protocol #821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 0 additions & 122 deletions manifest-schema.graphql

This file was deleted.

92 changes: 92 additions & 0 deletions src/protocols/ethereum/manifest.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Each referenced type's in any of the types below must be listed
# here either as `scalar` or `type` for the validation code to work
# properly.
#
# That's why `String` is listed as a scalar even though it's built-in
# GraphQL basic types.
scalar String
scalar File
scalar BigInt

type SubgraphManifest {
specVersion: String!
features: [String!]
schema: Schema!
description: String
repository: String
graft: Graft
dataSources: [DataSource!]!
templates: [DataSourceTemplate!]
}

type Schema {
file: File!
}

type DataSource {
kind: String!
name: String!
network: String
source: ContractSource!
mapping: ContractMapping!
}

type ContractSource {
address: String
abi: String!
startBlock: BigInt
}

type ContractMapping {
kind: String
apiVersion: String!
language: String!
file: File!
entities: [String!]!
abis: [ContractAbi!]!
blockHandlers: [BlockHandler!]
callHandlers: [CallHandler!]
eventHandlers: [ContractEventHandler!]
}

type ContractAbi {
name: String!
file: File!
}

type BlockHandler {
handler: String!
filter: BlockFilter
}

type BlockFilter {
kind: String!
}

type CallHandler {
function: String!
handler: String!
}

type ContractEventHandler {
event: String!
topic0: String
handler: String!
}

type Graft {
base: String!
block: BigInt!
}

type DataSourceTemplate {
kind: String!
name: String!
network: String
source: ContractSourceTemplate!
mapping: ContractMapping!
}

type ContractSourceTemplate {
abi: String!
}
24 changes: 17 additions & 7 deletions src/protocols/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ module.exports = class Protocol {
'aurora',
'aurora-testnet',
],
near: [
'near-mainnet',
'near-testnet'
],
near: ['near-mainnet', 'near-testnet'],
})
}

Expand Down Expand Up @@ -108,6 +105,15 @@ module.exports = class Protocol {
}
}

hasTemplates() {
switch (this.name) {
case 'ethereum':
return true
case 'near':
return false
}
}

getTypeGenerator(options) {
switch (this.name) {
case 'ethereum':
Expand All @@ -118,13 +124,17 @@ module.exports = class Protocol {
}

getTemplateCodeGen(template) {
if (!this.hasTemplates()) {
throw new Error(
`Template data sources with kind '${this.name}' are not supported yet`,
)
}

switch (this.name) {
case 'ethereum':
return new EthereumTemplateCodeGen(template)
default:
throw new Error(
`Template data sources with kind '${this.name}' are not supported yet`,
)
throw new Error(`Template data sources with kind '${this.name}' is unknown`)
}
}

Expand Down
57 changes: 57 additions & 0 deletions src/protocols/near/manifest.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Each referenced type's in any of the types below must be listed
# here either as `scalar` or `type` for the validation code to work
# properly.
#
# That's why `String` is listed as a scalar even though it's built-in
# GraphQL basic types.
scalar String
scalar File
scalar BigInt

type SubgraphManifest {
specVersion: String!
schema: Schema!
description: String
repository: String
graft: Graft
dataSources: [DataSource!]!
}

type Schema {
file: File!
}

type DataSource {
kind: String!
name: String!
network: String
source: ContractSource!
mapping: ContractMapping!
}

type ContractSource {
account: String
startBlock: BigInt
}

type ContractMapping {
apiVersion: String!
language: String!
file: File!
entities: [String!]!
blockHandlers: [BlockHandler!]
receiptHandlers: [ReceiptHandler!]
}

type BlockHandler {
handler: String!
}

type ReceiptHandler {
handler: String!
}

type Graft {
base: String!
block: BigInt!
}
31 changes: 18 additions & 13 deletions src/subgraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const buildCombinedWarning = (filename, warnings) =>
? warnings.reduce(
(msg, w) =>
`${msg}

Path: ${w.get('path').size === 0 ? '/' : w.get('path').join(' > ')}
${w
.get('message')
Expand All @@ -39,9 +39,21 @@ const buildCombinedWarning = (filename, warnings) =>

module.exports = class Subgraph {
static async validate(data, protocol, { resolveFile }) {
if (protocol.name == null) {
return immutable.fromJS([
{
path: [],
message: `Unable to determine for which protocol manifest file is built for. Ensure you have at least one 'dataSources' and/or 'templates' elements defined in your subgraph.`,
},
])
}

// Parse the default subgraph schema
let schema = graphql.parse(
await fs.readFile(path.join(__dirname, '..', 'manifest-schema.graphql'), 'utf-8'),
await fs.readFile(
path.join(__dirname, 'protocols', protocol.name, `manifest.graphql`),
'utf-8',
),
)

// Obtain the root `SubgraphManifest` type from the schema
Expand Down Expand Up @@ -146,10 +158,7 @@ At least one such handler must be defined.`,
}

static validateContractValues(manifest, protocol) {
return validation.validateContractValues(
manifest,
protocol,
)
return validation.validateContractValues(manifest, protocol)
}

// Validate that data source names are unique, so they don't overwrite each other.
Expand Down Expand Up @@ -200,17 +209,13 @@ More than one template named '${name}', template names must be unique.`,
return yaml.stringify(manifest.toJS())
}

static async load(
filename,
{ protocol, skipValidation } = { skipValidation: false }
) {
static async load(filename, { protocol, skipValidation } = { skipValidation: false }) {
// Load and validate the manifest
let data = null

if(filename.match(/.js$/)) {
if (filename.match(/.js$/)) {
data = require(path.resolve(filename))
}
else {
} else {
data = yaml.parse(await fs.readFile(filename, 'utf-8'))
}

Expand Down
Loading