Skip to content

Commit 9f69d96

Browse files
committed
gnd: Remove schema editing from 'gnd add'
1 parent e5614d3 commit 9f69d96

1 file changed

Lines changed: 0 additions & 159 deletions

File tree

gnd/src/commands/add.rs

Lines changed: 0 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,6 @@ pub async fn run_add(opt: AddOpt) -> Result<()> {
113113
// Add ABI file
114114
add_abi_file(project_dir, &contract_name, &abi)?;
115115

116-
// Add schema entities (append to schema.graphql)
117-
add_schema_entities(project_dir, &events, opt.merge_entities)?;
118-
119116
// Add mapping file
120117
add_mapping_file(project_dir, &contract_name, &events)?;
121118

@@ -229,110 +226,6 @@ fn add_abi_file(project_dir: &Path, contract_name: &str, abi: &JsonValue) -> Res
229226
Ok(())
230227
}
231228

232-
/// Add entity types to schema.graphql.
233-
fn add_schema_entities(
234-
project_dir: &Path,
235-
events: &[EventInfo],
236-
merge_entities: bool,
237-
) -> Result<()> {
238-
let schema_file = project_dir.join("schema.graphql");
239-
240-
let existing_schema = if schema_file.exists() {
241-
fs::read_to_string(&schema_file).context("Failed to read schema.graphql")?
242-
} else {
243-
String::new()
244-
};
245-
246-
// Generate entity types for events
247-
let mut new_entities = String::new();
248-
for event in events {
249-
let entity_name = &event.name;
250-
251-
// Check if entity already exists
252-
let entity_pattern = format!("type {} @entity", entity_name);
253-
if existing_schema.contains(&entity_pattern) {
254-
if merge_entities {
255-
step(
256-
Step::Skip,
257-
&format!("Entity {} already exists (merging)", entity_name),
258-
);
259-
continue;
260-
} else {
261-
return Err(anyhow!(
262-
"Entity '{}' already exists in schema.graphql. Use --merge-entities to skip existing entities.",
263-
entity_name
264-
));
265-
}
266-
}
267-
268-
new_entities.push_str(&generate_event_entity(event));
269-
new_entities.push_str("\n\n");
270-
}
271-
272-
if new_entities.is_empty() {
273-
step(Step::Skip, "No new entities to add");
274-
return Ok(());
275-
}
276-
277-
// Append to schema
278-
let updated_schema = if existing_schema.is_empty() {
279-
new_entities.trim_end().to_string()
280-
} else {
281-
format!(
282-
"{}\n\n{}",
283-
existing_schema.trim_end(),
284-
new_entities.trim_end()
285-
)
286-
};
287-
288-
step(Step::Write, "Updating schema.graphql");
289-
fs::write(&schema_file, updated_schema).context("Failed to write schema.graphql")?;
290-
291-
Ok(())
292-
}
293-
294-
/// Generate an entity type for an event.
295-
fn generate_event_entity(event: &EventInfo) -> String {
296-
let mut fields = String::new();
297-
fields.push_str(" id: Bytes!\n");
298-
299-
for input in &event.inputs {
300-
let field_name = sanitize_field_name(&input.name);
301-
let graphql_type = solidity_to_graphql(&input.solidity_type);
302-
fields.push_str(&format!(" {}: {}!\n", field_name, graphql_type));
303-
}
304-
305-
fields.push_str(" blockNumber: BigInt!\n");
306-
fields.push_str(" blockTimestamp: BigInt!\n");
307-
fields.push_str(" transactionHash: Bytes!");
308-
309-
format!("type {} @entity {{\n{}\n}}", event.name, fields)
310-
}
311-
312-
/// Convert Solidity type to GraphQL type.
313-
fn solidity_to_graphql(solidity_type: &str) -> &'static str {
314-
if solidity_type.ends_with("[]") {
315-
let inner = solidity_type.strip_suffix("[]").unwrap();
316-
return match solidity_to_graphql(inner) {
317-
"Bytes" => "[Bytes!]",
318-
"BigInt" => "[BigInt!]",
319-
"String" => "[String!]",
320-
"Boolean" => "[Boolean!]",
321-
_ => "[Bytes!]",
322-
};
323-
}
324-
325-
match solidity_type {
326-
"address" => "Bytes",
327-
"bool" => "Boolean",
328-
"string" => "String",
329-
"bytes" => "Bytes",
330-
t if t.starts_with("bytes") => "Bytes",
331-
t if t.starts_with("uint") || t.starts_with("int") => "BigInt",
332-
_ => "Bytes",
333-
}
334-
}
335-
336229
/// Sanitize a field name for GraphQL.
337230
fn sanitize_field_name(name: &str) -> String {
338231
if name.is_empty() {
@@ -617,23 +510,6 @@ mod tests {
617510
.contains("Invalid contract address"));
618511
}
619512

620-
#[test]
621-
fn test_solidity_to_graphql() {
622-
assert_eq!(solidity_to_graphql("address"), "Bytes");
623-
assert_eq!(solidity_to_graphql("bool"), "Boolean");
624-
assert_eq!(solidity_to_graphql("string"), "String");
625-
assert_eq!(solidity_to_graphql("bytes"), "Bytes");
626-
assert_eq!(solidity_to_graphql("bytes32"), "Bytes");
627-
assert_eq!(solidity_to_graphql("uint256"), "BigInt");
628-
assert_eq!(solidity_to_graphql("int128"), "BigInt");
629-
assert_eq!(solidity_to_graphql("uint8"), "BigInt");
630-
// Array types
631-
assert_eq!(solidity_to_graphql("address[]"), "[Bytes!]");
632-
assert_eq!(solidity_to_graphql("uint256[]"), "[BigInt!]");
633-
assert_eq!(solidity_to_graphql("bool[]"), "[Boolean!]");
634-
assert_eq!(solidity_to_graphql("string[]"), "[String!]");
635-
}
636-
637513
#[test]
638514
fn test_sanitize_field_name() {
639515
assert_eq!(sanitize_field_name("owner"), "owner");
@@ -653,41 +529,6 @@ mod tests {
653529
assert_eq!("ERC20Token".to_kebab_case(), "erc20-token");
654530
}
655531

656-
#[test]
657-
fn test_generate_event_entity() {
658-
let event = EventInfo {
659-
name: "Transfer".to_string(),
660-
signature: "Transfer(address,address,uint256)".to_string(),
661-
inputs: vec![
662-
EventInput {
663-
name: "from".to_string(),
664-
solidity_type: "address".to_string(),
665-
indexed: true,
666-
},
667-
EventInput {
668-
name: "to".to_string(),
669-
solidity_type: "address".to_string(),
670-
indexed: true,
671-
},
672-
EventInput {
673-
name: "value".to_string(),
674-
solidity_type: "uint256".to_string(),
675-
indexed: false,
676-
},
677-
],
678-
};
679-
680-
let entity = generate_event_entity(&event);
681-
assert!(entity.contains("type Transfer @entity"));
682-
assert!(entity.contains("id: Bytes!"));
683-
assert!(entity.contains("from: Bytes!"));
684-
assert!(entity.contains("to: Bytes!"));
685-
assert!(entity.contains("value: BigInt!"));
686-
assert!(entity.contains("blockNumber: BigInt!"));
687-
assert!(entity.contains("blockTimestamp: BigInt!"));
688-
assert!(entity.contains("transactionHash: Bytes!"));
689-
}
690-
691532
#[test]
692533
fn test_generate_event_handler() {
693534
let event = EventInfo {

0 commit comments

Comments
 (0)