|
1 | | -import { readFileSync, writeFileSync } from "node:fs"; |
| 1 | +import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; |
2 | 2 | import { join } from "node:path"; |
3 | 3 | import { generateSchema } from "@gql.tada/cli-utils"; |
4 | 4 | import { projectRoot } from "@settlemint/sdk-utils/filesystem"; |
@@ -96,48 +96,41 @@ export async function gqltadaSpinner(env: DotEnv) { |
96 | 96 | env, |
97 | 97 | }); |
98 | 98 | await gqltadaCodegen({ |
99 | | - type: "THEGRAPH", |
| 99 | + type: "THE_GRAPH", |
100 | 100 | env, |
101 | 101 | allowToFail: true, |
102 | 102 | }); |
103 | 103 | await gqltadaCodegen({ |
104 | | - type: "THEGRAPH_FALLBACK", |
| 104 | + type: "THE_GRAPH_FALLBACK", |
105 | 105 | env, |
106 | 106 | }); |
107 | 107 | } |
108 | 108 |
|
109 | 109 | async function gqltadaCodegen(options: { |
110 | | - type: "HASURA" | "PORTAL" | "THEGRAPH" | "THEGRAPH_FALLBACK"; |
| 110 | + type: "HASURA" | "PORTAL" | "THE_GRAPH" | "THE_GRAPH_FALLBACK"; |
111 | 111 | env: DotEnv; |
112 | 112 | allowToFail?: boolean; |
113 | 113 | }) { |
| 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 | + |
114 | 118 | let gqlEndpoint: string | undefined = undefined; |
115 | | - let output: string; |
116 | | - let turboOutput: string; |
117 | 119 | let adminSecret: string | undefined = undefined; |
118 | | - const accessToken = options.env.SETTLEMINT_ACCESS_TOKEN; |
119 | 120 |
|
120 | 121 | switch (options.type) { |
121 | 122 | case "HASURA": |
122 | 123 | gqlEndpoint = options.env.SETTLEMINT_HASURA_ENDPOINT; |
123 | | - output = "hasura-schema.graphql"; |
124 | | - turboOutput = "hasura-cache.d.ts"; |
125 | 124 | adminSecret = options.env.SETTLEMINT_HASURA_ADMIN_SECRET; |
126 | 125 | break; |
127 | 126 | case "PORTAL": |
128 | 127 | gqlEndpoint = options.env.SETTLEMINT_PORTAL_GRAPHQL_ENDPOINT; |
129 | | - output = "portal-schema.graphql"; |
130 | | - turboOutput = "portal-cache.d.ts"; |
131 | 128 | break; |
132 | | - case "THEGRAPH": |
| 129 | + case "THE_GRAPH": |
133 | 130 | gqlEndpoint = options.env.SETTLEMINT_THEGRAPH_SUBGRAPH_ENDPOINT; |
134 | | - output = "thegraph-schema.graphql"; |
135 | | - turboOutput = "thegraph-cache.d.ts"; |
136 | 131 | break; |
137 | | - case "THEGRAPH_FALLBACK": |
| 132 | + case "THE_GRAPH_FALLBACK": |
138 | 133 | gqlEndpoint = options.env.SETTLEMINT_THEGRAPH_SUBGRAPH_ENDPOINT_FALLBACK; |
139 | | - output = "thegraph-fallback-schema.graphql"; |
140 | | - turboOutput = "thegraph-fallback-cache.d.ts"; |
141 | 134 | break; |
142 | 135 | } |
143 | 136 |
|
@@ -184,6 +177,33 @@ async function gqltadaCodegen(options: { |
184 | 177 | tsconfig: undefined, |
185 | 178 | headers, |
186 | 179 | }); |
| 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 | + } |
187 | 207 | } catch (error) { |
188 | 208 | if (options.allowToFail) { |
189 | 209 | // ignore |
|
0 commit comments