Skip to content

Conversation

@roderik
Copy link
Member

@roderik roderik commented Oct 7, 2024

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:

  • Introduce code generation for client files based on GraphQL schema types, creating server clients for different environments like HASURA, PORTAL, THE_GRAPH, and THE_GRAPH_FALLBACK.

Enhancements:

  • Refactor the code to use a more consistent naming convention for GraphQL types by replacing underscores with camel case in template names.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Oct 7, 2024

Reviewer's Guide by Sourcery

This 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 generation

sequenceDiagram
    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
Loading

File-Level Changes

Change Details Files
Refactored GraphQL client type naming convention
  • Changed 'THEGRAPH' to 'THE_GRAPH'
  • Changed 'THEGRAPH_FALLBACK' to 'THE_GRAPH_FALLBACK'
  • Updated switch cases to use new type names
packages/cli/src/commands/codegen/gqltada.spinner.ts
Simplified output file naming logic
  • Introduced templateName variable for consistent naming
  • Removed separate turboOutput variable
  • Generated output filename now uses templateName
packages/cli/src/commands/codegen/gqltada.spinner.ts
Added client template generation feature
  • Created a clientTemplate string with dynamic content
  • Added logic to create a new directory for generated clients
  • Implemented file writing for the new client template
packages/cli/src/commands/codegen/gqltada.spinner.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@github-actions github-actions bot added the feat New feature label Oct 7, 2024
@github-actions
Copy link

github-actions bot commented Oct 7, 2024

📦 Packages

Package Version
SDK Cli @settlemint/sdk-cli@0.5.4-pr64b6d1e
SDK The Graph @settlemint/sdk-thegraph@0.5.4-pr64b6d1e
SDK Portal @settlemint/sdk-portal@0.5.4-pr64b6d1e
SDK Hasura @settlemint/sdk-hasura@0.5.4-pr64b6d1e
SDK JS @settlemint/sdk-js@0.5.4-pr64b6d1e
SDK Utils @settlemint/sdk-utils@0.5.4-pr64b6d1e

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a 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",
Copy link
Contributor

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.

Comment on lines +181 to +197
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!," : ""}
});
`;

Copy link
Contributor

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.

Comment on lines +204 to +205
writeFileSync(filePath, clientTemplate, "utf8");
}
Copy link
Contributor

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.

@roderik roderik merged commit 2068a88 into main Oct 7, 2024
@roderik roderik deleted the feat/client-codegen branch October 7, 2024 12:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feat New feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants