Skip to content

Commit 2068a88

Browse files
authored
feat: codegen the clients (#216)
## 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.
1 parent 9aaee85 commit 2068a88

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

packages/cli/src/commands/codegen/gqltada.spinner.ts

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { readFileSync, writeFileSync } from "node:fs";
1+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
22
import { join } from "node:path";
33
import { generateSchema } from "@gql.tada/cli-utils";
44
import { projectRoot } from "@settlemint/sdk-utils/filesystem";
@@ -96,48 +96,41 @@ export async function gqltadaSpinner(env: DotEnv) {
9696
env,
9797
});
9898
await gqltadaCodegen({
99-
type: "THEGRAPH",
99+
type: "THE_GRAPH",
100100
env,
101101
allowToFail: true,
102102
});
103103
await gqltadaCodegen({
104-
type: "THEGRAPH_FALLBACK",
104+
type: "THE_GRAPH_FALLBACK",
105105
env,
106106
});
107107
}
108108

109109
async function gqltadaCodegen(options: {
110-
type: "HASURA" | "PORTAL" | "THEGRAPH" | "THEGRAPH_FALLBACK";
110+
type: "HASURA" | "PORTAL" | "THE_GRAPH" | "THE_GRAPH_FALLBACK";
111111
env: DotEnv;
112112
allowToFail?: boolean;
113113
}) {
114+
const accessToken = options.env.SETTLEMINT_ACCESS_TOKEN;
115+
const templateName = options.type.toLowerCase().replace(/_(\w)/g, (_, c) => c.toUpperCase());
116+
const output = `${templateName}-schema.graphql`;
117+
114118
let gqlEndpoint: string | undefined = undefined;
115-
let output: string;
116-
let turboOutput: string;
117119
let adminSecret: string | undefined = undefined;
118-
const accessToken = options.env.SETTLEMINT_ACCESS_TOKEN;
119120

120121
switch (options.type) {
121122
case "HASURA":
122123
gqlEndpoint = options.env.SETTLEMINT_HASURA_ENDPOINT;
123-
output = "hasura-schema.graphql";
124-
turboOutput = "hasura-cache.d.ts";
125124
adminSecret = options.env.SETTLEMINT_HASURA_ADMIN_SECRET;
126125
break;
127126
case "PORTAL":
128127
gqlEndpoint = options.env.SETTLEMINT_PORTAL_GRAPHQL_ENDPOINT;
129-
output = "portal-schema.graphql";
130-
turboOutput = "portal-cache.d.ts";
131128
break;
132-
case "THEGRAPH":
129+
case "THE_GRAPH":
133130
gqlEndpoint = options.env.SETTLEMINT_THEGRAPH_SUBGRAPH_ENDPOINT;
134-
output = "thegraph-schema.graphql";
135-
turboOutput = "thegraph-cache.d.ts";
136131
break;
137-
case "THEGRAPH_FALLBACK":
132+
case "THE_GRAPH_FALLBACK":
138133
gqlEndpoint = options.env.SETTLEMINT_THEGRAPH_SUBGRAPH_ENDPOINT_FALLBACK;
139-
output = "thegraph-fallback-schema.graphql";
140-
turboOutput = "thegraph-fallback-cache.d.ts";
141134
break;
142135
}
143136

@@ -184,6 +177,33 @@ async function gqltadaCodegen(options: {
184177
tsconfig: undefined,
185178
headers,
186179
});
180+
181+
const clientTemplate = `
182+
import { createServer${templateName}Client } from "@settlemint/sdk-hasura";
183+
import type { introspection } from "../../${templateName}.d.ts;
184+
185+
export const { client: ${templateName}Client, graphql: ${templateName}Graphql } = createServer${templateName}Client<{
186+
introspection: introspection;
187+
disableMasking: true;
188+
scalars: {
189+
DateTime: Date;
190+
JSON: Record<string, unknown>;
191+
};
192+
}>({
193+
instance: process.env.SETTLEMINT_${options.type}_ENDPOINT!,
194+
accessToken: process.env.SETTLEMINT_ACCESS_TOKEN!,
195+
${options.type === "HASURA" ? "adminSecret: process.env.SETTLEMINT_HASURA_ADMIN_SECRET!," : ""}
196+
});
197+
`;
198+
199+
const projectDir = await projectRoot();
200+
const codegenDir = join(projectDir, "/lib/settlemint");
201+
mkdirSync(codegenDir, { recursive: true });
202+
const fileName = `${options.type.toLowerCase().replace(/_/g, "-")}.ts`;
203+
const filePath = join(codegenDir, fileName);
204+
if (!existsSync(filePath)) {
205+
writeFileSync(filePath, clientTemplate, "utf8");
206+
}
187207
} catch (error) {
188208
if (options.allowToFail) {
189209
// ignore

0 commit comments

Comments
 (0)