Skip to content

Commit

Permalink
chore(dev): buckify sdl gen / diff (#3511)
Browse files Browse the repository at this point in the history
* chore(toolchains): add rover toolchain

* chore(dev): buckify sdl updating / diffing

* chore(dev): write-sdl.ts writes out stdout

* chore: add update-schemas.sh
  • Loading branch information
bodymindarts authored Nov 7, 2023
1 parent a48f6f9 commit 2f43ae9
Show file tree
Hide file tree
Showing 24 changed files with 491 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,4 @@ jobs:
- name: Run the Magic Nix Cache
uses: DeterminateSystems/magic-nix-cache-action@v2
- uses: actions/checkout@v3
- run: nix develop -c buck2 run //dev:update-supergraph
- name: Run check code
run: nix develop -c buck2 test //dev:check-sdl
- run: nix develop -c buck2 test //dev:check-sdls
2 changes: 1 addition & 1 deletion ci/apps/app-template.lib.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ plan:
- { get: pipeline-tasks }
- task: #@ tilt_integration_test_name(app)
attempts: 2
timeout: 12m
timeout: 18m
tags: ["galoy-staging"]
config:
platform: linux
Expand Down
20 changes: 20 additions & 0 deletions core/api-keys/BUCK
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
load("@toolchains//rover:macros.bzl", "sdl", "diff_check", "dev_update_file")

sdl(
name = "sdl",
generator = ":write-sdl",
visibility = ["PUBLIC"],
)

diff_check(
name = "schema-diff",
original = "subgraph/schema.graphql",
new = ":sdl"
)

dev_update_file(
name = "update-schema",
generated = ":sdl",
out = "subgraph/schema.graphql"
)

rust_binary(
name = "write-sdl",
edition = "2021",
Expand Down
40 changes: 40 additions & 0 deletions core/api/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ load(
"yaml_check",
"madge_check",
)
load("@toolchains//rover:macros.bzl", "sdl", "diff_check", "dev_update_file")

dev_pnpm_task_binary(
name = "lint-fix",
Expand Down Expand Up @@ -84,6 +85,43 @@ prod_tsc_build_bin(
run_file = "servers/write-sdl.js",
)

sdl(
name = "public-sdl",
generator = ":write-sdl",
args = ["public"],
visibility = ["PUBLIC"],
)

diff_check(
name = "public-schema-diff",
original = "src/graphql/public/schema.graphql",
new = ":public-sdl"
)

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 = "src/graphql/admin/schema.graphql",
new = ":admin-sdl"
)

dev_update_file(
name = "update-admin-schema",
generated = ":admin-sdl",
out = "src/graphql/admin/schema.graphql"
)

eslint(
name = "check-lint",
srcs = [":src"] + [":test_src"] + glob([".eslint*"]),
Expand Down Expand Up @@ -118,5 +156,7 @@ test_suite(
":check-type",
":check-yaml",
":check-circular-dependencies",
":public-schema-diff",
":admin-schema-diff",
],
)
2 changes: 1 addition & 1 deletion core/api/dev/apollo-federation/supergraph.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2024,4 +2024,4 @@ enum WalletCurrency

"""Unique identifier of a wallet"""
scalar WalletId
@join__type(graph: PUBLIC)
@join__type(graph: PUBLIC)
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()
43 changes: 38 additions & 5 deletions dev/BUCK
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("@toolchains//rover:macros.bzl", "supergraph", "diff_check", "dev_update_file")
load( ":tilt.bzl", "tilt_down", "tilt_up",)

# Bring up the full set of services for development
Expand All @@ -16,14 +17,46 @@ python_bootstrap_binary(
visibility = ["PUBLIC"],
)

sh_binary(
supergraph(
name = "supergraph",
config = "config/apollo-federation/supergraph-config.yaml",
subgraphs = {
"API_KEYS_SCHEMA": "//core/api-keys:sdl",
"PUBLIC_SCHEMA": "//core/api:public-sdl",
},
)

diff_check(
name = "supergraph-diff",
original = "config/apollo-federation/supergraph.graphql",
new = ":supergraph"
)

test_suite(
name = "check-sdls",
tests = [
":supergraph-diff",
"//core/api:public-schema-diff",
"//core/api:admin-schema-diff",
"//core/api-keys:schema-diff",
],
)

dev_update_file(
name = "update-supergraph",
main = "bin/update-supergraph.sh",
generated = ":supergraph",
out = "config/apollo-federation/supergraph.graphql"
)

sh_test(
name = "check-sdl",
test = "bin/check-sdl.sh",
dev_update_file(
name = "update-core-supergraph",
generated = ":supergraph",
out = "../core/api/dev/apollo-federation/supergraph.graphql"
)

sh_binary(
name = "update-schemas",
main = "bin/update-schemas.sh",
)

sh_binary(
Expand Down
15 changes: 0 additions & 15 deletions dev/bin/check-sdl.sh

This file was deleted.

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
20 changes: 0 additions & 20 deletions dev/bin/update-supergraph.sh

This file was deleted.

4 changes: 2 additions & 2 deletions dev/config/apollo-federation/supergraph-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ subgraphs:
public:
routing_url: http://bats-tests:4012/graphql
schema:
file: ../../../core/api/src/graphql/public/schema.graphql
file: ${env.PUBLIC_SCHEMA:-../../../core/api/src/graphql/public/schema.graphql}
api_keys:
routing_url: http://bats-tests:5397/graphql
schema:
file: ../../../core/api-keys/subgraph/schema.graphql
file: ${env.API_KEYS_SCHEMA:-../../../core/api-keys/subgraph/schema.graphql}
2 changes: 1 addition & 1 deletion dev/config/apollo-federation/supergraph.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2024,4 +2024,4 @@ enum WalletCurrency

"""Unique identifier of a wallet"""
scalar WalletId
@join__type(graph: PUBLIC)
@join__type(graph: PUBLIC)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions toolchains/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,10 @@ workspace_pnpm_toolchain(
name = "workspace_pnpm",
visibility = ["PUBLIC"],
)

load("@toolchains//rover:toolchain.bzl", "rover_toolchain")

rover_toolchain(
name = "rover",
visibility = ["PUBLIC"],
)
9 changes: 9 additions & 0 deletions toolchains/rover/BUCK
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export_file(
name = "output_sdl.py",
visibility = ["PUBLIC"],
)

export_file(
name = "generate_supergraph.py",
visibility = ["PUBLIC"],
)
Loading

0 comments on commit 2f43ae9

Please sign in to comment.