Skip to content

Commit

Permalink
chore(dev): write-sdl.ts writes out stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
bodymindarts committed Nov 7, 2023
1 parent 315100d commit fd35acf
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 52 deletions.
43 changes: 28 additions & 15 deletions core/api/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion core/api/src/graphql/admin/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -537,4 +537,4 @@ enum WalletCurrency {
}

"""Unique identifier of a wallet"""
scalar WalletId
scalar WalletId
2 changes: 1 addition & 1 deletion core/api/src/graphql/public/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -1567,4 +1567,4 @@ enum WalletCurrency {
}

"""Unique identifier of a wallet"""
scalar WalletId
scalar WalletId
52 changes: 20 additions & 32 deletions core/api/src/servers/write-sdl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand All @@ -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()
2 changes: 1 addition & 1 deletion dev/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
)

Expand Down
17 changes: 17 additions & 0 deletions dev/bin/update-schemas.sh
Original file line number Diff line number Diff line change
@@ -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
10 changes: 9 additions & 1 deletion toolchains/rover/macros.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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()
)

Expand All @@ -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],
Expand Down
10 changes: 9 additions & 1 deletion toolchains/rover/output_sdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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",
Expand Down

0 comments on commit fd35acf

Please sign in to comment.