You cannot import enums into mappings from the generated schema. Instead, you are expected to use the string representation of the enum value to set an enum field of an entity, just as mentioned in the documentation. I can give an example to illustrate this:
Suppose we have the enum ProposalStatus
defined in "schema.graphql" as follows:
enum ProposalStatus {
Null
Submitted
Executed
Rejected
Passed
Pended
Expired
}
Where the enum is consumed by the type Proposal
in the same schema file as follows:
type Proposal @entity {
id: ID!
...
status: ProposalStatus!
}
When implementing the mapping, we would set the status field of the proposal entity as follows:
...
proposal.status = "Passed";
This is an example demonstrates how to set the status to ProposalStatus.Passed
.
Now you could do some typescript refactoring to make this look better, here's how I usually do it:
class ProposalStatus {
static Null: string = "Null";
static Submitted: string = "Submitted";
static Executed: string = "Executed";
static Rejected: string = "Rejected";
static Passed: string = "Passed";
static Pended: string = "Pended";
static Expired: string = "Expired";
}
That way, you eliminate the possibility of mistyping an enum value and you could rewrite that line in the mapping as follows:
...
proposal.status = ProposalStatus.Passed;
Also, you need to be aware that enums received as part of an emitted event are represented by their u32 index and not their string representation, since that is how enums are implemented in Solidity. So if ProposalStatus.Passed
was received as part of an Ethereum event, its value in the event params would be represented as 4
. Therefore, you would most likely also require a getter function of sort to convert enum indices into their string representation.