Skip to content

Commit 70423ca

Browse files
committed
Generate only immutable entities when using graph init with a source subgraph
1 parent c9fe1c4 commit 70423ca

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-3
lines changed

packages/cli/src/commands/init.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,7 +1253,7 @@ async function initSubgraphFromContract(
12531253
}
12541254
}
12551255

1256-
let entities: string[] | undefined;
1256+
let immutableEntities: string[] | undefined;
12571257

12581258
if (isComposedSubgraph) {
12591259
try {
@@ -1274,7 +1274,7 @@ async function initSubgraphFromContract(
12741274
startBlock ||= getMinStartBlock(manifestYaml)?.toString();
12751275
const schemaString = await loadSubgraphSchemaFromIPFS(ipfsClient, source);
12761276
const schema = await Schema.loadFromString(schemaString);
1277-
entities = schema.getEntityNames();
1277+
immutableEntities = schema.getImmutableEntityNames();
12781278
} catch (e) {
12791279
this.error(`Failed to load and parse subgraph schema: ${e.message}`, { exit: 1 });
12801280
}
@@ -1316,7 +1316,7 @@ async function initSubgraphFromContract(
13161316
startBlock,
13171317
node,
13181318
spkgPath,
1319-
entities,
1319+
entities: immutableEntities,
13201320
},
13211321
spinner,
13221322
);

packages/cli/src/schema.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { describe, expect, test } from 'vitest';
2+
import Schema from './schema.js';
3+
4+
describe('Schema', () => {
5+
const schemaDocument = `
6+
type Entity1 @entity {
7+
id: ID!
8+
}
9+
10+
type Entity2 @entity(immutable: true) {
11+
id: ID!
12+
}
13+
14+
type Entity3 @entity(immutable: false) {
15+
id: ID!
16+
}
17+
`;
18+
19+
test('getEntityNames returns all entity types', async () => {
20+
const schema = await Schema.loadFromString(schemaDocument);
21+
const entityNames = schema.getEntityNames();
22+
expect(entityNames).toEqual(['Entity1', 'Entity2', 'Entity3']);
23+
});
24+
25+
test('getImmutableEntityNames returns only immutable entity types', async () => {
26+
const schema = await Schema.loadFromString(schemaDocument);
27+
const immutableEntityNames = schema.getImmutableEntityNames();
28+
expect(immutableEntityNames).toEqual(['Entity2']);
29+
});
30+
31+
test('getImmutableEntityNames handles entities without immutable flag', async () => {
32+
const schema = await Schema.loadFromString(schemaDocument);
33+
const immutableEntityNames = schema.getImmutableEntityNames();
34+
expect(immutableEntityNames).not.toContain('Entity1');
35+
});
36+
37+
test('getImmutableEntityNames handles explicitly non-immutable entities', async () => {
38+
const schema = await Schema.loadFromString(schemaDocument);
39+
const immutableEntityNames = schema.getImmutableEntityNames();
40+
expect(immutableEntityNames).not.toContain('Entity3');
41+
});
42+
43+
test('getImmutableEntityNames ignores non-entity types', async () => {
44+
const schema = await Schema.loadFromString(schemaDocument);
45+
const immutableEntityNames = schema.getImmutableEntityNames();
46+
expect(immutableEntityNames).not.toContain('Entity1');
47+
expect(immutableEntityNames).not.toContain('Entity3');
48+
});
49+
});

packages/cli/src/schema.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,24 @@ export default class Schema {
4242
)
4343
.map(entity => (entity as graphql.ObjectTypeDefinitionNode).name.value);
4444
}
45+
46+
getImmutableEntityNames(): string[] {
47+
return this.ast.definitions
48+
.filter(
49+
def =>
50+
def.kind === 'ObjectTypeDefinition' &&
51+
def.directives?.find(
52+
directive =>
53+
directive.name.value === 'entity' &&
54+
directive.arguments?.find(arg => {
55+
return (
56+
arg.name.value === 'immutable' &&
57+
arg.value.kind === 'BooleanValue' &&
58+
arg.value.value === true
59+
);
60+
}),
61+
) !== undefined,
62+
)
63+
.map(entity => (entity as graphql.ObjectTypeDefinitionNode).name.value);
64+
}
4565
}

0 commit comments

Comments
 (0)