|
| 1 | +# Development Guide: Implementing a New Entity |
| 2 | + |
| 3 | +This guide explains how to implement a new entity in the Hypercerts API, from type definition to resolver implementation. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The Hypercerts API uses a modular architecture where each entity follows a consistent pattern: |
| 8 | + |
| 9 | +1. Type Definition |
| 10 | +2. Query Arguments |
| 11 | +3. Entity Service |
| 12 | +4. Resolver |
| 13 | + |
| 14 | +## Step-by-Step Implementation |
| 15 | + |
| 16 | +### 1. Define Entity Types |
| 17 | + |
| 18 | +Create a new file in `src/graphql/schemas/typeDefs/` for your entity types: |
| 19 | + |
| 20 | +```typescript |
| 21 | +// src/graphql/schemas/typeDefs/yourEntityTypeDefs.ts |
| 22 | +import { Field, ObjectType } from "type-graphql"; |
| 23 | +import { BaseEntity } from "./baseTypes.js"; |
| 24 | + |
| 25 | +@ObjectType() |
| 26 | +export class YourEntity extends BaseEntity { |
| 27 | + @Field(() => String) |
| 28 | + name: string; |
| 29 | + |
| 30 | + @Field(() => String, { nullable: true }) |
| 31 | + description?: string; |
| 32 | + |
| 33 | + // Add other fields as needed |
| 34 | +} |
| 35 | + |
| 36 | +@ObjectType() |
| 37 | +export class GetYourEntitiesResponse { |
| 38 | + @Field(() => [YourEntity]) |
| 39 | + data: YourEntity[]; |
| 40 | + |
| 41 | + @Field(() => Int) |
| 42 | + count: number; |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +### 2. Define Query Arguments |
| 47 | + |
| 48 | +Create a new file in `src/graphql/schemas/args/` for your query arguments: |
| 49 | + |
| 50 | +```typescript |
| 51 | +// src/graphql/schemas/args/yourEntityArgs.ts |
| 52 | +import { ArgsType } from "type-graphql"; |
| 53 | +import { createEntityArgs } from "../../../lib/graphql/createEntityArgs.js"; |
| 54 | +import { EntityTypeDefs } from "../typeDefs/typeDefs.js"; |
| 55 | + |
| 56 | +// Define your entity fields |
| 57 | +const fields = { |
| 58 | + name: "string", |
| 59 | + description: "string", |
| 60 | + // Add other fields as needed |
| 61 | +} as const; |
| 62 | + |
| 63 | +// Create query arguments |
| 64 | +export const { WhereInput, SortOptions } = createEntityArgs( |
| 65 | + "YourEntity" as EntityTypeDefs, |
| 66 | + fields, |
| 67 | +); |
| 68 | + |
| 69 | +@ArgsType() |
| 70 | +export class GetYourEntitiesArgs { |
| 71 | + first?: number; |
| 72 | + offset?: number; |
| 73 | + where?: typeof WhereInput; |
| 74 | + sortBy?: typeof SortOptions; |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +### 3. Create Entity Service |
| 79 | + |
| 80 | +Create a new file in `src/services/database/entities/` for your entity service: |
| 81 | + |
| 82 | +```typescript |
| 83 | +// src/services/database/entities/YourEntityService.ts |
| 84 | +import { injectable } from "tsyringe"; |
| 85 | +import { createEntityService } from "./EntityServiceFactory.js"; |
| 86 | +import { GetYourEntitiesArgs } from "../../../graphql/schemas/args/yourEntityArgs.js"; |
| 87 | +import { YourEntity } from "../../../graphql/schemas/typeDefs/yourEntityTypeDefs.js"; |
| 88 | + |
| 89 | +@injectable() |
| 90 | +export class YourEntityService { |
| 91 | + private service = createEntityService<YourEntity, GetYourEntitiesArgs>( |
| 92 | + "your_entity_table", |
| 93 | + { |
| 94 | + // Add any custom query modifiers if needed |
| 95 | + }, |
| 96 | + ); |
| 97 | + |
| 98 | + async getYourEntities(args: GetYourEntitiesArgs) { |
| 99 | + return this.service.getMany(args); |
| 100 | + } |
| 101 | + |
| 102 | + async getYourEntity(args: GetYourEntitiesArgs) { |
| 103 | + return this.service.getSingle(args); |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +### 4. Implement Resolver |
| 109 | + |
| 110 | +Create a new file in `src/graphql/schemas/resolvers/` for your resolver: |
| 111 | + |
| 112 | +```typescript |
| 113 | +// src/graphql/schemas/resolvers/yourEntityResolver.ts |
| 114 | +import { inject, injectable } from "tsyringe"; |
| 115 | +import { Args, Query, Resolver } from "type-graphql"; |
| 116 | +import { YourEntityService } from "../../../services/database/entities/YourEntityService.js"; |
| 117 | +import { GetYourEntitiesArgs } from "../args/yourEntityArgs.js"; |
| 118 | +import { |
| 119 | + GetYourEntitiesResponse, |
| 120 | + YourEntity, |
| 121 | +} from "../typeDefs/yourEntityTypeDefs.js"; |
| 122 | + |
| 123 | +@injectable() |
| 124 | +@Resolver(() => YourEntity) |
| 125 | +class YourEntityResolver { |
| 126 | + constructor( |
| 127 | + @inject(YourEntityService) |
| 128 | + private yourEntityService: YourEntityService, |
| 129 | + ) {} |
| 130 | + |
| 131 | + @Query(() => GetYourEntitiesResponse) |
| 132 | + async yourEntities(@Args() args: GetYourEntitiesArgs) { |
| 133 | + return this.yourEntityService.getYourEntities(args); |
| 134 | + } |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +### 5. Register the Resolver |
| 139 | + |
| 140 | +Add your resolver to the list of resolvers in `src/graphql/schemas/resolvers/index.ts`: |
| 141 | + |
| 142 | +```typescript |
| 143 | +export * from "./yourEntityResolver.js"; |
| 144 | +``` |
| 145 | + |
| 146 | +## Best Practices |
| 147 | + |
| 148 | +1. **Type Safety**: Always use TypeScript's type system to ensure type safety across your implementation. |
| 149 | +2. **Consistent Naming**: Follow the existing naming conventions in the codebase. |
| 150 | +3. **Error Handling**: Implement proper error handling in your service and resolver methods. |
| 151 | +4. **Testing**: Write unit tests for your new entity implementation. |
| 152 | +5. **Documentation**: Add JSDoc comments to document your types, methods, and classes. |
| 153 | + |
| 154 | +## Example Implementation |
| 155 | + |
| 156 | +For a complete example, you can look at the implementation of existing entities like `Contract`, `Metadata`, or `AttestationSchema` in the codebase. |
| 157 | + |
| 158 | +## Common Pitfalls |
| 159 | + |
| 160 | +1. **Type Registration**: Ensure all your types are properly registered in the GraphQL schema. |
| 161 | +2. **Dependency Injection**: Use the `@injectable()` and `@inject()` decorators correctly. |
| 162 | +3. **Query Arguments**: Make sure your query arguments match the expected structure. |
| 163 | +4. **Database Schema**: Ensure your database table matches the entity structure. |
| 164 | + |
| 165 | +## Testing Your Implementation |
| 166 | + |
| 167 | +1. Start the development server: `pnpm dev` |
| 168 | +2. Access the GraphQL playground at `http://localhost:4000/v2/graphql` |
| 169 | +3. Test your queries and mutations |
| 170 | +4. Run the test suite: `pnpm test` |
| 171 | + |
| 172 | +## Additional Resources |
| 173 | + |
| 174 | +- [TypeGraphQL Documentation](https://typegraphql.com/) |
| 175 | +- [Kysely Documentation](https://kysely.dev/docs/intro) |
| 176 | +- [Supabase Documentation](https://supabase.com/docs) |
0 commit comments