Skip to content

Commit 05e08f4

Browse files
committed
applying feedback
1 parent 519559b commit 05e08f4

File tree

1 file changed

+40
-11
lines changed

1 file changed

+40
-11
lines changed

website/pages/en/cookbook/enums.mdx

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
title: Categorize NFT Marketplaces Using Enums
33
---
44

5-
Learn how to effectively organize information from transactions and marketplace interactions using Enums.
6-
7-
> Note: This guide uses the CryptoCoven NFT smart contract.
5+
Use Enums to make your code cleaner and less error-prone. Here's a full example of using Enums on NFT marketplaces.
86

97
## What are Enums?
108

11-
Enums, or enumeration types, are a specific data type that allows you to define a set of allowed values.
9+
Enums, or enumeration types, are a specific data type that allows you to define a set of specific, allowed values.
10+
11+
### Example of Enums in Your Schema
1212

13-
### Enums Syntax
13+
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.
1414

15-
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.
15+
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.
1616

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

1919
```graphql
2020
enum TokenStatus {
@@ -24,7 +24,7 @@ enum TokenStatus {
2424
}
2525
```
2626

27-
This means that when you use the type `TokenStatus` in your schema, you expect it to be exactly one of `OriginalOwner`, `SecondOwner`, or `ThirdOwner`.
27+
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.
2828

2929
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).
3030

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

37-
## Defining Enums
37+
### Without Enums
38+
39+
If you choose to define the type as a string instead of using an Enum, your code might look like this:
40+
41+
```graphql
42+
type Token @entity {
43+
id: ID!
44+
tokenId: BigInt!
45+
owner: Bytes! # Owner of the token
46+
tokenStatus: String! # String field to track token status
47+
timestamp: BigInt!
48+
}
49+
```
50+
51+
In this schema, `TokenStatus` is a simple string with no specific, allowed values.
52+
53+
#### Why is this a problem?
54+
55+
- 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.
56+
- It's easy to make typos such as `Orgnalowner` instead of `OriginalOwner`, making the data and potential queries unreliable.
57+
58+
### With Enums
59+
60+
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.
61+
62+
Enums provide type safety, minimize typo risks, and ensure consistent and reliable results.
63+
64+
## Defining Enums for NFT Marketplaces
65+
66+
> Note: The following guide uses the CryptoCoven NFT smart contract.
3867

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

@@ -49,13 +78,13 @@ enum Marketplace {
4978
}
5079
```
5180

52-
## Using Enums
81+
## Using Enums for NFT Marketplaces
5382

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

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

58-
### Implementing a Function
87+
### Implementing a Function for NFT Marketplaces
5988

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

0 commit comments

Comments
 (0)