@@ -363,3 +363,126 @@ func TestNewSubGraphV2_WithComposeDirective(t *testing.T) {
363363 t .Errorf ("expected compose directive '@custom', got '%s'" , composeDirectives [0 ])
364364 }
365365}
366+
367+ func TestNewSubGraphV2_ExtractsDirectiveDefinitions (t * testing.T ) {
368+ schema := `
369+ schema @composeDirective(name: "@rateLimit") {
370+ query: Query
371+ }
372+
373+ directive @rateLimit(limit: Int!, duration: Int!) on FIELD_DEFINITION
374+
375+ type Query {
376+ expensiveQuery: String! @rateLimit(limit: 10, duration: 60)
377+ }
378+ `
379+
380+ sg , err := graph .NewSubGraphV2 ("product" , []byte (schema ), "http://product.example.com" )
381+ if err != nil {
382+ t .Fatalf ("NewSubGraphV2 failed: %v" , err )
383+ }
384+
385+ defs := sg .GetDirectiveDefinitions ()
386+ if len (defs ) != 1 {
387+ t .Fatalf ("expected 1 directive definition, got %d" , len (defs ))
388+ }
389+
390+ def , ok := defs ["rateLimit" ]
391+ if ! ok {
392+ t .Fatal ("expected 'rateLimit' directive definition, not found" )
393+ }
394+
395+ if def .Name .String () != "rateLimit" {
396+ t .Errorf ("expected directive name 'rateLimit', got '%s'" , def .Name .String ())
397+ }
398+ }
399+
400+ func TestNewSubGraphV2_ExtractsMultipleDirectiveDefinitions (t * testing.T ) {
401+ schema := `
402+ schema
403+ @composeDirective(name: "@rateLimit")
404+ @composeDirective(name: "@cacheControl") {
405+ query: Query
406+ }
407+
408+ directive @rateLimit(limit: Int!) on FIELD_DEFINITION
409+ directive @cacheControl(maxAge: Int!) on FIELD_DEFINITION | OBJECT
410+
411+ type Query {
412+ cachedData: String! @cacheControl(maxAge: 3600) @rateLimit(limit: 100)
413+ }
414+ `
415+
416+ sg , err := graph .NewSubGraphV2 ("product" , []byte (schema ), "http://product.example.com" )
417+ if err != nil {
418+ t .Fatalf ("NewSubGraphV2 failed: %v" , err )
419+ }
420+
421+ defs := sg .GetDirectiveDefinitions ()
422+ if len (defs ) != 2 {
423+ t .Fatalf ("expected 2 directive definitions, got %d" , len (defs ))
424+ }
425+
426+ if _ , ok := defs ["rateLimit" ]; ! ok {
427+ t .Error ("expected 'rateLimit' directive definition, not found" )
428+ }
429+ if _ , ok := defs ["cacheControl" ]; ! ok {
430+ t .Error ("expected 'cacheControl' directive definition, not found" )
431+ }
432+ }
433+
434+ func TestNewSubGraphV2_DirectiveDefinitions_OnlyComposed (t * testing.T ) {
435+ // @internal is defined but NOT listed in @composeDirective, so should not be extracted
436+ schema := `
437+ schema @composeDirective(name: "@rateLimit") {
438+ query: Query
439+ }
440+
441+ directive @rateLimit(limit: Int!) on FIELD_DEFINITION
442+ directive @internal on FIELD_DEFINITION
443+
444+ type Query {
445+ data: String! @rateLimit(limit: 10) @internal
446+ }
447+ `
448+
449+ sg , err := graph .NewSubGraphV2 ("product" , []byte (schema ), "http://product.example.com" )
450+ if err != nil {
451+ t .Fatalf ("NewSubGraphV2 failed: %v" , err )
452+ }
453+
454+ defs := sg .GetDirectiveDefinitions ()
455+ if len (defs ) != 1 {
456+ t .Fatalf ("expected 1 directive definition (only composed ones), got %d" , len (defs ))
457+ }
458+
459+ if _ , ok := defs ["rateLimit" ]; ! ok {
460+ t .Error ("expected 'rateLimit' directive definition, not found" )
461+ }
462+ if _ , ok := defs ["internal" ]; ok {
463+ t .Error ("'internal' directive should not be extracted (not in @composeDirective)" )
464+ }
465+ }
466+
467+ func TestNewSubGraphV2_NoComposeDirective_EmptyDefinitions (t * testing.T ) {
468+ schema := `
469+ type Product @key(fields: "id") {
470+ id: ID!
471+ name: String!
472+ }
473+
474+ type Query {
475+ product(id: ID!): Product
476+ }
477+ `
478+
479+ sg , err := graph .NewSubGraphV2 ("product" , []byte (schema ), "http://product.example.com" )
480+ if err != nil {
481+ t .Fatalf ("NewSubGraphV2 failed: %v" , err )
482+ }
483+
484+ defs := sg .GetDirectiveDefinitions ()
485+ if len (defs ) != 0 {
486+ t .Errorf ("expected 0 directive definitions, got %d" , len (defs ))
487+ }
488+ }
0 commit comments