feat(cmd/api): add GraphQL playground spec flag

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-02 02:42:42 +00:00
parent f0b2d8b248
commit 8e28b0209c
3 changed files with 51 additions and 2 deletions

View file

@ -32,6 +32,7 @@ func addSDKCommand(parent *cli.Command) {
description string
version string
graphqlPath string
graphqlPlayground bool
ssePath string
wsPath string
pprofEnabled bool
@ -55,7 +56,7 @@ func addSDKCommand(parent *cli.Command) {
// If no spec file provided, generate one to a temp file.
if specFile == "" {
builder := sdkSpecBuilder(title, description, version, graphqlPath, ssePath, wsPath, pprofEnabled, expvarEnabled, termsURL, contactName, contactURL, contactEmail, licenseName, licenseURL, externalDocsDescription, externalDocsURL, servers)
builder := sdkSpecBuilder(title, description, version, graphqlPath, graphqlPlayground, ssePath, wsPath, pprofEnabled, expvarEnabled, termsURL, contactName, contactURL, contactEmail, licenseName, licenseURL, externalDocsDescription, externalDocsURL, servers)
groups := sdkSpecGroupsIter()
tmpFile, err := os.CreateTemp("", "openapi-*.json")
@ -105,6 +106,7 @@ func addSDKCommand(parent *cli.Command) {
cli.StringFlag(cmd, &description, "description", "d", defaultSDKDescription, "API description in generated spec")
cli.StringFlag(cmd, &version, "version", "V", defaultSDKVersion, "API version in generated spec")
cli.StringFlag(cmd, &graphqlPath, "graphql-path", "", "", "GraphQL endpoint path in generated spec")
cli.BoolFlag(cmd, &graphqlPlayground, "graphql-playground", "", false, "Include the GraphQL playground endpoint in generated spec")
cli.StringFlag(cmd, &ssePath, "sse-path", "", "", "SSE endpoint path in generated spec")
cli.StringFlag(cmd, &wsPath, "ws-path", "", "", "WebSocket endpoint path in generated spec")
cli.BoolFlag(cmd, &pprofEnabled, "pprof", "", false, "Include pprof endpoints in generated spec")
@ -122,12 +124,13 @@ func addSDKCommand(parent *cli.Command) {
parent.AddCommand(cmd)
}
func sdkSpecBuilder(title, description, version, graphqlPath, ssePath, wsPath string, pprofEnabled, expvarEnabled bool, termsURL, contactName, contactURL, contactEmail, licenseName, licenseURL, externalDocsDescription, externalDocsURL, servers string) *goapi.SpecBuilder {
func sdkSpecBuilder(title, description, version, graphqlPath string, graphqlPlayground bool, ssePath, wsPath string, pprofEnabled, expvarEnabled bool, termsURL, contactName, contactURL, contactEmail, licenseName, licenseURL, externalDocsDescription, externalDocsURL, servers string) *goapi.SpecBuilder {
return &goapi.SpecBuilder{
Title: title,
Description: description,
Version: version,
GraphQLPath: graphqlPath,
GraphQLPlayground: graphqlPlayground,
SSEPath: ssePath,
WSPath: wsPath,
PprofEnabled: pprofEnabled,

View file

@ -19,6 +19,7 @@ func addSpecCommand(parent *cli.Command) {
description string
version string
graphqlPath string
graphqlPlayground bool
ssePath string
wsPath string
pprofEnabled bool
@ -41,6 +42,7 @@ func addSpecCommand(parent *cli.Command) {
Description: description,
Version: version,
GraphQLPath: graphqlPath,
GraphQLPlayground: graphqlPlayground,
SSEPath: ssePath,
WSPath: wsPath,
PprofEnabled: pprofEnabled,
@ -76,6 +78,7 @@ func addSpecCommand(parent *cli.Command) {
cli.StringFlag(cmd, &description, "description", "d", "Lethean Core API", "API description in spec")
cli.StringFlag(cmd, &version, "version", "V", "1.0.0", "API version in spec")
cli.StringFlag(cmd, &graphqlPath, "graphql-path", "", "", "GraphQL endpoint path in generated spec")
cli.BoolFlag(cmd, &graphqlPlayground, "graphql-playground", "", false, "Include the GraphQL playground endpoint in generated spec")
cli.StringFlag(cmd, &ssePath, "sse-path", "", "", "SSE endpoint path in generated spec")
cli.StringFlag(cmd, &wsPath, "ws-path", "", "", "WebSocket endpoint path in generated spec")
cli.BoolFlag(cmd, &pprofEnabled, "pprof", "", false, "Include pprof endpoints in generated spec")

View file

@ -85,6 +85,9 @@ func TestAPISpecCmd_Good_JSON(t *testing.T) {
if specCmd.Flag("graphql-path") == nil {
t.Fatal("expected --graphql-path flag on spec command")
}
if specCmd.Flag("graphql-playground") == nil {
t.Fatal("expected --graphql-playground flag on spec command")
}
if specCmd.Flag("sse-path") == nil {
t.Fatal("expected --sse-path flag on spec command")
}
@ -156,6 +159,42 @@ func TestAPISpecCmd_Good_CustomDescription(t *testing.T) {
}
}
func TestAPISpecCmd_Good_GraphQLPlaygroundFlagPopulatesSpecPaths(t *testing.T) {
root := &cli.Command{Use: "root"}
AddAPICommands(root)
outputFile := t.TempDir() + "/spec.json"
root.SetArgs([]string{
"api", "spec",
"--graphql-path", "/graphql",
"--graphql-playground",
"--output", outputFile,
})
root.SetErr(new(bytes.Buffer))
if err := root.Execute(); err != nil {
t.Fatalf("unexpected error: %v", err)
}
data, err := os.ReadFile(outputFile)
if err != nil {
t.Fatalf("expected spec file to be written: %v", err)
}
var spec map[string]any
if err := json.Unmarshal(data, &spec); err != nil {
t.Fatalf("expected valid JSON spec, got error: %v", err)
}
paths, ok := spec["paths"].(map[string]any)
if !ok {
t.Fatal("expected paths object in generated spec")
}
if _, ok := paths["/graphql/playground"]; !ok {
t.Fatal("expected GraphQL playground path in generated spec")
}
}
func TestAPISpecCmd_Good_ContactFlagsPopulateSpecInfo(t *testing.T) {
root := &cli.Command{Use: "root"}
AddAPICommands(root)
@ -652,6 +691,7 @@ func TestAPISDKCmd_Good_TempSpecUsesMetadataFlags(t *testing.T) {
"Custom SDK description",
"9.9.9",
"/gql",
true,
"/events",
"/ws",
true,
@ -704,6 +744,9 @@ func TestAPISDKCmd_Good_TempSpecUsesMetadataFlags(t *testing.T) {
if _, ok := paths["/gql"]; !ok {
t.Fatal("expected GraphQL path to be included in generated spec")
}
if _, ok := paths["/gql/playground"]; !ok {
t.Fatal("expected GraphQL playground path to be included in generated spec")
}
if _, ok := paths["/events"]; !ok {
t.Fatal("expected SSE path to be included in generated spec")
}