This repository has been archived by the owner on Oct 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
enhancement: generate GraphQL schema from JSON ABI #1396
Closed
Closed
Changes from 9 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d052e53
generate schema from json abi
lostman 9e46ac2
clippy
lostman 72976db
update test output
lostman e02d0d3
Merge branch 'develop' into maciej/auto-generate-schema
lostman 67ec272
document forc index new --json-abi flag
lostman 5d43dcb
update the docs
lostman d20f959
tweaks
lostman a866090
cleanup
lostman c19af88
Merge remote-tracking branch 'origin/develop' into maciej/auto-genera…
lostman 813b67a
move metrics macro to fuel-indexer-macros
lostman 226b163
comment
lostman efaea47
add fuel-indexer-metrics-macros crate
lostman 1f59c21
Merge remote-tracking branch 'origin/develop' into maciej/auto-genera…
lostman 7bea7ec
move code
lostman fbd9522
fix imports
lostman 42cbc8b
add missing file
lostman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
# Automatically generating GraphQL schema from JSON ABI | ||
|
||
`forc index new` supports automatically generating GraphQL schema from a contract JSON ABI. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
Sway `struct`s are translated into GrapQL `type`s, and the following `struct` field types are supported: | ||
|
||
| Sway Type | GraphQL Type | | ||
|-----------|--------------| | ||
| u128 | U128 | | ||
| u64 | U64 | | ||
| u32 | U32 | | ||
| u8 | U8 | | ||
| i128 | I128 | | ||
| i64 | I64 | | ||
| i32 | I32 | | ||
| i8 | I8 | | ||
| bool | Boolean | | ||
| u8[64] | Bytes64 | | ||
| u8[32] | Bytes32 | | ||
| u8[8] | Bytes8 | | ||
| u8[4] | Bytes4 | | ||
| Vec<u8>| Bytes | | ||
| SizedAsciiString<64> | ID | | ||
| String | String | | ||
| str[32] | Bytes32 | | ||
| str[64] | Bytes64 | | ||
|
||
Sway `enum` types can also be translated. However, all enum variants must have `()` type. For example: | ||
|
||
```rust | ||
pub enum SimpleEnum { | ||
One: (), | ||
Two: (), | ||
Three: (), | ||
} | ||
``` | ||
|
||
Will be translated to GraphQL as: | ||
|
||
```GraphQL | ||
enum SimpleEnum { | ||
One | ||
Two | ||
Three | ||
} | ||
``` | ||
|
||
## Example | ||
|
||
Using the `DAO-contract-abi.json`, which can be found in the `fuel-indexer` repository: | ||
|
||
```bash | ||
forc index new --json-abi ./packages/fuel-indexer-tests/trybuild/abi/DAO-contract-abi.json dao-indexer | ||
``` | ||
|
||
We get the following schema: | ||
|
||
```GraphQL | ||
enum CreationError { | ||
DurationCannotBeZero | ||
InvalidAcceptancePercentage | ||
} | ||
|
||
enum InitializationError { | ||
CannotReinitialize | ||
ContractNotInitialized | ||
} | ||
|
||
enum ProposalError { | ||
InsufficientApprovals | ||
ProposalExecuted | ||
ProposalExpired | ||
ProposalStillActive | ||
} | ||
|
||
enum UserError { | ||
AmountCannotBeZero | ||
IncorrectAssetSent | ||
InsufficientBalance | ||
InvalidId | ||
VoteAmountCannotBeZero | ||
} | ||
|
||
type CallDataEntity @entity { | ||
id: ID! | ||
arguments: U64! | ||
function_selector: U64! | ||
} | ||
|
||
type CreateProposalEventEntity @entity { | ||
id: ID! | ||
proposal_info: ProposalInfoEntity! | ||
} | ||
|
||
type DepositEventEntity @entity { | ||
id: ID! | ||
amount: U64! | ||
user: Identity! | ||
} | ||
|
||
type ExecuteEventEntity @entity { | ||
id: ID! | ||
acceptance_percentage: U64! | ||
user: Identity! | ||
} | ||
|
||
type InitializeEventEntity @entity { | ||
id: ID! | ||
author: Identity! | ||
token: ContractId! | ||
} | ||
|
||
type ProposalEntity @entity { | ||
id: ID! | ||
amount: U64! | ||
asset: ContractId! | ||
call_data: CallDataEntity! | ||
gas: U64! | ||
} | ||
|
||
type ProposalInfoEntity @entity { | ||
id: ID! | ||
acceptance_percentage: U64! | ||
author: Identity! | ||
deadline: U64! | ||
executed: Boolean! | ||
no_votes: U64! | ||
proposal_transaction: ProposalEntity! | ||
yes_votes: U64! | ||
} | ||
|
||
type UnlockVotesEventEntity @entity { | ||
id: ID! | ||
user: Identity! | ||
vote_amount: U64! | ||
} | ||
|
||
type VoteEventEntity @entity { | ||
id: ID! | ||
user: Identity! | ||
vote_amount: U64! | ||
} | ||
|
||
type VotesEntity @entity { | ||
id: ID! | ||
no_votes: U64! | ||
yes_votes: U64! | ||
} | ||
|
||
type WithdrawEventEntity @entity { | ||
id: ID! | ||
amount: U64! | ||
user: Identity! | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub mod constants; | ||
pub mod parser; | ||
pub mod schema_gen; | ||
pub mod types; | ||
pub mod validator; | ||
|
||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 calledAutomatic Schema Generation
(or something that makes it clear what this section is about).