-
Notifications
You must be signed in to change notification settings - Fork 2
feat: codegen the clients #216
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
Conversation
Reviewer's Guide by SourceryThis pull request implements code generation for GraphQL clients, refactoring the existing code to support multiple client types and introducing a new client template generation feature. Sequence diagram for gqltadaCodegen client template generationsequenceDiagram
participant Env as DotEnv
participant Codegen as gqltadaCodegen
participant FS as FileSystem
participant Project as ProjectRoot
Env->>Codegen: Provide environment variables
Codegen->>Codegen: Determine gqlEndpoint and templateName
Codegen->>Project: Get project root directory
Project-->>Codegen: Return project directory
Codegen->>FS: Create codegen directory
Codegen->>FS: Check if client template file exists
alt File does not exist
Codegen->>FS: Write client template file
end
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
📦 Packages
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @roderik - I've reviewed your changes - here's some feedback:
Overall Comments:
- Consider logging errors even when allowToFail is true, to aid in debugging.
Here's what I looked at during the review
- 🟢 General issues: all looks good
- 🟢 Security: all looks good
- 🟡 Review instructions: 3 issues found
- 🟢 Testing: all looks good
- 🟢 Complexity: all looks good
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| env, | ||
| }); | ||
| await gqltadaCodegen({ | ||
| type: "THEGRAPH", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (review_instructions): Consider using an enum for type values
Using an enum for the type values would improve type safety and make the code more maintainable. It would also prevent typos and ensure consistency across the codebase.
enum CodegenType {
THE_GRAPH = "THE_GRAPH",
// Add other types as needed
}
// ...
await gqltadaCodegen({
type: CodegenType.THE_GRAPH,
env,
allowToFail: true,
Review instructions:
Path patterns: **/*.ts
Instructions:
- You always use the latest version of NodeJS, Bun, React, NestJS and NextJS, and you are familiar with the latest features and best practices.
- Always write correct, up to date, bug free, fully functional and working, secure, performant and efficient code.
- Focus on readability over being performant, but performance is important.
| import { createServer${templateName}Client } from "@settlemint/sdk-hasura"; | ||
| import type { introspection } from "../../${templateName}.d.ts; | ||
| export const { client: ${templateName}Client, graphql: ${templateName}Graphql } = createServer${templateName}Client<{ | ||
| introspection: introspection; | ||
| disableMasking: true; | ||
| scalars: { | ||
| DateTime: Date; | ||
| JSON: Record<string, unknown>; | ||
| }; | ||
| }>({ | ||
| instance: process.env.SETTLEMINT_${options.type}_ENDPOINT!, | ||
| accessToken: process.env.SETTLEMINT_ACCESS_TOKEN!, | ||
| ${options.type === "HASURA" ? "adminSecret: process.env.SETTLEMINT_HASURA_ADMIN_SECRET!," : ""} | ||
| }); | ||
| `; | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (review_instructions): Consider moving the template string to a separate file
Large template strings can make the code harder to read. Moving this to a separate file (e.g., a .ts or .ejs file) could improve maintainability and separation of concerns.
import { readFileSync } from 'fs';
import { join } from 'path';
const templatePath = join(__dirname, '..', '..', 'templates', 'clientTemplate.ts');
const clientTemplate = readFileSync(templatePath, 'utf-8');
const renderedTemplate = clientTemplate
.replace(/\${templateName}/g, templateName)
.replace(/\${options.type}/g, options.type);
Review instructions:
Path patterns: **/*.ts
Instructions:
- You always use the latest version of NodeJS, Bun, React, NestJS and NextJS, and you are familiar with the latest features and best practices.
- Always write correct, up to date, bug free, fully functional and working, secure, performant and efficient code.
- Focus on readability over being performant, but performance is important.
| writeFileSync(filePath, clientTemplate, "utf8"); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (review_instructions): Consider using async file operations
Using synchronous file operations can block the event loop. Consider using the async versions (e.g., fs.promises.writeFile) for better performance, especially in a CLI tool that might be used in various environments.
const { access, writeFile } = require('fs').promises;
try {
await access(filePath);
} catch {
await writeFile(filePath, clientTemplate, "utf8");
}
Review instructions:
Path patterns: **/*.ts
Instructions:
- You always use the latest version of NodeJS, Bun, React, NestJS and NextJS, and you are familiar with the latest features and best practices.
- Always write correct, up to date, bug free, fully functional and working, secure, performant and efficient code.
- Focus on readability over being performant, but performance is important.
Summary by Sourcery
Implement code generation for GraphQL client files, enhancing the naming convention for GraphQL types and ensuring client files are created for different environments with appropriate configurations.
New Features:
Enhancements: