diff --git a/openapi.go b/openapi.go index 742ca9e..b65a6ea 100644 --- a/openapi.go +++ b/openapi.go @@ -191,7 +191,11 @@ func (sb *SpecBuilder) buildPaths(groups []preparedRouteGroup) map[string]any { }, } - if graphqlPath := strings.TrimSpace(sb.GraphQLPath); graphqlPath != "" { + graphqlPath := strings.TrimSpace(sb.GraphQLPath) + if graphqlPath == "" && sb.GraphQLPlayground { + graphqlPath = defaultGraphQLPath + } + if graphqlPath != "" { graphqlPath = normaliseOpenAPIPath(graphqlPath) paths[graphqlPath] = graphqlPathItem(graphqlPath, operationIDs) if sb.GraphQLPlayground { diff --git a/openapi_test.go b/openapi_test.go index dae31db..0d0f13f 100644 --- a/openapi_test.go +++ b/openapi_test.go @@ -304,6 +304,32 @@ func TestSpecBuilder_Good_GraphQLPlaygroundEndpoint(t *testing.T) { } } +func TestSpecBuilder_Good_GraphQLPlaygroundDefaultsToGraphQLPath(t *testing.T) { + sb := &api.SpecBuilder{ + Title: "Test", + Version: "1.0.0", + GraphQLPlayground: true, + } + + data, err := sb.Build(nil) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + var spec map[string]any + if err := json.Unmarshal(data, &spec); err != nil { + t.Fatalf("invalid JSON: %v", err) + } + + paths := spec["paths"].(map[string]any) + if _, ok := paths["/graphql"].(map[string]any); !ok { + t.Fatal("expected default /graphql path when playground is enabled") + } + if _, ok := paths["/graphql/playground"].(map[string]any); !ok { + t.Fatal("expected default /graphql/playground path when playground is enabled") + } +} + func TestSpecBuilder_Good_WebSocketEndpoint(t *testing.T) { sb := &api.SpecBuilder{ Title: "Test",