Skip to content

Commit

Permalink
applying feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
idalithb committed Nov 13, 2024
1 parent 519559b commit 05e08f4
Showing 1 changed file with 40 additions and 11 deletions.
51 changes: 40 additions & 11 deletions website/pages/en/cookbook/enums.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
title: Categorize NFT Marketplaces Using Enums
---

Learn how to effectively organize information from transactions and marketplace interactions using Enums.

> Note: This guide uses the CryptoCoven NFT smart contract.
Use Enums to make your code cleaner and less error-prone. Here's a full example of using Enums on NFT marketplaces.

## What are Enums?

Enums, or enumeration types, are a specific data type that allows you to define a set of allowed values.
Enums, or enumeration types, are a specific data type that allows you to define a set of specific, allowed values.

### Example of Enums in Your Schema

### Enums Syntax
If you're building a subgraph to track the ownership history of tokens on a marketplace, each token might go through different ownerships, such as `OriginalOwner`, `SecondOwner`, and `ThirdOwner`. By using enums, you can define these specific ownerships, ensuring only predefined values are assigned.

You can define enums in your schema, and once defined, you can use the string representation of the enum value to set an enum field on an entity.
You can define enums in your schema, and once defined, you can use the string representation of the enum values to set an enum field on an entity.

Here's what an enum definition might look like in your schema:
Here's what an enum definition might look like in your schema, based on the example above:

```graphql
enum TokenStatus {
Expand All @@ -24,7 +24,7 @@ enum TokenStatus {
}
```

This means that when you use the type `TokenStatus` in your schema, you expect it to be exactly one of `OriginalOwner`, `SecondOwner`, or `ThirdOwner`.
This means that when you use the `TokenStatus` type in your schema, you expect it to be exactly one of predefined values: `OriginalOwner`, `SecondOwner`, or `ThirdOwner`, ensuring consistency and validity.

To learn more about enums, check out [Creating a Subgraph](/developing/creating-a-subgraph/#enums) and [GraphQL documentation](https://graphql.org/learn/schema/#enumeration-types).

Expand All @@ -34,7 +34,36 @@ To learn more about enums, check out [Creating a Subgraph](/developing/creating-
- **Validation:** Enums enforce strict value definitions, preventing invalid data entries.
- **Maintainability:** When you need to change or add new categories, enums allow you to do this in a focused manner.

## Defining Enums
### Without Enums

If you choose to define the type as a string instead of using an Enum, your code might look like this:

```graphql
type Token @entity {
id: ID!
tokenId: BigInt!
owner: Bytes! # Owner of the token
tokenStatus: String! # String field to track token status
timestamp: BigInt!
}
```

In this schema, `TokenStatus` is a simple string with no specific, allowed values.

#### Why is this a problem?

- There's no restriction of `TokenStatus` values, so any string can be accidentally assigned. This makes it hard to ensure that only valid statuses like `OriginalOwner`, `SecondOwner`, or `ThirdOwner` are set.
- It's easy to make typos such as `Orgnalowner` instead of `OriginalOwner`, making the data and potential queries unreliable.

### With Enums

Instead of assigning free-form strings, you can define an enum for `TokenStatus` with specific values: `OriginalOwner`, `SecondOwner`, or `ThirdOwner`. Using an enum ensures only allowed values are used.

Enums provide type safety, minimize typo risks, and ensure consistent and reliable results.

## Defining Enums for NFT Marketplaces

> Note: The following guide uses the CryptoCoven NFT smart contract.

To define enums for the various marketplaces where NFTs are traded, use the following in your subgraph schema:

Expand All @@ -49,13 +78,13 @@ enum Marketplace {
}
```

## Using Enums
## Using Enums for NFT Marketplaces

Once defined, enums can be used throughout your subgraph to categorize transactions or events.

For example, when logging NFT sales, you can specify the marketplace involved in the trade using the enum.

### Implementing a Function
### Implementing a Function for NFT Marketplaces

Here's how you can implement a function to retrieve the marketplace name from the enum as a string:

Expand Down

0 comments on commit 05e08f4

Please sign in to comment.