Skip to content
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

Support subgraph datasources #1754

Merged
merged 8 commits into from
Nov 29, 2024
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
6 changes: 6 additions & 0 deletions .changeset/quick-bats-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphprotocol/graph-cli': minor
'@graphprotocol/graph-ts': minor
---

Add support for subgraph datasource and associated types.
2 changes: 1 addition & 1 deletion packages/cli/src/codegen/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const formatTS = async (code: string) =>
await prettier.format(code, { parser: 'typescript', semi: false });

const createSchemaCodeGen = (schema: string) =>
new SchemaCodeGenerator(new Schema('', schema, graphql.parse(schema)));
new SchemaCodeGenerator(new Schema(schema, graphql.parse(schema), ''));

const testEntity = async (generatedTypes: any[], expectedEntity: any) => {
const entity = generatedTypes.find(type => type.name === expectedEntity.name);
Expand Down
12 changes: 7 additions & 5 deletions packages/cli/src/codegen/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ export default class SchemaCodeGenerator {
];
}

generateTypes(): Array<tsCodegen.Class> {
generateTypes(generateStoreMethods = true): Array<tsCodegen.Class> {
return this.schema.ast.definitions
.map(def => {
if (this._isEntityTypeDefinition(def)) {
schemaCodeGeneratorDebug.extend('generateTypes')(
`Generating entity type for ${def.name.value}`,
);
return this._generateEntityType(def);
return this._generateEntityType(def, generateStoreMethods);
}
})
.filter(Boolean) as Array<tsCodegen.Class>;
Expand Down Expand Up @@ -157,7 +157,7 @@ export default class SchemaCodeGenerator {
return def.kind === 'InterfaceTypeDefinition';
}

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

// Generate and add save() and getById() methods
this._generateStoreMethods(name, idField).forEach(method => klass.addMethod(method));
if (generateStoreMethods) {
// Generate and add save() and getById() methods
this._generateStoreMethods(name, idField).forEach(method => klass.addMethod(method));
}

// Generate and add entity field getters and setters
def.fields
Expand Down
9 changes: 6 additions & 3 deletions packages/cli/src/command-helpers/scaffold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,27 @@ export const generateScaffold = async (
{
protocolInstance,
abi,
contract,
source,
network,
subgraphName,
indexEvents,
contractName = 'Contract',
startBlock,
node,
spkgPath,
entities,
}: {
protocolInstance: Protocol;
abi: ABI;
contract: string;
source: string;
network: string;
subgraphName: string;
indexEvents: boolean;
contractName?: string;
startBlock?: string;
node?: string;
spkgPath?: string;
entities?: string[];
},
spinner: Spinner,
) => {
Expand All @@ -83,13 +85,14 @@ export const generateScaffold = async (
protocol: protocolInstance,
abi,
indexEvents,
contract,
contract: source,
network,
contractName,
startBlock,
subgraphName,
node,
spkgPath,
entities,
});

return await scaffold.generate();
Expand Down
14 changes: 13 additions & 1 deletion packages/cli/src/commands/codegen.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'path';
import { Args, Command, Flags } from '@oclif/core';
import * as DataSourcesExtractor from '../command-helpers/data-sources';
import { DEFAULT_IPFS_URL } from '../command-helpers/ipfs';
import { assertGraphTsVersion, assertManifestApiVersion } from '../command-helpers/version';
import debug from '../debug';
import Protocol from '../protocols';
Expand Down Expand Up @@ -38,6 +39,11 @@ export default class CodegenCommand extends Command {
summary: 'Generate Float Subgraph Uncrashable helper file.',
char: 'u',
}),
ipfs: Flags.string({
summary: 'IPFS node to use for fetching subgraph data.',
char: 'i',
default: DEFAULT_IPFS_URL,
}),
'uncrashable-config': Flags.file({
summary: 'Directory for uncrashable config.',
aliases: ['uc'],
Expand All @@ -54,6 +60,7 @@ export default class CodegenCommand extends Command {
'output-dir': outputDir,
'skip-migrations': skipMigrations,
watch,
ipfs,
uncrashable,
'uncrashable-config': uncrashableConfig,
},
Expand All @@ -62,6 +69,7 @@ export default class CodegenCommand extends Command {
codegenDebug('Initialized codegen manifest: %o', manifest);

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

const dataSourcesAndTemplates = await DataSourcesExtractor.fromFilePath(manifest);

protocol = Protocol.fromDataSources(dataSourcesAndTemplates);
subgraphSources = dataSourcesAndTemplates
.filter((ds: any) => ds.kind == 'subgraph')
.map((ds: any) => ds.source.address);
} catch (e) {
this.error(e, { exit: 1 });
}
Expand All @@ -85,7 +95,9 @@ export default class CodegenCommand extends Command {
skipMigrations,
protocol,
uncrashable,
subgraphSources,
uncrashableConfig: uncrashableConfig || 'uncrashable-config.yaml',
ipfsUrl: ipfs,
});

// Watch working directory for file updates or additions, trigger
Expand Down
Loading
Loading