Skip to content
This repository has been archived by the owner on Oct 25, 2024. It is now read-only.

enhancement: generate GraphQL schema from JSON ABI #1396

Closed
wants to merge 16 commits into from

Conversation

lostman
Copy link
Contributor

@lostman lostman commented Oct 5, 2023

Description

Closes #846.

This PR adds a --json-abi flag to forc index new which facilitates automatic generation of the GraphQL schema from the given JSON ABI.

Testing steps

Create a new indexer:

cargo run -p forc-index -- new --json-abi ./packages/fuel-indexer-tests/trybuild/abi/DAO-contract-abi.json dao-indexer

Copy the JSON ABI:

cp ./packages/fuel-indexer-tests/trybuild/abi/DAO-contract-abi.json /tmp/dao-indexer

Set the abi in the Manifest

abi: DAO-contract-abi.json

Change lib.rs to refer to types from the generated schema:

extern crate alloc;
use fuel_indexer_utils::prelude::*;

#[indexer(manifest = "dao_indexer.manifest.yaml")]
pub mod dao_indexer_index_mod {

    fn dao_indexer_handler(ie: InitializeEvent) {
        info!("author: {:?}", ie.author);
        let iee = InitializeEventEntity::new(ie.author, ie.token);
        iee.get_or_create();
    }
}

Verify that it builds:

forc index build

Changelog

  • Add generate_schema(), which generates a GraphQL schema from JSON ABI.
  • Add documentation.
  • Move some functions from packages/fuel-indexer-macros/src/helpers.rs to packages/fuel-indexer-lib/src/helpers.rs because they are now used in both places.

@lostman lostman force-pushed the maciej/auto-generate-schema branch from 7eb0ea3 to d052e53 Compare October 5, 2023 11:06
@lostman lostman force-pushed the maciej/auto-generate-schema branch from 142ae7e to 9e46ac2 Compare October 6, 2023 11:38
@lostman lostman self-assigned this Oct 12, 2023
@lostman lostman force-pushed the maciej/auto-generate-schema branch from 294d8a9 to d20f959 Compare October 18, 2023 14:07
@lostman lostman marked this pull request as ready for review October 18, 2023 18:36
Copy link
Contributor

@ra0x3 ra0x3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lostman

  • Left some feedback
  • Mainly about restructuring this a bit, and using fuel_indexer_macro_utils

@@ -21,6 +21,7 @@
- [Scalars](./designing-a-schema/scalars.md)
- [Directives](./designing-a-schema/directives.md)
- [Relationships](./designing-a-schema/relationships.md)
- [Generating a Schema](./generating-a-schema/index.md)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should go int the Designing a schema section maybe under a section called Automatic Schema Generation (or something that makes it clear what this section is about).

@@ -0,0 +1,155 @@
# Automatically generating GraphQL schema from JSON ABI

`forc index new` supports automatically generating GraphQL schema from a contract JSON ABI.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I think this has to be re-written.
  • @lostman We spent weeks on the docs, combing through every single line making sure they read properly and that all examples are informative and work
    • It sucked, it wasn't fun, but the docs are so much better now
    • Let's keep this trend going with the docs in this PR.
    • Ideally the docs should:
      1. Show me the tiniest Sway smart contract with various simple types (struct, enum, etc)
      2. Show me how to use forc index new --json-abi
      3. Show me the resultant schema that gets produced from that command ☝🏼
    • I think we have an example of this in the docs already here (again, I want to be very clear that I realize this is a PITA 😅 )

packages/fuel-indexer-lib/src/graphql/schema_gen.rs Outdated Show resolved Hide resolved
}

/// Whether a `TypeDeclaration` is a unit type
pub fn is_unit_type(typ: &TypeDeclaration) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lostman

  • Ah ok, yea, as I mentioned in the "Is there any particular reason..." comment...
    • All of this codegen logic needs to be moved to fuel-indexer-macros (we're duplicating a ton of logic here)

So maybe something like:

  • fuel_indexer_macros::schema_gen
    • This is where you put all the logic related to auto-creating this schema
      • And you would just pull in fuel_indexer_macros::helpers instead of duplicating this stuff
      • We already have a fuel_indexer_macro_utils crate
        • This was previously being used for indexer-internal proc macros (as opposed to public macros)
        • You could potentially move that #[metrics] macro out of fuel_indexer_macro_utils and into fuel_indexer_macros (so now fuel_indexer_macros would export #[metrics] and #[indexer]
        • Then that would allow you to put your macro logic in fuel_indexer_macro_utils (after you make it a non-proc-macro crate)
        • Finally, this would allow you to import the macro functions into forc_index::new via use fuel_indexer_macro_utils::schema_gen::*
          • I think this solves a lot of problems but let me know what you think

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is that fuel-indexer-macros is a proc-macro crate and can only export macros.

So, fuel_indexer_macros::schema_gen doesn't work.

we're duplicating a ton of logic here

Not duplicating. I moved the code from fuel_indexer_macros::helpers.

Finally, this would allow you to import the macro functions into forc_index::new via use fuel_indexer_macro_utils::schema_gen::*

This is fine. I can move this logic there and make it non-proc-macro.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lostman I mentioned in my previous comment

Then that would allow you to put your macro logic in fuel_indexer_macro_utils (after you make it a non-proc-macro crate)

@@ -13,6 +13,7 @@ ARGS:
OPTIONS:
--absolute-paths Resolve indexer asset filepaths using absolute paths.
-h, --help Print help information
--json-abi <JSON_ABI> Path to JSON ABI for automatic schema generation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌🏼

@lostman lostman force-pushed the maciej/auto-generate-schema branch 3 times, most recently from dd2806e to 4012e5e Compare October 27, 2023 14:34
@lostman lostman force-pushed the maciej/auto-generate-schema branch from 4012e5e to 7bea7ec Compare October 27, 2023 14:37
@deekerno deekerno closed this Feb 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Auto-generate GraphQL schema using contract ABI
3 participants