Skip to content

Commit c301481

Browse files
Fix _id field clash (#1810)
* Add `_id` to the remap dictionary. * Simplify the remap logic since adding subgraph name to the field doesn't make much sense. * Replace `id` with `internal_id` is more logical and explicit * Add `id` field to tests
1 parent edb601d commit c301481

File tree

4 files changed

+34
-24
lines changed

4 files changed

+34
-24
lines changed

.changeset/short-keys-boil.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphprotocol/graph-cli': patch
3+
---
4+
5+
fix bug with clashing \_id field name in schema

packages/cli/src/scaffold/ethereum.test.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const TEST_EVENT = {
2626
],
2727
},
2828
{ name: 'd', type: 'string', indexed: true },
29+
{ name: 'id', type: 'string' },
2930
],
3031
};
3132

@@ -106,7 +107,7 @@ dataSources:
106107
- name: Contract
107108
file: ./abis/Contract.json
108109
eventHandlers:
109-
- event: ExampleEvent(indexed uint256,bytes[4],string,(uint256,bytes32,string,(uint96,string,bytes32)),indexed string)
110+
- event: ExampleEvent(indexed uint256,bytes[4],string,(uint256,bytes32,string,(uint96,string,bytes32)),indexed string,string)
110111
handler: handleExampleEvent
111112
- event: ExampleEvent(bytes32)
112113
handler: handleExampleEvent1
@@ -139,6 +140,7 @@ type ExampleEvent @entity(immutable: true) {
139140
c_c3_value1: String! # string
140141
c_c3_value2: Bytes! # bytes32
141142
d: String! # string
143+
internal_id: String! # string
142144
blockNumber: BigInt!
143145
blockTimestamp: BigInt!
144146
transactionHash: Bytes!
@@ -233,6 +235,7 @@ export function handleExampleEvent(event: ExampleEventEvent): void {
233235
entity.c_c3_value1 = event.params.c.c3.value1
234236
entity.c_c3_value2 = event.params.c.c3.value2
235237
entity.d = event.params.d
238+
entity.internal_id = event.params.id
236239
237240
entity.blockNumber = event.block.number
238241
entity.blockTimestamp = event.block.timestamp
@@ -285,7 +288,8 @@ describe("Describe entity assertions", () => {
285288
let param2 = "Example string value"
286289
let c = "ethereum.Tuple Not implemented"
287290
let d = "Example string value"
288-
let newExampleEventEvent = createExampleEventEvent(a, b, param2, c, d)
291+
let id = "Example string value"
292+
let newExampleEventEvent = createExampleEventEvent(a, b, param2, c, d, id)
289293
handleExampleEvent(newExampleEventEvent)
290294
})
291295
@@ -346,7 +350,8 @@ export function createExampleEventEvent(
346350
b: Array<Bytes>,
347351
param2: string,
348352
c: ethereum.Tuple,
349-
d: string
353+
d: string,
354+
id: string
350355
): ExampleEvent {
351356
let exampleEventEvent = changetype<ExampleEvent>(newMockEvent())
352357
@@ -367,6 +372,9 @@ export function createExampleEventEvent(
367372
exampleEventEvent.parameters.push(
368373
new ethereum.EventParam("d", ethereum.Value.fromString(d))
369374
)
375+
exampleEventEvent.parameters.push(
376+
new ethereum.EventParam("id", ethereum.Value.fromString(id))
377+
)
370378
371379
return exampleEventEvent
372380
}
@@ -415,7 +423,8 @@ describe("Describe entity assertions", () => {
415423
let param2 = "Example string value"
416424
let c = "ethereum.Tuple Not implemented"
417425
let d = "Example string value"
418-
let newExampleEventEvent = createExampleEventEvent(a, b, param2, c, d)
426+
let id = "Example string value"
427+
let newExampleEventEvent = createExampleEventEvent(a, b, param2, c, d, id)
419428
handleExampleEvent(newExampleEventEvent)
420429
})
421430
@@ -476,7 +485,8 @@ export function createExampleEventEvent(
476485
b: Array<Bytes>,
477486
param2: string,
478487
c: ethereum.Tuple,
479-
d: string
488+
d: string,
489+
id: string
480490
): ExampleEvent {
481491
let exampleEventEvent = changetype<ExampleEvent>(newMockEvent())
482492
@@ -497,6 +507,9 @@ export function createExampleEventEvent(
497507
exampleEventEvent.parameters.push(
498508
new ethereum.EventParam("d", ethereum.Value.fromString(d))
499509
)
510+
exampleEventEvent.parameters.push(
511+
new ethereum.EventParam("id", ethereum.Value.fromString(id))
512+
)
500513
501514
return exampleEventEvent
502515
}

packages/cli/src/scaffold/mapping.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,23 @@ export const generateFieldAssignments = ({ index, input }: { index: number; inpu
1313
[input.name || `param${index}`],
1414
);
1515

16-
type BlacklistDictionary = Record<string, string>;
17-
1816
/**
1917
* Map of input names that are reserved so we do not use them as field names to avoid conflicts
18+
* see https://github.com/graphprotocol/graph-tooling/issues/710
19+
* name => mappedName
2020
*/
21-
export const INPUT_NAMES_BLACKLIST = {
22-
/** Related to https://github.com/graphprotocol/graph-tooling/issues/710 */
23-
id: 'id',
21+
const NAMES_REMAP_DICTIONARY: Record<string, string> = {
22+
id: 'internal_id',
23+
_id: 'internal__id',
2424
} as const;
2525

26-
export const renameInput = (name: string, subgraphName: string) => {
27-
const inputMap: BlacklistDictionary = {
28-
[INPUT_NAMES_BLACKLIST.id]: `${subgraphName}_id`,
29-
};
30-
31-
return inputMap?.[name] ?? name;
26+
export const renameNameIfNeeded = (name: string) => {
27+
return NAMES_REMAP_DICTIONARY[name] ?? name;
3228
};
3329

34-
export const generateEventFieldAssignments = (event: any, contractName: string) =>
30+
export const generateEventFieldAssignments = (event: any, _contractName: string) =>
3531
event.inputs.reduce((acc: any[], input: any, index: number) => {
36-
if (Object.values(INPUT_NAMES_BLACKLIST).includes(input.name)) {
37-
input.mappedName = renameInput(input.name, contractName ?? 'contract');
38-
}
32+
input.mappedName = renameNameIfNeeded(input.name);
3933
return acc.concat(generateFieldAssignments({ input, index }));
4034
}, []);
4135

packages/cli/src/scaffold/schema.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import immutable from 'immutable';
22
import { ascTypeForProtocol, valueTypeForAsc } from '../codegen/types/index.js';
33
import * as util from '../codegen/util.js';
44
import Protocol from '../protocols/index.js';
5-
import { INPUT_NAMES_BLACKLIST, renameInput } from './mapping.js';
5+
import { renameNameIfNeeded } from './mapping.js';
66

77
export function abiEvents(abi: { data: immutable.Collection<any, any> }) {
88
return util.disambiguateNames({
@@ -73,9 +73,7 @@ export const generateEventType = (
7373
id: Bytes!
7474
${event.inputs
7575
.reduce((acc: any[], input: any, index: number) => {
76-
if (Object.values(INPUT_NAMES_BLACKLIST).includes(input.name)) {
77-
input.name = renameInput(input.name, contractName ?? 'contract');
78-
}
76+
input.name = renameNameIfNeeded(input.name);
7977
return acc.concat(generateEventFields({ input, index, protocolName }));
8078
}, [])
8179
.join('\n')}

0 commit comments

Comments
 (0)