Skip to content

Commit 2050bf6

Browse files
incrypto320237h
andauthoredNov 29, 2024··
Support subgraph datasources (#1754)
* Add new datasource type for subgraph composition * Support ABI's in subgraph datasource manifest * Generate types for source subgraph schema * Dont generate store methods for source subgraph schema * Add graph init command for subgraph composition --------- Co-authored-by: Etienne Donneger <etienne@pinax.network>
1 parent 023bac6 commit 2050bf6

File tree

18 files changed

+467
-61
lines changed

18 files changed

+467
-61
lines changed
 

‎.changeset/quick-bats-teach.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@graphprotocol/graph-cli': minor
3+
'@graphprotocol/graph-ts': minor
4+
---
5+
6+
Add support for subgraph datasource and associated types.

‎packages/cli/src/codegen/schema.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const formatTS = async (code: string) =>
1010
await prettier.format(code, { parser: 'typescript', semi: false });
1111

1212
const createSchemaCodeGen = (schema: string) =>
13-
new SchemaCodeGenerator(new Schema('', schema, graphql.parse(schema)));
13+
new SchemaCodeGenerator(new Schema(schema, graphql.parse(schema), ''));
1414

1515
const testEntity = async (generatedTypes: any[], expectedEntity: any) => {
1616
const entity = generatedTypes.find(type => type.name === expectedEntity.name);

‎packages/cli/src/codegen/schema.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,14 @@ export default class SchemaCodeGenerator {
9797
];
9898
}
9999

100-
generateTypes(): Array<tsCodegen.Class> {
100+
generateTypes(generateStoreMethods = true): Array<tsCodegen.Class> {
101101
return this.schema.ast.definitions
102102
.map(def => {
103103
if (this._isEntityTypeDefinition(def)) {
104104
schemaCodeGeneratorDebug.extend('generateTypes')(
105105
`Generating entity type for ${def.name.value}`,
106106
);
107-
return this._generateEntityType(def);
107+
return this._generateEntityType(def, generateStoreMethods);
108108
}
109109
})
110110
.filter(Boolean) as Array<tsCodegen.Class>;
@@ -157,7 +157,7 @@ export default class SchemaCodeGenerator {
157157
return def.kind === 'InterfaceTypeDefinition';
158158
}
159159

160-
_generateEntityType(def: ObjectTypeDefinitionNode) {
160+
_generateEntityType(def: ObjectTypeDefinitionNode, generateStoreMethods = true) {
161161
const name = def.name.value;
162162
const klass = tsCodegen.klass(name, { export: true, extends: 'Entity' });
163163
const fields = def.fields;
@@ -166,8 +166,10 @@ export default class SchemaCodeGenerator {
166166
// Generate and add a constructor
167167
klass.addMethod(this._generateConstructor(name, fields));
168168

169-
// Generate and add save() and getById() methods
170-
this._generateStoreMethods(name, idField).forEach(method => klass.addMethod(method));
169+
if (generateStoreMethods) {
170+
// Generate and add save() and getById() methods
171+
this._generateStoreMethods(name, idField).forEach(method => klass.addMethod(method));
172+
}
171173

172174
// Generate and add entity field getters and setters
173175
def.fields

‎packages/cli/src/command-helpers/scaffold.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,27 @@ export const generateScaffold = async (
5555
{
5656
protocolInstance,
5757
abi,
58-
contract,
58+
source,
5959
network,
6060
subgraphName,
6161
indexEvents,
6262
contractName = 'Contract',
6363
startBlock,
6464
node,
6565
spkgPath,
66+
entities,
6667
}: {
6768
protocolInstance: Protocol;
6869
abi: ABI;
69-
contract: string;
70+
source: string;
7071
network: string;
7172
subgraphName: string;
7273
indexEvents: boolean;
7374
contractName?: string;
7475
startBlock?: string;
7576
node?: string;
7677
spkgPath?: string;
78+
entities?: string[];
7779
},
7880
spinner: Spinner,
7981
) => {
@@ -83,13 +85,14 @@ export const generateScaffold = async (
8385
protocol: protocolInstance,
8486
abi,
8587
indexEvents,
86-
contract,
88+
contract: source,
8789
network,
8890
contractName,
8991
startBlock,
9092
subgraphName,
9193
node,
9294
spkgPath,
95+
entities,
9396
});
9497

9598
return await scaffold.generate();

‎packages/cli/src/commands/codegen.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from 'path';
22
import { Args, Command, Flags } from '@oclif/core';
33
import * as DataSourcesExtractor from '../command-helpers/data-sources';
4+
import { DEFAULT_IPFS_URL } from '../command-helpers/ipfs';
45
import { assertGraphTsVersion, assertManifestApiVersion } from '../command-helpers/version';
56
import debug from '../debug';
67
import Protocol from '../protocols';
@@ -38,6 +39,11 @@ export default class CodegenCommand extends Command {
3839
summary: 'Generate Float Subgraph Uncrashable helper file.',
3940
char: 'u',
4041
}),
42+
ipfs: Flags.string({
43+
summary: 'IPFS node to use for fetching subgraph data.',
44+
char: 'i',
45+
default: DEFAULT_IPFS_URL,
46+
}),
4147
'uncrashable-config': Flags.file({
4248
summary: 'Directory for uncrashable config.',
4349
aliases: ['uc'],
@@ -54,6 +60,7 @@ export default class CodegenCommand extends Command {
5460
'output-dir': outputDir,
5561
'skip-migrations': skipMigrations,
5662
watch,
63+
ipfs,
5764
uncrashable,
5865
'uncrashable-config': uncrashableConfig,
5966
},
@@ -62,6 +69,7 @@ export default class CodegenCommand extends Command {
6269
codegenDebug('Initialized codegen manifest: %o', manifest);
6370

6471
let protocol;
72+
let subgraphSources;
6573
try {
6674
// Checks to make sure codegen doesn't run against
6775
// older subgraphs (both apiVersion and graph-ts version).
@@ -73,8 +81,10 @@ export default class CodegenCommand extends Command {
7381
await assertGraphTsVersion(path.dirname(manifest), '0.25.0');
7482

7583
const dataSourcesAndTemplates = await DataSourcesExtractor.fromFilePath(manifest);
76-
7784
protocol = Protocol.fromDataSources(dataSourcesAndTemplates);
85+
subgraphSources = dataSourcesAndTemplates
86+
.filter((ds: any) => ds.kind == 'subgraph')
87+
.map((ds: any) => ds.source.address);
7888
} catch (e) {
7989
this.error(e, { exit: 1 });
8090
}
@@ -85,7 +95,9 @@ export default class CodegenCommand extends Command {
8595
skipMigrations,
8696
protocol,
8797
uncrashable,
98+
subgraphSources,
8899
uncrashableConfig: uncrashableConfig || 'uncrashable-config.yaml',
100+
ipfsUrl: ipfs,
89101
});
90102

91103
// Watch working directory for file updates or additions, trigger

0 commit comments

Comments
 (0)
Please sign in to comment.