-
Notifications
You must be signed in to change notification settings - Fork 2
feat: create a codegen command #214
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| .env* | ||
| .env* | ||
| *.schema.graphql |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| 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 type { DotEnv } from "@settlemint/sdk-utils/validation"; | ||
| import { bold, italic, underline } from "yoctocolors"; | ||
|
|
||
| /** | ||
| * Creates and returns the 'connect' command for the SettleMint SDK. | ||
| * This command initializes the setup of the SettleMint SDK in the user's project. | ||
| * It guides the user through a series of prompts to configure their environment, | ||
| * select services, and set up necessary files. | ||
| * | ||
| * @returns {Command} The configured 'connect' command | ||
| */ | ||
| export function codegenCommand(): Command { | ||
| return ( | ||
| new Command("codegen") | ||
| // Add options for various configuration parameters | ||
| .option("-e, --environment <environment>", "The name of your environment, defaults to development", "development") | ||
| // Set the command description | ||
| .description("Generate GraphQL and REST types and queries") | ||
| // Define the action to be executed when the command is run | ||
| .action(async ({ environment }) => { | ||
| intro( | ||
| `Generating GraphQL types and queries for your dApp's ${italic(underline(bold(environment)))} environment`, | ||
| ); | ||
|
|
||
| const env: DotEnv = await loadEnv(); | ||
|
|
||
| await gqltadaSpinner(env); | ||
|
|
||
| outro("Codegen complete"); | ||
| }) | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import { generateSchema } from "@gql.tada/cli-utils"; | ||
| import type { DotEnv } from "@settlemint/sdk-utils/validation"; | ||
|
|
||
| /** | ||
| * Writes environment variables to .env files with a spinner for visual feedback. | ||
| * | ||
| * @param env - Partial environment variables to be written. | ||
| * @param environment - The name of the environment (e.g., "development", "production"). | ||
| * @returns A promise that resolves when the environment variables are written. | ||
| * @throws If there's an error writing the environment files. | ||
| * | ||
| * @example | ||
| * await writeEnvSpinner( | ||
| * { SETTLEMINT_INSTANCE: "https://example.com", SETTLEMINT_ACCESS_TOKEN: "token123" }, | ||
| * "development" | ||
| * ); | ||
| */ | ||
| export async function gqltadaSpinner(env: DotEnv) { | ||
| await gqltadaCodegen({ | ||
| type: "HASURA", | ||
| env, | ||
| }); | ||
| await gqltadaCodegen({ | ||
| type: "PORTAL", | ||
| env, | ||
| }); | ||
| await gqltadaCodegen({ | ||
| type: "THEGRAPH", | ||
| env, | ||
| allowToFail: true, | ||
| }); | ||
| await gqltadaCodegen({ | ||
| type: "THEGRAPH_FALLBACK", | ||
| env, | ||
| }); | ||
| } | ||
|
|
||
| async function gqltadaCodegen(options: { | ||
| type: "HASURA" | "PORTAL" | "THEGRAPH" | "THEGRAPH_FALLBACK"; | ||
| env: DotEnv; | ||
| allowToFail?: boolean; | ||
| }) { | ||
| let gqlEndpoint: string | undefined = undefined; | ||
| let output: 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"; | ||
| adminSecret = options.env.SETTLEMINT_HASURA_ADMIN_SECRET; | ||
| break; | ||
| case "PORTAL": | ||
| gqlEndpoint = options.env.SETTLEMINT_PORTAL_GRAPHQL_ENDPOINT; | ||
| output = "portal.schema.graphql"; | ||
| break; | ||
| case "THEGRAPH": | ||
| gqlEndpoint = options.env.SETTLEMINT_THEGRAPH_SUBGRAPH_ENDPOINT; | ||
| output = "thegraph.schema.graphql"; | ||
| break; | ||
| case "THEGRAPH_FALLBACK": | ||
| gqlEndpoint = options.env.SETTLEMINT_THEGRAPH_SUBGRAPH_ENDPOINT_FALLBACK; | ||
| output = "thegraph-fallback.schema.graphql"; | ||
| } | ||
|
|
||
| if (!gqlEndpoint) { | ||
| return; | ||
| } | ||
|
|
||
| const headers = { | ||
| ...(adminSecret && { "x-hasura-admin-secret": adminSecret }), | ||
| "x-auth-token": accessToken, | ||
| "Content-Type": "application/json", | ||
| }; | ||
|
|
||
| try { | ||
| // Test the endpoint with a simple introspection query | ||
| const response = await fetch(gqlEndpoint, { | ||
| method: "POST", | ||
| headers, | ||
| body: JSON.stringify({ | ||
| query: ` | ||
| query { | ||
| __schema { | ||
| queryType { | ||
| name | ||
| } | ||
| } | ||
| } | ||
| `, | ||
| }), | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error(`Failed to fetch schema: ${response.statusText}`); | ||
| } | ||
|
|
||
| const data = await response.json(); | ||
| if (data.errors) { | ||
| throw new Error(`GraphQL errors: ${JSON.stringify(data.errors)}`); | ||
| } | ||
|
|
||
| await generateSchema({ | ||
| input: gqlEndpoint, | ||
| output, | ||
| tsconfig: undefined, | ||
| headers, | ||
| }); | ||
| } catch (error) { | ||
| if (options.allowToFail) { | ||
| // ignore | ||
| } else { | ||
| throw error; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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: Consider refactoring gqltadaCodegen to reduce repetition in the switch statement
You could create a configuration object that maps types to their respective settings, reducing the need for the switch statement and making it easier to add new types in the future.