Subgraph documentation mentions enums but does not show how to use them in mappings.

I tried to import it from the generated schema, along with other type definitions, but compiler gives error:

TS2305: Module '"../generated/schema"' has no exported member 'Status'.

Status was the name of the enum.

How to use enums in mappings?

1 Answer 1


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 {

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.

  • Thanks for the answer. "You cannot import enums into mappings from the generated schema." So basically, we need to duplicate the enum declaration inside the mapping? Oh my. This was what I was trying to avoid. "just as mentioned in the documentation" can you please refer to the relevant section of the documentation in your answer?
    – ferit
    Dec 29, 2022 at 13:45
  • @ferit Basically, yes. You would define a data structure to contain enum values in your mapping. This is optional though, as you could simply just use the enum string values directly without defining a structure to contain them. The portion in documentation that describes it is the following: "Once the enum is defined in the schema, you can use the string representation of the enum value to set an enum field on an entity. For example, you can set the tokenStatus to SecondOwner by first defining your entity and subsequently setting the field with entity.tokenStatus = "SecondOwner"." Dec 29, 2022 at 23:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.