Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cookbook-Enums #798

Merged
merged 9 commits into from
Nov 15, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
enums2
  • Loading branch information
idalithb committed Oct 17, 2024
commit 1ca3527fe971224c4fe0033ab0c1177facc30216
62 changes: 30 additions & 32 deletions website/pages/en/cookbook/enums.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
title: Optomize Subgraphs Using Enums
idalithb marked this conversation as resolved.
Show resolved Hide resolved
---

In this guide, you will learn how to effectively extract detailed information from transactions and marketplace interactions using Enums.
Learn how to effectively extract detailed information from transactions and marketplace interactions using Enums.
idalithb marked this conversation as resolved.
Show resolved Hide resolved

> Note: This guide uses the CryptoCoven NFT smart contract.

## What are Enums?

Enums, or enumeration types, are a specific type of scalar restricted to a defined set of allowable values. To learn more check out [GraphQL documentation](https://graphql.org/learn/schema/#enumeration-types).
Enums, or enumeration types, are a specific data type that allows you to define a set of allowed values. 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).
idalithb marked this conversation as resolved.
Show resolved Hide resolved

## Benefits of Using Enums
idalithb marked this conversation as resolved.
Show resolved Hide resolved

- **Clarity:** Enums provide meaningful names for values, making the data easier to understand.
- **Clarity:** Enums provide meaningful names for values, making data easier to understand.
- **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.
idalithb marked this conversation as resolved.
Show resolved Hide resolved

## Defining Enums

This guide defines enums for the various marketplaces where NFTs are traded. Here’s how you can define enums in your subgraph schema:
To define enums for the various marketplaces where NFTs are traded, use the following in your subgraph schema:

```gql
# Enum for Marketplaces that the CryptoCoven contract interacted with(likely a Trade/Mint)
Expand All @@ -39,9 +39,11 @@ enum Marketplace {
}
```

## Utilizing Enums
## Using Enums

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.
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.

### Example Function

Expand Down Expand Up @@ -82,15 +84,17 @@ export function getMarketplaceName(marketplace: Marketplace): string {

## Best Practices for Using Enums

- Consistent Naming: Use clear and descriptive names for enum values to enhance readability.
- Centralized Management: Maintain enums in a single file for easier updates and management.
- Documentation: Comment on enum definitions to provide context.
- **Consistent Naming:** Use clear, descriptive names for enum values to improve readability.
- **Centralized Management:** Keep enums in a single file for easier updates and management.
- **Documentation:** Add comments to enum definitions for better context.

## Testing & Sample Queries
## Sample Queries
idalithb marked this conversation as resolved.
Show resolved Hide resolved

### Query 1: Account With The Highest NFT Marketplace Interactions

This query is designed to find the account that has interacted with the most unique marketplaces and get detailed information about their marketplace interactions, total spent amount, and NFT transactions. It can be useful for analyzing an account's activity and involvement in the NFT marketplace ecosystem.
This query aims to find the account that has interacted with the most unique marketplaces and to obtain detailed information about their marketplace interactions, total spending amount, and NFT transactions.

This analysis can be valuable for assessing an account's activity and involvement in the NFT marketplace ecosystem.

```gql
{
Expand Down Expand Up @@ -119,7 +123,7 @@ This query is designed to find the account that has interacted with the most uni
}
```

### Returns
### Return

```gql
{
Expand Down Expand Up @@ -692,11 +696,11 @@ This query is designed to find the account that has interacted with the most uni
}
```

### Query 2: Accounts With The Most Unique Marketplaces Interacted With
### Query 2: Accounts That Engaged with the Most Unique Marketplaces

This query fetches the top 5 accounts with the most unique marketplaces interacted with. The marketplaces field retrieves a list of marketplace interactions for each account.
This query retrieves the top 5 accounts that have interacted with the most unique marketplaces. The marketplace field retrieves a list of interactions for each account.

```gql
```graphql
{
accounts(first: 5, orderBy: uniqueMarketplacesCount, orderDirection: desc) {
id
Expand All @@ -708,7 +712,7 @@ This query fetches the top 5 accounts with the most unique marketplaces interact
}
```

### Result 2
### Return

```gql
{
Expand Down Expand Up @@ -851,11 +855,13 @@ This query fetches the top 5 accounts with the most unique marketplaces interact
}
```

To analyze which marketplaces are the most active, you can aggregate the data for each marketplace by iterating through the accounts' marketplace interactions on the client-side (React and Apollo Client)
To analyze which marketplaces are the most active, you can aggregate the data for each marketplace by iterating through the accounts' marketplace interactions on the client-side (React and Apollo Client).
idalithb marked this conversation as resolved.
Show resolved Hide resolved

## GraphQL Query
## GraphQL Query: Obtain Marketplaces for Each Account Interaction

First, write a query to get the marketplaces each account has interacted with:
### Step 1: Write a Query

Write the following query to get the marketplaces each account has interacted with:

```gql
{
Expand All @@ -868,11 +874,13 @@ First, write a query to get the marketplaces each account has interacted with:
}
```

This will return all accounts with their respective marketplace interactions. Depending on the data volume, you may want to paginate or adjust the number of accounts.
This will return all accounts with their respective marketplace interactions.

> Note: Depending on the data volume, you may want to paginate or adjust the number of accounts.

## Step 2: Apollo Client Setup in React

Next, you’ll fetch the data in your React component using Apollo Client.
Fetch the data in your React component using Apollo Client.

```js
import React, { useEffect, useState } from 'react'
Expand Down Expand Up @@ -957,14 +965,4 @@ const MarketplaceAnalysis = () => {
export default MarketplaceAnalysis
```

## Frequently Asked Questions

- What are enums? Enums are a special data type that allow you to define a set of named values. They are particularly useful for categorizing data.

- Why should I use enums in my subgraph? Enums enhance clarity, validation, and maintainability of your data, making it easier to manage transactions.

## Conclusion

By implementing enums in your subgraph, you can improve data organization, clarity, and maintainability. This repository provides a foundational understanding of how to effectively use enums with the CryptoCoven contract.

For further reading, check out [The Graph's official documentation.](https://thegraph.com/docs/en/developing/creating-a-subgraph/#enums)
For additional information, check out this guide's [repo](https://github.com/chidubemokeke/Subgraph-Tutorial-Enums).
Loading