diff --git a/core/api/BUCK b/core/api/BUCK index bd0bf1a7c06..be001e3423e 100644 --- a/core/api/BUCK +++ b/core/api/BUCK @@ -10,7 +10,7 @@ load( "yaml_check", "madge_check", ) -load("@toolchains//rover:macros.bzl", "sdl", "diff_check") +load("@toolchains//rover:macros.bzl", "sdl", "diff_check", "dev_update_file") dev_pnpm_task_binary( name = "lint-fix", @@ -80,33 +80,46 @@ prod_tsc_build_bin( run_file = "servers/graphql-main-server.js", ) -export_file( - name = "public.schema", - src = "src/graphql/public/schema.graphql", +prod_tsc_build_bin( + name = "write-sdl", + run_file = "servers/write-sdl.js", +) + +sdl( + name = "public-sdl", + generator = ":write-sdl", + args = ["public"], visibility = ["PUBLIC"], ) diff_check( name = "public-schema-diff", - original = ":public.schema", - new = ":public.schema" + original = "src/graphql/public/schema.graphql", + new = ":public-sdl" ) -export_file( - name = "admin.schema", - src = "src/graphql/admin/schema.graphql", - visibility = ["PUBLIC"], +dev_update_file( + name = "update-public-schema", + generated = ":public-sdl", + out = "src/graphql/public/schema.graphql" +) + +sdl( + name = "admin-sdl", + generator = ":write-sdl", + args = ["admin"] ) diff_check( name = "admin-schema-diff", - original = ":admin.schema", - new = ":admin.schema" + original = "src/graphql/admin/schema.graphql", + new = ":admin-sdl" ) -prod_tsc_build_bin( - name = "write-sdl", - run_file = "servers/write-sdl.js", +dev_update_file( + name = "update-admin-schema", + generated = ":admin-sdl", + out = "src/graphql/admin/schema.graphql" ) eslint( diff --git a/core/api/src/graphql/admin/schema.graphql b/core/api/src/graphql/admin/schema.graphql index 99fb5fd4a3e..c3fc363e9c6 100644 --- a/core/api/src/graphql/admin/schema.graphql +++ b/core/api/src/graphql/admin/schema.graphql @@ -537,4 +537,4 @@ enum WalletCurrency { } """Unique identifier of a wallet""" -scalar WalletId \ No newline at end of file +scalar WalletId diff --git a/core/api/src/graphql/public/schema.graphql b/core/api/src/graphql/public/schema.graphql index 17c370aeec8..c1bda9c3039 100644 --- a/core/api/src/graphql/public/schema.graphql +++ b/core/api/src/graphql/public/schema.graphql @@ -1567,4 +1567,4 @@ enum WalletCurrency { } """Unique identifier of a wallet""" -scalar WalletId \ No newline at end of file +scalar WalletId diff --git a/core/api/src/servers/write-sdl.ts b/core/api/src/servers/write-sdl.ts index 882d39bcae4..1a9cb4991d0 100644 --- a/core/api/src/servers/write-sdl.ts +++ b/core/api/src/servers/write-sdl.ts @@ -25,9 +25,6 @@ for (const [envVar, defaultValue] of Object.entries(envVarsWithDefaults)) { } } -import fs from "fs/promises" -import path from "path" - import { GraphQLSchema, lexicographicSortSchema, printSchema } from "graphql" import { QueryType as QueryTypeAdmin } from "@/graphql/admin/queries" @@ -37,13 +34,13 @@ import { ALL_INTERFACE_TYPES as ALL_INTERFACE_TYPES_ADMIN } from "@/graphql/admi import { ALL_INTERFACE_TYPES } from "@/graphql/public/types" import { QueryType } from "@/graphql/public/queries" - import { MutationType } from "@/graphql/public/mutations" import { SubscriptionType } from "@/graphql/public/subscriptions" export { queryFields } from "@/graphql/public/queries" export { mutationFields } from "@/graphql/public/mutations" +// Define the schemas export const gqlAdminSchema = new GraphQLSchema({ query: QueryTypeAdmin, mutation: MutationTypeAdmin, @@ -57,39 +54,30 @@ export const gqlPublicSchema = new GraphQLSchema({ types: ALL_INTERFACE_TYPES, }) -const packageRoot = process.argv[2] || __dirname +// Main function to handle the script's logic +const main = async () => { + const schemaType = process.argv[2] // Accepts 'admin' or 'public' -;(async () => { try { - console.log("write public schema") - - const schemaPublicPath = path.resolve( - packageRoot, - "src/graphql/public/schema.graphql", - ) - console.log(`Writing to path: ${schemaPublicPath}`) - - const sortedPublicSchema = printSchema(lexicographicSortSchema(gqlPublicSchema)) - - const fileHandleMain = await fs.open(schemaPublicPath, "w") - await fileHandleMain.writeFile(sortedPublicSchema) - await fileHandleMain.close() - - console.log("write admin schema") + let schema + if (schemaType === "admin") { + schema = gqlAdminSchema + } else if (schemaType === "public") { + schema = gqlPublicSchema + } else { + throw new Error('Please specify "admin" or "public" as an argument.') + } - const schemaAdminPath = path.resolve(packageRoot, "src/graphql/admin/schema.graphql") - console.log(`Writing to path: ${schemaAdminPath}`) + const sortedSchema = printSchema(lexicographicSortSchema(schema)) - const sortedAdminSchema = printSchema(lexicographicSortSchema(gqlAdminSchema)) + console.log(sortedSchema) // Prints the sorted schema to stdout - const fileHandle = await fs.open(schemaAdminPath, "w") - await fileHandle.writeFile(sortedAdminSchema) - await fileHandle.close() - - console.log("done") + process.exit(0) } catch (error) { console.error("An error occurred:", error) - } finally { - process.exit(0) + process.exit(1) // Exit with a non-zero status code to indicate an error } -})() +} + +// Execute the main function +main() diff --git a/dev/BUCK b/dev/BUCK index da70173bf6d..ca1861a5381 100644 --- a/dev/BUCK +++ b/dev/BUCK @@ -22,7 +22,7 @@ supergraph( config = "config/apollo-federation/supergraph-config.yaml", subgraphs = { "API_KEYS_SCHEMA": "//core/api-keys:sdl", - "PUBLIC_SCHEMA": "//core/api:public.schema", + "PUBLIC_SCHEMA": "//core/api:public-sdl", }, ) diff --git a/dev/bin/update-schemas.sh b/dev/bin/update-schemas.sh new file mode 100755 index 00000000000..ea9832da57f --- /dev/null +++ b/dev/bin/update-schemas.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +set -e + +TARGETS=( + "//dev:update-supergraph" + "//dev:update-core-supergraph" + "//core/api:update-public-schema" + "//core/api:update-admin-schema" + "//core/api-keys:update-schema" +) + +buck2 build "${TARGETS[@]}" + +for TARGET in "${TARGETS[@]}"; do + buck2 run "$TARGET" +done diff --git a/toolchains/rover/macros.bzl b/toolchains/rover/macros.bzl index da2ef860365..a3c25dc3f45 100644 --- a/toolchains/rover/macros.bzl +++ b/toolchains/rover/macros.bzl @@ -56,7 +56,7 @@ def diff_impl( diff_check = rule( impl = diff_impl, attrs = { - "original": attrs.string( + "original": attrs.source( doc = """The original file on disk""", ), "new": attrs.source( @@ -129,6 +129,10 @@ def sdl_impl(ctx: AnalysisContext) -> list[DefaultInfo]: rover_toolchain.output_sdl[DefaultInfo].default_outputs, "--generator-bin", ctx.attrs.generator[RunInfo], + ) + for arg in ctx.attrs.args: + cmd.add("--arg", arg) + cmd.add( out.as_output() ) @@ -142,6 +146,10 @@ sdl = rule( providers = [RunInfo], doc = """Generator that will output the sdl""", ), + "args": attrs.list( + attrs.string(), + default = [], + ), "_python_toolchain": attrs.toolchain_dep( default = "toolchains//:python", providers = [PythonToolchainInfo], diff --git a/toolchains/rover/output_sdl.py b/toolchains/rover/output_sdl.py index 94986e8078f..202f056121f 100644 --- a/toolchains/rover/output_sdl.py +++ b/toolchains/rover/output_sdl.py @@ -24,7 +24,8 @@ def main(args): # Run the generator binary and redirect stdout to the out-path try: with open(out_path, 'w') as out_file: - result = subprocess.run([generator_bin], stdout=out_file, stderr=subprocess.PIPE, text=True) + command = [generator_bin] + args.additional_args + result = subprocess.run(command, stdout=out_file, stderr=subprocess.PIPE, text=True) # Check if the generator command was successful if result.returncode != 0: @@ -44,6 +45,13 @@ def main(args): required=True, help="Path to the generator binary", ) + parser.add_argument( + "--arg", + action='append', + dest='additional_args', + default=[], + help="Additional arguments to pass to the generator script prefixed with --arg" + ) parser.add_argument( "out_path", help="Path to output the schema to",