Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/short-keys-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphprotocol/graph-cli': patch
---

fix bug with clashing \_id field name in schema
23 changes: 18 additions & 5 deletions packages/cli/src/scaffold/ethereum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const TEST_EVENT = {
],
},
{ name: 'd', type: 'string', indexed: true },
{ name: 'id', type: 'string' },
],
};

Expand Down Expand Up @@ -106,7 +107,7 @@ dataSources:
- name: Contract
file: ./abis/Contract.json
eventHandlers:
- event: ExampleEvent(indexed uint256,bytes[4],string,(uint256,bytes32,string,(uint96,string,bytes32)),indexed string)
- event: ExampleEvent(indexed uint256,bytes[4],string,(uint256,bytes32,string,(uint96,string,bytes32)),indexed string,string)
handler: handleExampleEvent
- event: ExampleEvent(bytes32)
handler: handleExampleEvent1
Expand Down Expand Up @@ -139,6 +140,7 @@ type ExampleEvent @entity(immutable: true) {
c_c3_value1: String! # string
c_c3_value2: Bytes! # bytes32
d: String! # string
internal_id: String! # string
blockNumber: BigInt!
blockTimestamp: BigInt!
transactionHash: Bytes!
Expand Down Expand Up @@ -233,6 +235,7 @@ export function handleExampleEvent(event: ExampleEventEvent): void {
entity.c_c3_value1 = event.params.c.c3.value1
entity.c_c3_value2 = event.params.c.c3.value2
entity.d = event.params.d
entity.internal_id = event.params.id

entity.blockNumber = event.block.number
entity.blockTimestamp = event.block.timestamp
Expand Down Expand Up @@ -285,7 +288,8 @@ describe("Describe entity assertions", () => {
let param2 = "Example string value"
let c = "ethereum.Tuple Not implemented"
let d = "Example string value"
let newExampleEventEvent = createExampleEventEvent(a, b, param2, c, d)
let id = "Example string value"
let newExampleEventEvent = createExampleEventEvent(a, b, param2, c, d, id)
handleExampleEvent(newExampleEventEvent)
})

Expand Down Expand Up @@ -346,7 +350,8 @@ export function createExampleEventEvent(
b: Array<Bytes>,
param2: string,
c: ethereum.Tuple,
d: string
d: string,
id: string
): ExampleEvent {
let exampleEventEvent = changetype<ExampleEvent>(newMockEvent())

Expand All @@ -367,6 +372,9 @@ export function createExampleEventEvent(
exampleEventEvent.parameters.push(
new ethereum.EventParam("d", ethereum.Value.fromString(d))
)
exampleEventEvent.parameters.push(
new ethereum.EventParam("id", ethereum.Value.fromString(id))
)

return exampleEventEvent
}
Expand Down Expand Up @@ -415,7 +423,8 @@ describe("Describe entity assertions", () => {
let param2 = "Example string value"
let c = "ethereum.Tuple Not implemented"
let d = "Example string value"
let newExampleEventEvent = createExampleEventEvent(a, b, param2, c, d)
let id = "Example string value"
let newExampleEventEvent = createExampleEventEvent(a, b, param2, c, d, id)
handleExampleEvent(newExampleEventEvent)
})

Expand Down Expand Up @@ -476,7 +485,8 @@ export function createExampleEventEvent(
b: Array<Bytes>,
param2: string,
c: ethereum.Tuple,
d: string
d: string,
id: string
): ExampleEvent {
let exampleEventEvent = changetype<ExampleEvent>(newMockEvent())

Expand All @@ -497,6 +507,9 @@ export function createExampleEventEvent(
exampleEventEvent.parameters.push(
new ethereum.EventParam("d", ethereum.Value.fromString(d))
)
exampleEventEvent.parameters.push(
new ethereum.EventParam("id", ethereum.Value.fromString(id))
)

return exampleEventEvent
}
Expand Down
24 changes: 9 additions & 15 deletions packages/cli/src/scaffold/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,23 @@ export const generateFieldAssignments = ({ index, input }: { index: number; inpu
[input.name || `param${index}`],
);

type BlacklistDictionary = Record<string, string>;

/**
* Map of input names that are reserved so we do not use them as field names to avoid conflicts
* see https://github.com/graphprotocol/graph-tooling/issues/710
* name => mappedName
*/
export const INPUT_NAMES_BLACKLIST = {
/** Related to https://github.com/graphprotocol/graph-tooling/issues/710 */
id: 'id',
const NAMES_REMAP_DICTIONARY: Record<string, string> = {
id: 'internal_id',
_id: 'internal__id',
} as const;

export const renameInput = (name: string, subgraphName: string) => {
const inputMap: BlacklistDictionary = {
[INPUT_NAMES_BLACKLIST.id]: `${subgraphName}_id`,
};

return inputMap?.[name] ?? name;
export const renameNameIfNeeded = (name: string) => {
return NAMES_REMAP_DICTIONARY[name] ?? name;
};

export const generateEventFieldAssignments = (event: any, contractName: string) =>
export const generateEventFieldAssignments = (event: any, _contractName: string) =>
event.inputs.reduce((acc: any[], input: any, index: number) => {
if (Object.values(INPUT_NAMES_BLACKLIST).includes(input.name)) {
input.mappedName = renameInput(input.name, contractName ?? 'contract');
}
input.mappedName = renameNameIfNeeded(input.name);
return acc.concat(generateFieldAssignments({ input, index }));
}, []);

Expand Down
6 changes: 2 additions & 4 deletions packages/cli/src/scaffold/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import immutable from 'immutable';
import { ascTypeForProtocol, valueTypeForAsc } from '../codegen/types/index.js';
import * as util from '../codegen/util.js';
import Protocol from '../protocols/index.js';
import { INPUT_NAMES_BLACKLIST, renameInput } from './mapping.js';
import { renameNameIfNeeded } from './mapping.js';

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