Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 6 additions & 2 deletions packages/cli/src/commands/codegen.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { gqltadaSpinner } from "@/commands/codegen/gqltada.spinner";
import { Command } from "@commander-js/extra-typings";
import { loadEnv } from "@settlemint/sdk-utils/environment";
import { intro, outro } from "@settlemint/sdk-utils/terminal";
import { cancel, intro, outro } from "@settlemint/sdk-utils/terminal";
import type { DotEnv } from "@settlemint/sdk-utils/validation";
import { bold, italic, underline } from "yoctocolors";

Expand All @@ -28,7 +28,11 @@ export function codegenCommand(): Command {

const env: DotEnv = await loadEnv();

await gqltadaSpinner(env);
try {
await gqltadaSpinner(env);
} catch (error) {
cancel((error as Error).message);
}

outro("Codegen complete");
})
Expand Down
98 changes: 93 additions & 5 deletions packages/cli/src/commands/codegen/gqltada.spinner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { generateSchema } from "@gql.tada/cli-utils";
import { readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { generateSchema, generateTurbo } from "@gql.tada/cli-utils";
import { projectRoot } from "@settlemint/sdk-utils/filesystem";
import type { DotEnv } from "@settlemint/sdk-utils/validation";

/**
Expand All @@ -16,6 +19,79 @@ import type { DotEnv } from "@settlemint/sdk-utils/validation";
* );
*/
export async function gqltadaSpinner(env: DotEnv) {
const tadaConfig = {
name: "@0no-co/graphqlsp",
schemas: [
{
name: "hasura",
schema: "hasura-schema.graphql",
tadaOutputLocation: "hasura-env.d.ts",
tadaTurboLocation: "hasura-cache.d.ts",
},
{
name: "thegraph",
schema: "thegraph-schema.graphql",
tadaOutputLocation: "thegraph-env.d.ts",
tadaTurboLocation: "thegraph-cache.d.ts",
},
{
name: "thegraph-fallback",
schema: "thegraph-fallback-schema.graphql",
tadaOutputLocation: "thegraph-fallback-env.d.ts",
tadaTurboLocation: "thegraph-fallback-cache.d.ts",
},
{
name: "portal",
schema: "portal-schema.graphql",
tadaOutputLocation: "portal-env.d.ts",
tadaTurboLocation: "portal-cache.d.ts",
},
],
};

const projectDir = await projectRoot();
const tsconfigFile = join(projectDir, "tsconfig.json");
const tsconfigContent = readFileSync(tsconfigFile, "utf8");
const tsconfig: {
compilerOptions: {
plugins?: {
name: string;
schemas: {
name: string;
schema: string;
tadaOutputLocation: string;
tadaTurboLocation: string;
}[];
}[];
};
} = JSON.parse(tsconfigContent);
// Find the existing @0no-co/graphqlsp plugin or create a new one
if (!tsconfig.compilerOptions.plugins) {
tsconfig.compilerOptions.plugins = [];
}
let graphqlspPlugin = (tsconfig.compilerOptions.plugins ?? []).find((plugin) => plugin.name === "@0no-co/graphqlsp");

if (!graphqlspPlugin) {
// If the plugin doesn't exist, create it and add it to the plugins array
graphqlspPlugin = { name: "@0no-co/graphqlsp", schemas: [] };
tsconfig.compilerOptions.plugins.push(graphqlspPlugin);
}

// Update or add the schemas defined in tadaConfig
for (const schema of tadaConfig.schemas) {
const existingSchemaIndex = graphqlspPlugin.schemas.findIndex((s) => s.name === schema.name);
if (existingSchemaIndex !== -1) {
// Update existing schema
graphqlspPlugin.schemas[existingSchemaIndex] = schema;
} else {
// Add new schema
graphqlspPlugin.schemas.push(schema);
}
}

// Write the updated tsconfig back to the file
writeFileSync(tsconfigFile, JSON.stringify(tsconfig, null, 2), "utf8");
Comment on lines +22 to +93
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 extracting the tsconfig update logic into a separate function for better readability.

This would improve code organization and make the main function easier to understand at a glance.

function updateTsConfig(tadaConfig: any, projectDir: string) {
  const tsconfigFile = join(projectDir, "tsconfig.json");
  const tsconfig = JSON.parse(readFileSync(tsconfigFile, "utf8"));

  tsconfig.compilerOptions.plugins = tsconfig.compilerOptions.plugins || [];
  let graphqlspPlugin = tsconfig.compilerOptions.plugins.find(p => p.name === "@0no-co/graphqlsp") || { name: "@0no-co/graphqlsp", schemas: [] };

  tadaConfig.schemas.forEach(schema => {
    const existingIndex = graphqlspPlugin.schemas.findIndex(s => s.name === schema.name);
    existingIndex !== -1 ? graphqlspPlugin.schemas[existingIndex] = schema : graphqlspPlugin.schemas.push(schema);
  });

  writeFileSync(tsconfigFile, JSON.stringify(tsconfig, null, 2), "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.


await gqltadaCodegen({
type: "HASURA",
env,
Expand All @@ -42,26 +118,32 @@ async function gqltadaCodegen(options: {
}) {
let gqlEndpoint: string | undefined = undefined;
let output: string;
let turboOutput: string;
let adminSecret: string | undefined = undefined;
const accessToken = options.env.SETTLEMINT_ACCESS_TOKEN;

switch (options.type) {
case "HASURA":
gqlEndpoint = options.env.SETTLEMINT_HASURA_ENDPOINT;
output = "hasura.schema.graphql";
output = "hasura-schema.graphql";
turboOutput = "hasura-cache.d.ts";
adminSecret = options.env.SETTLEMINT_HASURA_ADMIN_SECRET;
break;
case "PORTAL":
gqlEndpoint = options.env.SETTLEMINT_PORTAL_GRAPHQL_ENDPOINT;
output = "portal.schema.graphql";
output = "portal-schema.graphql";
turboOutput = "portal-cache.d.ts";
break;
case "THEGRAPH":
gqlEndpoint = options.env.SETTLEMINT_THEGRAPH_SUBGRAPH_ENDPOINT;
output = "thegraph.schema.graphql";
output = "thegraph-schema.graphql";
turboOutput = "thegraph-cache.d.ts";
break;
case "THEGRAPH_FALLBACK":
gqlEndpoint = options.env.SETTLEMINT_THEGRAPH_SUBGRAPH_ENDPOINT_FALLBACK;
output = "thegraph-fallback.schema.graphql";
output = "thegraph-fallback-schema.graphql";
turboOutput = "thegraph-fallback-cache.d.ts";
break;
}

if (!gqlEndpoint) {
Expand Down Expand Up @@ -107,6 +189,12 @@ async function gqltadaCodegen(options: {
tsconfig: undefined,
headers,
});

await generateTurbo({
failOnWarn: false,
output: turboOutput,
tsconfig: undefined,
});
} catch (error) {
if (options.allowToFail) {
// ignore
Expand Down